lida
lida

Reputation: 163

Using JPanel coordinates while drawing

I'm making simple game in Java and I'm using Swing. I have JFrame and inside it I want to have two JPanels - one for score and so on and second below, for actual game. I read that every JPanel has its own coordinates, so point (0, 0) is on the upper-left corner of that panel.

I override method paintComponent() im my class GameView which displays the game (so it's the second JPanel from these I mentioned). But when I want to draw something in upper-left corner of gameView and set coordinates of that image to (0,0) it draws on BarView.

I read many tutorials and posts about drawing and I don't see what am I doing wrong. So my question is, how to draw something using JPanel coordinates, not JFrame ones? Here's some code:

Adding objects extending JPanel to JFrame:

GameView v = new GameView();
    BarView bv = new BarView();
    frame.getContentPane().add(bv);
    frame.getContentPane().add(v);
    frame.setVisible(true);
    v.requestFocus();
    v.repaint();
    bv.repaint();

Drawing in JPanel:

public class GameView extends JPanel implements View, Commons{

    public static final int WIDTH=WINDOW_WIDTH, HEIGHT=ARENA_HEIGHT;
    private GameScene gameScene;
    private TexturePaint paint;
    private BufferedImage bi;

    public GameView(){
    addKeyListener(new CustomKeyListener());
    addMouseMotionListener(new CustomMouseListener());
    setSize(WINDOW_WIDTH, ARENA_HEIGHT);
    setFocusable(true);
    try {
        bi = ImageIO.read(new File("src/res/texture.png"));
    } catch (IOException e) {
            // TODO Auto-generated catch block
        e.printStackTrace();
    }
     this.paint = new TexturePaint(bi, new Rectangle(0, 0, bi.getWidth(), bi.getHeight()));
    }

    @Override
    public void paintComponent(Graphics g1) {

    Graphics2D g = (Graphics2D) g1;
    g.setPaint(paint);
    g.fillRect(0, 0, WINDOW_WIDTH, ARENA_HEIGHT);
    for(Iterator<Drawable> it = gameScene.models.iterator(); it.hasNext();)
    {
        Drawable d = it.next();
        d.draw(g1);
    }
    Toolkit.getDefaultToolkit().sync();

    }

and method draw of model in gameScene usually looks like this:

public void draw(Graphics g1){
    Graphics2D g = (Graphics2D) g1.create();
    int cx = image.getWidth(null) / 2;
    int cy = image.getHeight(null) / 2;
    g.rotate(rotation, cx+x, cy+y);
    g.drawImage(image, x, y, null);
}

Upvotes: 0

Views: 907

Answers (1)

Jason Braucht
Jason Braucht

Reputation: 2368

It looks like you haven't specifed a LayoutManager for your frame, so it will default to BorderLayout.

When you subsequently call frame.getContentPane().add(component) without passing in a position constant, the position BorderLayout.CENTER will be defaulted.

The result is that your GameView and BarView components will be rendered on top of each other.

As a quick test, try specifying the component position as follows:

GameView v = new GameView();
BarView bv = new BarView();
frame.getContentPane().add(bv, BorderLayout.LINE_START);
frame.getContentPane().add(v, BorderLayout.LINE_END);

Unless your UI is really simple, you'll probably find that you need to use some other layout manager. Refer to 'How to Use Various Layout Managers' for more on this subject.

Upvotes: 3

Related Questions