Results 1 to 4 of 4
  1. #1
    Neotaxi is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default draw in nested JPanels

    Hi,
    I did a Jpanel nested in another one.
    Why?
    Because i did a very big map with a car moving in. To make a kind of zoom, I drew the map and the car(a simple circle) in a Jpanel nested in another one. The size of the nested Jpanel/map is 3000:3000 but you can only see 600:800 px of the map. When the car move, I recover the new location of the car and I move my nested JPanel in the parent JPanel to let the car in the center.

    I don't really know why but the car in the middle of the map can sometime advance by 2 pixel and just after (the tick timer after) go back by one pixel. It's a very creepy result.

    I have a very simple idea to resolve the problem. Make the car in the middle of the first Panel and make move the map behind (my car stay in the middle anyway). But I can't find the order of super.paintComponent(g); and repaint(g) to have a correct result.

    My code:
    Java Code:
    public class ContainerWorld extends JPanel implements ActionListener {
    	Timer timer;
            World map;
    	public ContainerWorld() {
    		timer = new Timer(50, this);
    		map = new World();
    		this.add(map);
                    map.setBounds(0,0,3000,3000);
                    map.setVisible(true);
                    map.setEnabled(true);
    	}
    	
    	public void actionPerformed(ActionEvent e) {
                    repaint();
    	}
    	
    	public void paintComponent(Graphics g) {
                
                    super.paintComponent(g);
                    g.setColor (Color.blue);
                    g.fillOval(300, 300, 1000, 1000);//I want this at the foreground, it's the "car"
    		
                    map.repaint();
                    
    	}
    	
    	
    }
    Java Code:
    public class World extends JPanel  {
    	public World() {
                    this.setBorder(new LineBorder(Color.BLACK, 2));
    	}
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
                    g.setColor (Color.BLACK);
                    Map map;
                    map = roverserveur.RoverServeur.Map;
                    
                    //obstacle
                    for (Rectangle r: map.getRectangles()){
                        g.fillRect(r.getUp_left().x, r.getUp_left().y, r.getSize().x, r.getSize().y);
                    }
                    for (Circle c: map.getCircles()){
                        g.fillOval(c.getCenter().x-c.getSize(), c.getCenter().y-c.getSize(), c.getSize()*2, c.getSize()*2);
                    }
                    
                    
    	}
    	
    	
    }
    Thank's for your help! :)

    PS:If my english is too bad and you don't understand the problem, you can tell me too! :)
    Last edited by Neotaxi; 01-20-2013 at 10:15 AM.

  2. #2
    Neotaxi is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: draw in nested JPanels

    I still have not found the solution. Some details:
    I made the circle (the car) 1000 px just to test.(g.fillOval(300, 300, 1000, 1000);)
    And when the nested JPanel moves completely to one side, I can see my 1000 px circle behind the nested JPanel. I would like to see this circle circle always at the forefront.
    Thank's for your help.

  3. #3
    Neotaxi is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: draw in nested JPanels

    I still have the problems... :)
    If my explanation is not clear please just said to me! :)

  4. #4
    Neotaxi is offline Member
    Join Date
    Jan 2013
    Posts
    4
    Rep Power
    0

    Default Re: draw in nested JPanels

    Hep! Still have this problem, are there no issue?

Similar Threads

  1. JPanels ?
    By santa in forum New To Java
    Replies: 5
    Last Post: 02-25-2012, 08:10 PM
  2. Replies: 2
    Last Post: 04-26-2011, 03:50 PM
  3. Replies: 20
    Last Post: 03-22-2011, 05:02 AM
  4. Problems regarding JPanels in JPanels
    By ColtonPhillips in forum AWT / Swing
    Replies: 2
    Last Post: 07-19-2010, 09:33 PM
  5. HELP with jPanels
    By maverik_vz in forum AWT / Swing
    Replies: 1
    Last Post: 03-12-2009, 12:46 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
  •