I was wondering how you would paint a Graphics2D object onto a JPanel? I have just started exploring applications and 2d graphics support.
Printable View
I was wondering how you would paint a Graphics2D object onto a JPanel? I have just started exploring applications and 2d graphics support.
<s>im sorry but i cannot seem to find the paintComponent method in the java doc. am i looking in the worng place?
JPanel (Java Platform SE 6)</s>
edit: sorry i missed the last sentence that talked about jcomponent
one more question how do i recall the paintComponent method after the first time that it is called?
hmm i am doing a very bad job at reading the javadocs i was looking particularity for that method. Well thanks for the help.
If you want a more advanced tutorial on painting with Swing and AWT, please have a look at this great article: Painting in AWT and Swing
In this case a better place to start is: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)Quote:
hmm i am doing a very bad job at reading the javadocs i was looking particularity for that method.
here I will write simple code for you
-class myDrawing extends JPanel{
- public myDrawing(){
-
- }
- protected void paintComponent(Graphics g){
- Graphics2D g2=(Graphics2D) g;
- Shape p=new Rectangle(0,0,100,100);
- g2.setColor(Color.RED);
- g2.draw(p);
- g2.fill(p);
- }
- public static void main(String[] args){
- JFrame myFrame=new JFrame();
- myFrame.setBounds(0,0,500,500);
- myFrame.add(new myDrawing());
- myFrame.setVisible(true);
-}
I have not compiled this code I only write in reply thread
But in this forum, I wrote a code before about drawings and mouse click detection in that drawing
http://www.java-forums.org/awt-swing...tml#post241249
the link is compiled code