Results 1 to 3 of 3
  1. #1
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default Drawing with JPanel

    Ok, wierd problem, I tried some really basic windowing without extending JPanel and JFrame:
    Java Code:
    import java.awt.*;
    import javax.swing.*;
    
    public class GraphTest {
    		public static void main(String[] args) {
    			JFrame frame = new JFrame("Test app");
    			JPanel panel = new JPanel();
    			initFrame(frame, panel);
    		}
    		
    		private static void initPanel(JPanel p) {
    			p.setBackground(Color.white);
    			p.setFocusable(true);
    			p.setPreferredSize(new Dimension(500,500));
    		}
    		
    		private static void drawSomething(JPanel p) {
    			Graphics g = p.getGraphics();
    			if(g == null) {
    				System.out.println("Can't get graphics");
    				return;
    			}
    			g.setColor(Color.black);
    			g.fillRect(200,200,50,100);
    			p.repaint();
    		}
    		
    		private static void initFrame(JFrame f, JPanel p) {
    			f.getContentPane().add(p);
    			initPanel(p);
    			f.pack();
    			p.requestFocus();
    			drawSomething(p);
    			f.setVisible(true);
    			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		}
    }
    All I get is a white window, without the black rectangle (drawn in drawSomething()). I got the Graphics context from JPanel, after drawing I called the repaint method, how come it doesn't show up?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,771
    Blog Entries
    7
    Rep Power
    21

    Default

    You have seen the behaviour of active drawing versus passive drawing; it's the AWT EDT (Event Dispatch Thread) that decides when (part of) a component needs to be drawn. All the way down the call chain the paintComponent(Graphics g) method is called when the component needs to be repainted. The JPanel implementation doesn't do much more than repaint its entire area given a background color.
    You have to extend that class and override the paintComponent( ... ) method to do some more interesting painting/drawing. This is passive drawing, the opposite of active drawing. So extend a JPanel class, override the paintComponent( ... ) method and you'll see the results.

    Your rectangle was visible for just a moment. Then the JPanel's default paintComponent( ... ) ruined the show.

    kind regards,

    Jos

  3. #3
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Ok, thanks a lot. I thought I could avoid extending my panel classes, now I know just do define a subclass of JPanel.

Similar Threads

  1. Drawing Graph and Add to JPanel
    By halo3 in forum NetBeans
    Replies: 17
    Last Post: 09-22-2009, 02:31 PM
  2. Looking for help on drawing stuff in a jPanel
    By Gatts79 in forum AWT / Swing
    Replies: 3
    Last Post: 08-28-2009, 07:00 PM
  3. drawing in JPanel ,beyond the co-ordinates
    By anandnarayan891 in forum AWT / Swing
    Replies: 4
    Last Post: 04-05-2009, 04:42 AM
  4. drawing to a JPanel
    By diggitydoggz in forum New To Java
    Replies: 11
    Last Post: 03-09-2009, 08:42 AM
  5. Drawing points on a JPanel
    By josephdcoleman in forum New To Java
    Replies: 6
    Last Post: 02-25-2009, 04:47 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
  •