Results 1 to 9 of 9
  1. #1
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default paintComponent() Trouble

    Hey there.

    I'm having a little trouble trying to get the "paintComponent()" method from JPanel to work right.

    So far this is my code:

    basicWindow.java
    Java Code:
    package tests;
    
    import javax.swing.*;
    
    @SuppressWarnings("serial")
    public class basicWindow extends JFrame{	
    	public basicWindow(String windowTitle, int sizeX, int sizeY) {
    		super(windowTitle);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setSize(sizeX, sizeY);
    	}
    	
    	public void display() {
    		setVisible(true);
    	}
    }
    mainClass.java
    Java Code:
    package tests;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class mainClass {
    	public static basicWindow wnd;
    	public static myCanvas mc;
    	
    	public static void main(String argv[]) {
    		wnd = new basicWindow("Drawing", 320, 240);
    		mc = new myCanvas(200, 200);
    		wnd.add(mc);
    			
    		wnd.display();
    	}
    }
    
    @SuppressWarnings("serial")
    class myCanvas extends JPanel {
    	public myCanvas(int sizeX, int sizeY) {
    		setPreferredSize(new Dimension(sizeX, sizeY));
    	}
    	
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		Graphics2D g2d = (Graphics2D) g;
    		
    		g2d.setBackground(Color.RED);
    	}
    }
    So far, everying will run and compile, but the JPanel won't turn red at all. I also tried shoving in a "drawString()" method, and it still did nothing.

    Any help is really appreciated. (I'm sorry if this belongs in the 2D forum, but it seems more like and issue with Swing than actual drawing.)
    Last edited by CuddlyKittens11; 04-20-2011 at 08:03 PM.

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Java Code:
    mc = new myCanvas(200, 00);
    Are you sure you wish to set the preferred height to 0? Further, I recommend reading the following link: Code Conventions for the Java Programming Language

  3. #3
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Sorry, that was a typo.
    I tried it with the correct, 200x200, still didn't work.

  4. #4
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    You aren't painting anything, only setting the background of the graphics. If this is all you want, just set the background of the component. If you want to actually draw, using the Graphics drawing routines like drawRect (also see the API for graphics: setting the background color does nothing unless you call clearRect. For drawing, set the foreground color - see http://download.oracle.com/javase/6/...a.awt.Color%29 )
    Last edited by doWhile; 04-20-2011 at 09:06 PM.

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

    Default

    Never change the property of a component in the paintComponent() method. You might cause an infinite loop.

    The setBackground() should be invoked in the consctuctor. Also there is no need to do custom painting just to set the background color. I suggest you read the Swing tutorial on Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for working examples. And follow the structure in the tutorial for creating the classes. There is no need to extend JFrame just to set a couple of properties. You only extend a component when you add/change functionality of the component.

  6. #6
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    Quote Originally Posted by camickr View Post
    Never change the property of a component in the paintComponent() method. You might cause an infinite loop.

    The setBackground() should be invoked in the consctuctor. Also there is no need to do custom painting just to set the background color. I suggest you read the Swing tutorial on Lesson: Performing Custom Painting (The Java™ Tutorials > Creating a GUI With JFC/Swing) for working examples. And follow the structure in the tutorial for creating the classes. There is no need to extend JFrame just to set a couple of properties. You only extend a component when you add/change functionality of the component.
    Great advice. But just as a point of clarification, the original poster is setting the background of the Graphics2D parameter of paintComponent (not the component itself).

  7. #7
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

    Default

    Okay, I finally got the painting to work. Just one more quick question:
    Is there a method for just changing the color of one pixel on a JPanel? Ex, When I used to use DarkGDK, there was the "dbDot(int x, int y)" function that would change the color of one pixel.

    Thank you guys for your help.

  8. #8
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default

    draw a rect with height and width of 1, or draw a line of length 1

  9. #9
    Join Date
    Apr 2011
    Posts
    39
    Rep Power
    0

Similar Threads

  1. paintComponent problem
    By luke in forum New To Java
    Replies: 5
    Last Post: 04-02-2011, 03:07 PM
  2. Help with paintComponent!
    By joeyea in forum Java 2D
    Replies: 6
    Last Post: 12-27-2010, 02:59 PM
  3. Trouble with paintComponent()
    By ShirlyPunk in forum AWT / Swing
    Replies: 17
    Last Post: 03-13-2010, 05:27 PM
  4. Working around paintcomponent
    By sahhhm in forum New To Java
    Replies: 2
    Last Post: 05-16-2008, 03:43 AM
  5. paint() and paintComponent()
    By goldhouse in forum Java 2D
    Replies: 1
    Last Post: 07-17-2007, 04:43 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
  •