Using Graphics to draw on a Swing/AWT Component

View previous topic View next topic Go down

Java Using Graphics to draw on a Swing/AWT Component

Post by BIT0102-Mohaimin on Tue Aug 03, 2010 4:03 am


This topic lacks in information and may create misconception. You are suggested the following tutorials to learn about painting.
1. http://iitdu.forumsmotion.com/tutorials-f93/painting-in-swing-components-part-1-t950.htm
2. http://iitdu.forumsmotion.com/tutorials-f93/java-tutorial-painting-in-swing-components-part-2-interactive-painting-t1111.htm
The topic is locked.


Objective this tutorial is to help you draw on Swing Components using Graphics, not to make you understand this. This is because I am not very clear about the inner concept. I am just used to use it. So, do not ask me "how does it work".

Few things you should know before starting are:

  • JComponent is an abstract class defined in javax.swing package

  • All swing components extend JComponent



All swing components has a graphics mode [I don't know what it means]. You can obtain this using the method getGraphics() defined by JComponent. With this you can draw various geometric figures on the component. I am giving you an example, I hope this will will make you clear enough to use Graphics.

Code:

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Draw extends JPanel {
    public void letsDraw(){
        Graphics g = getGraphics();
        if (g == null) {
            throw new NullPointerException();
        }
        int[] xPoints = { 30, 40, 90, 230, 245, 230, 130 }, yPoints = { 40, 20,
                25, 40, 50, 140, 170 };

        g.setColor(Color.CYAN); // Sets the line color
        g.drawLine(100, 100, 375, 430); // Draw a line
        g.setColor(Color.RED);
        g.drawRect(30, 40, 200, 100); // Draw a rectangle
        g.setColor(Color.GREEN);
        g.drawOval(30, 40, 200, 100); // Draw an oval, you can draw circles                                                    // with this
        g.setColor(Color.BLUE);
        g.drawPolygon(xPoints, yPoints, xPoints.length); // Draw a polygon
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.setSize(400, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Draw d = new Draw();
        frame.add(d);

        frame.setVisible(true);
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        d.letsDraw();
    }
}


NullPointerException will be thrown if you try to use Graphics of a component when the component is not visible. That means you must call the letsDraw() method after you have added the JPanel to the Container (or some other container) and after the mother container is set visible.

Please read about repaint(), paint(), paintComponent(), paintComponents(), paintChildren() methods defined by JComponent.

Some of the information I supplied may not be correct. Because I have made this tutorial from a very vague idea about the topic. If you find something confusing, please let me know.


Last edited by BIT0102-Mohaimin on Tue Nov 23, 2010 2:29 am; edited 2 times in total (Reason for editing : Depreciating)
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0112-Rokon on Tue Aug 03, 2010 4:24 am

Nice work BIT0102-Mohaimin .

rep++

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0122-Amit on Tue Aug 03, 2010 5:17 am

that was close ^_^!

you didn't properly use the code tag. keep in mind, it is not
Code:

<code>
your code here
</code>


It is actually,
Code:

[code]
your code here
[/code]




Anyway, considering that it is not exactly a proper tutorial, and as you said, we can not understand how it works but how to make it work, I think this should be moved to Java Programmer's lounge corner.

besides, we can discuss about it there and if enough things come out, we can then convert it to a fully fledged tutorial Very Happy

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0112-Rokon on Tue Aug 03, 2010 5:26 am

yap...

Ami asole arokom akta report korte chaicilam.. but mohaimin jodi mind kore ..tai kori na...

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0122-Amit on Tue Aug 03, 2010 5:37 am

bit0112-rokon wrote:yap...

Ami asole arokom akta report korte chaicilam.. but mohaimin jodi mind kore ..tai kori na...

mind korbi bole report korli na.. saiful re to dharam kore korsili Neutral

But then again, if i remember correctly, you said remaining as a friend is more important to you than marching to them saying that they should do it some other way.

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0112-Rokon on Tue Aug 03, 2010 6:13 am

Saiful ar tai beshi problem cilo... aita te problem kom tai...!!

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0122-Amit on Wed Aug 04, 2010 3:41 am

hoise thak :p
saiful er tae khali naming convention e problem dabi kora hoise :p
ar to kisu na :p

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0112-Rokon on Wed Aug 04, 2010 4:46 am

BIT0122-Amit wrote:hoise thak :p
saiful er tae khali naming convention e problem dabi kora hoise :p
ar to kisu na :p


oita tate naming convention vul cilo.. + vul jaigai post kora cilo....

_________________________________________________________________


Code Explosion Blog | Code Explosion Wiki | The Rokonoid | নির্ঝরিণী
avatar
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

View user profile http://blog.codexplo.org

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0122-Amit on Wed Aug 04, 2010 4:54 am

well, the report was about the inappropriate place, not the wrong convention very very funny

_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0102-Mohaimin on Thu Aug 05, 2010 12:04 am

Ok, maramari kor. J Jitbe tar kotha thik...
By the way... Report dile ki hoy??
avatar
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

View user profile

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by BIT0122-Amit on Thu Aug 05, 2010 12:09 am


_________________________________________________________________
Adminship / Moderatorship is not about power, it is about Responsibility.

|About me|My Blog|
avatar
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

View user profile http://iitdu.forumsmotion.com

Back to top Go down

Java Re: Using Graphics to draw on a Swing/AWT Component

Post by Sponsored content


Sponsored content


Back to top Go down

View previous topic View next topic Back to top


 
Permissions in this forum:
You cannot reply to topics in this forum