Results 1 to 11 of 11
  1. #1
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Paint Graphics2D objects onto a JPanel

    I was wondering how you would paint a Graphics2D object onto a JPanel? I have just started exploring applications and 2d graphics support.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Paint Graphics2D objects onto a JPanel

    Quote Originally Posted by sublixt View Post
    I was wondering how you would paint a Graphics2D object onto a JPanel? I have just started exploring applications and 2d graphics support.
    You don't. You use the Graphics/Graphics2D object given to you in the JPanel's paintComponent method. Please note that you can also use the Graphics/Graphics2D object obtained from a BufferedImage and this can then be displayed in a Swing JComponent.

  3. #3
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: Paint Graphics2D objects onto a JPanel

    <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
    Last edited by sublixt; 10-16-2011 at 03:04 AM.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Paint Graphics2D objects onto a JPanel

    Quote Originally Posted by sublixt View Post
    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)
    Nope, you're not searching the wrong place -- Search that page for paintComponent -- you'll find it.

  5. #5
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: Paint Graphics2D objects onto a JPanel

    one more question how do i recall the paintComponent method after the first time that it is called?

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Paint Graphics2D objects onto a JPanel

    Quote Originally Posted by sublixt View Post
    one more question how do i recall the paintComponent method after the first time that it is called?
    You never call it directly. Instead call repaint() on the component.

  7. #7
    sublixt's Avatar
    sublixt is offline Member
    Join Date
    Jul 2011
    Location
    somewhere
    Posts
    37
    Rep Power
    0

    Default Re: Paint Graphics2D objects onto a JPanel

    hmm i am doing a very bad job at reading the javadocs i was looking particularity for that method. Well thanks for the help.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Paint Graphics2D objects onto a JPanel

    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

  9. #9
    camickr is offline Senior Member
    Join Date
    Jul 2009
    Posts
    1,234
    Rep Power
    7

    Default Re: Paint Graphics2D objects onto a JPanel

    hmm i am doing a very bad job at reading the javadocs i was looking particularity for that method.
    In this case a better place to start is: Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing)

  10. #10
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

    Default Re: Paint Graphics2D objects onto a JPanel

    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

  11. #11
    Grkn is offline Member
    Join Date
    Oct 2011
    Posts
    65
    Rep Power
    0

Similar Threads

  1. How To Draw Objects Not On A JPanel
    By anthropamorphic in forum New To Java
    Replies: 18
    Last Post: 08-11-2011, 05:55 AM
  2. Placing a new JPanel over a paint overriden JPanel
    By Tanshaydar in forum AWT / Swing
    Replies: 4
    Last Post: 12-08-2010, 06:00 PM
  3. Java Paint Program problem (JPanel)
    By KilKidd in forum Advanced Java
    Replies: 6
    Last Post: 11-20-2010, 04:31 AM
  4. paint vs paintComponent for a JPanel
    By lightstream in forum AWT / Swing
    Replies: 4
    Last Post: 01-29-2009, 02:26 AM
  5. help?! paint on top of swing components on JPanel
    By beam2008 in forum AWT / Swing
    Replies: 1
    Last Post: 12-05-2008, 04:15 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •