Results 1 to 2 of 2
  1. #1
    typedef is offline Member
    Join Date
    Feb 2014
    Posts
    52
    Rep Power
    0

    Default Painting a JComponent onto a JPanel

    Hello, this is kind of a follow up from my previous question but is different enough to deserve its own thread. I am attempting to make some Java2D shapes by wrapping them in subclasses of JComponent and giving them functionality. Unfortunately after defining paintComponent and adding it to the JPanel the shape is not showing up. As in my last thread the code is mostly irrelevant but the perhaps worth a skim. The only methods truely relevant are the paintComponent method in my shape and Sandbox constructor.

    Any suggestions would be greatly appreciated. Thank you for your time.
    Java Code:
    public class Sandbox extends JPanel
    {
    	private Timer timer;
    	private Circle c;
    	
    	public Sandbox()
    	{
    		c = new Circle();
    		add(c);
    		setPreferredSize(new Dimension(400, 300));
    		
    		setFocusable(true);
    		setBackground(Color.WHITE);
    		
    		addKeyListener(new KeyAdapter()
    			{
    				public void keyPressed(KeyEvent e)
    				{
    					c.keyPressed(e);
    				}
    				
    				public void keyReleased(KeyEvent e)
    				{
    					c.keyReleased(e);
    				}
    			});
    		
    		timer = new Timer(5, new ActionListener()
    			{
    				public void actionPerformed(ActionEvent e)
    				{
    					c.move();
    					c.repaint();
    				}
    			});
    		timer.start();
    	}
    	
    	public static void main(String[] args)
    	{
    		JFrame frame = new JFrame();
    		Sandbox s = new Sandbox();
    		frame.add(s);
    		
    		frame.setResizable(false);
    		frame.pack();
    		frame.setLocationRelativeTo(null);
    		frame.setTitle("Phy Sandbox");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setVisible(true);
    	}
    }
    Java Code:
    public class Circle extends JComponent implements Particle
    {
    	private int x;
    	private int y;
    	private int dx;
    	private int dy;
    	
    	private Color color;
    	
    	public Circle()
    	{
    		x = 40;
    		y = 40;
    		color = Color.BLACK;
    	}
    	
    	public void move()
    	{
    		if (x + dx >= 0 && x + dx <= 400 - 40)
    			x += dx;
    		
    		if (y + dy >= 0 && y + dy <= 300 - 40)
    			y += dy;
    	}
    	
    	public void keyPressed(KeyEvent e)
    	{
    		int key = e.getKeyCode();
    		
    		if (key == KeyEvent.VK_LEFT)
    			dx = -1;
    		
    		if (key == KeyEvent.VK_RIGHT)
    			dx = 1;
    		
    		if (key == KeyEvent.VK_UP)
    			dy = -1;
    		
    		if (key == KeyEvent.VK_DOWN)
    			dy = 1;
    	}
    	
    	public void keyReleased(KeyEvent e)
    	{
    		int key = e.getKeyCode();
    
    		if (key == KeyEvent.VK_LEFT)
    			dx = 0;
    		
    		if (key == KeyEvent.VK_RIGHT)
    			dx = 0;
    		
    		if (key == KeyEvent.VK_UP)
    			dy = 0;
    		
    		if (key == KeyEvent.VK_DOWN)
    			dy = 0;
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		super.paintComponent(g);
    		
    		g.setColor(color);
    		g.fillOval(x, y, 40, 40);
    	}
    }

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,649
    Rep Power
    5

    Default Re: Painting a JComponent onto a JPanel

    The problem could be a number of things. For example, each shape looks like it will have its own key listener. That may be a problem since you will need to first get the proper component's focus to move the object. But the big problem is that you are drawing the shape in its own component space. You want to draw it in the JPanel space. So you are using two different coordinate systems. So, does x,y represent where you want to draw it in the component or the JPanel? I would think you always want to draw the shape starting at 0,0 but then position the component appropriately in the JPanel.

    Having said that, I would not do it that way. I do not like moving components around for animation. I would simply have my own class of shapes, perhaps using Polygon or something from the java.awt.geom package. Then I would just draw them on the JPanel as appropriate. I would also recommend using an interface for the drawing operation. That way, you can store all the shapes in a list of type of that interface and iterate over them in your JPanel paintComponent method, passing in the Graphics context to the interface method.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Problem with painting a JPanel
    By Navarrano in forum New To Java
    Replies: 12
    Last Post: 04-12-2012, 09:47 PM
  2. painting to a JPanel in a JFrame
    By Parkournerd in forum AWT / Swing
    Replies: 1
    Last Post: 01-16-2012, 09:24 PM
  3. Problem painting on JPanel
    By Boatski in forum AWT / Swing
    Replies: 0
    Last Post: 12-01-2010, 04:36 AM
  4. JPanel not always painting everything
    By ekted in forum AWT / Swing
    Replies: 0
    Last Post: 11-26-2009, 11:24 AM
  5. Jpanel painting problem
    By kcakir in forum AWT / Swing
    Replies: 3
    Last Post: 04-15-2009, 10:21 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
  •