Results 1 to 3 of 3
  1. #1
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Can't paint on panel

    Hey guys, for some reason it seems like the method: paintComponent is not getting called..
    I know this because I put a System.out.println there to see this.
    I've heard online that it might be due to not initializing the layout? But had no luck with implementing that..
    Here's my code, it's going to be a draw program BTW and it's not homework of any kind.

    I initialized the values of x y w h to be 20 just to see if it's working,
    The idea is to select the position, size, shape and color and press draw.
    Then a shape should appear.

    Java Code:
    package MyPaint;
    
    import java.awt.Color;
    import java.awt.FlowLayout;
    import java.awt.Graphics;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseMotionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    
    public class MyPaint extends JPanel implements ActionListener{
    	private int x1=20;int y1=20;int w=20;int h=20;
    	private int choice=0;
    	
    	private JPanel p;
    	
    	private JButton rect;
    	private JButton circ;
    	private JButton redCol;
    	private JButton blueCol;
    	private JButton greenCol;
    	private JButton draw;
    	
    	private JTextField wSizeIn;
    	private JTextField hSizeIn;
    	private JTextField xIn;
    	private JTextField yIn;
    	
    	private Color color;
    	
    	public MyPaint(){
    		p = new JPanel();
    		
    		rect = new JButton("rectangle");
    		circ = new JButton("circle");
    		redCol = new JButton("red");
    		blueCol = new JButton("blue");
    		greenCol = new JButton("green");
    		draw = new JButton("Draw!");
    		
    		wSizeIn = new JTextField("0",10);
    		hSizeIn = new JTextField("0",10);
    		
    		xIn = new JTextField("0",10);
    		yIn = new JTextField("0",10);
    		
    		gui();
    	}
    	
    	public void gui(){
    		JFrame f = new JFrame("My Paint");
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(1000, 600);
    		f.setVisible(true);
    		//adding action listener to objects
    		rect.addActionListener(this);circ.addActionListener(this);redCol.addActionListener(this);blueCol.addActionListener(this);greenCol.addActionListener(this);
    		draw.addActionListener(this);
    		//adding them to the panel
    		p.add(xIn);p.add(yIn);p.add(wSizeIn);p.add(hSizeIn);p.add(rect);p.add(circ);p.add(redCol);p.add(blueCol);p.add(greenCol);p.add(draw);
    		f.setLayout(new FlowLayout(FlowLayout.RIGHT)); 
    		//adding the panel to the frame
    		
    		f.add(p);
    		
    	}
    	
    	public void paintComponent(Graphics g){
    		super.paintComponent(g);
    		//testing
    		System.out.println("paint");
    		x1 = Integer.parseInt(xIn.getText());
    		y1 = Integer.parseInt(yIn.getText());
    		w = Integer.parseInt(wSizeIn.getText());
    		h = Integer.parseInt(hSizeIn.getText());
    		
    		//set color g.setColor(color);
    		switch (choice){
    		case 0:
    			g.fillRect(x1, y1, w, h);
    			break;
    		case 1:
    			g.fillOval(x1, y1, w, h);
    			break;
    		}
    		
    		//testing
    		System.out.println(x1+" "+y1+" "+w+" "+h);
    	}
    	/*
    	private class Listener extends MouseAdapter implements MouseMotionListener{
    		public void mousePressed(MouseEvent e){
    			x1 = e.getX();
    			y1 = e.getY();
    		}
    		public void mouseDragged(MouseEvent e){
    			x2 = e.getX();
    			y2 = e.getY();
    			repaint();
    		}
    	}
    	*/
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    		//checking which button was clicked
    		if(e.getSource()==rect){
    			choice = 0;
    		}else
    			if(e.getSource()==circ){
    				choice = 1;
    			}else
    				if(e.getSource()==redCol){
    					color = new Color(255,0,20);
    				}else
    					if(e.getSource()==greenCol){
    						color = new Color(30,255,50);
    					}else
    						if(e.getSource()==blueCol){
    							color = new Color(0,45,255);
    						}else
    							if(e.getSource()==draw){
    								if(x1>0 && y1>0 && w>0 && h>0 && color!=null){
    									repaint();
    								}
    							}
    		
    	}
    	
    	public static void main (String args[]){
    		
    		
    		MyPaint p = new MyPaint();
    		p.setVisible(true);
    		
    	}
    
    }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,189
    Rep Power
    12

    Default Re: Can't paint on panel

    The problem is that you are adding p to JFrame in line 69 which is wrong. The method paintComponent is overridden in MyPaint via
    extending JPanel. So line 69 should be f.add(this). And get rid of line 38 as you don't need it. This also means that the p qualifiers in line
    65 need to be removed (just use add without the p). Note that there my be other problems too but these were the obvious ones.

    Regards,
    Jim
    Last edited by jim829; 11-20-2017 at 05:01 AM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Yakg is offline Member
    Join Date
    Dec 2010
    Posts
    88
    Rep Power
    0

    Default Re: Can't paint on panel

    Quote Originally Posted by jim829 View Post
    The problem is that you are adding p to JFrame in line 69 which is wrong. The method paintComponent is overridden in MyPaint via
    extending JPanel. So line 69 should be f.add(this). And get rid of line 38 as you don't need it. This also means that the p qualifiers in line
    65 need to be removed (just use add without the p). Note that there my be other problems too but these were the obvious ones.

    Regards,
    Jim
    Very accurate and clear thanks :)
    No luck with showing the shapes on the panel, but it did make the paintcomponent to be called at least.
    Will sit on that later on, perhaps the shapes are hidden underneath the panel?.. Weird..
    Anyway i'll have a look thanks.

Similar Threads

  1. can't navigate from child panel to main panel
    By sharmapiyush in forum New To Java
    Replies: 76
    Last Post: 04-09-2014, 09:39 PM
  2. Replies: 1
    Last Post: 11-03-2013, 09:47 PM
  3. Paint--- trying to paint an oval
    By noobie_nate in forum Java 2D
    Replies: 2
    Last Post: 06-05-2013, 05:45 PM
  4. Paint is invalid type for variable paint.
    By minibronya in forum New To Java
    Replies: 3
    Last Post: 05-25-2012, 05:52 AM
  5. Replies: 10
    Last Post: 04-11-2011, 09:34 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
  •