Results 1 to 2 of 2
  1. #1
    such orb is offline Member
    Join Date
    Mar 2012
    Posts
    24
    Rep Power
    0

    Default can several seperate paint component classes draw on the one jpanel

    i am trying to use good oop practices for my gui game. as the question asks, can you draw seperately on one container?

    some code:

    Java Code:
    @Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    
    		g2.setColor(Color.RED);
    
    		g2.fillRoundRect(600, 100, 100, 100, 20, 20);
    
    		g2.setBackground(Color.BLUE);
    		g2.setColor(Color.CYAN);
    		g2.fillOval(400, 200, 200, 200);
    more code:


    Java Code:
    @Override
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
    				RenderingHints.VALUE_ANTIALIAS_ON);
    
    		g2.setColor(Color.RED);
    
    		g2.fillRoundRect(200, 100, 100, 100, 20, 20);
    
    		g2.setBackground(Color.BLUE);
    		g2.setColor(Color.CYAN);
    		g2.fillOval(700, 200, 200, 200);
    when i draw with both classes, only one of the classes actually draws. is there a solution?

    thx!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,839
    Rep Power
    8

    Default Re: can several seperate paint component classes draw on the one jpanel

    Not really sure what you're actually doing with that code- an SSCCE would be much better.

    You can pass the Graphics instance to whatever method you want, including methods in other classes that contain some rendering logic. You only have to override paintComponent once though.
    How to Ask Questions the Smart Way
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Can't paint my Buffered image on my component?
    By Bram in forum New To Java
    Replies: 2
    Last Post: 06-05-2012, 04:22 PM
  2. Replies: 1
    Last Post: 05-20-2011, 01:59 PM
  3. 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
  4. Conditions through seperate classes
    By LennyKosmos in forum New To Java
    Replies: 9
    Last Post: 09-22-2010, 04:10 PM
  5. Custom component and paint outside of bounds
    By happy_hippie in forum AWT / Swing
    Replies: 2
    Last Post: 06-30-2010, 05:41 PM

Posting Permissions

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