简体   繁体   English

绘图时使用JPanel座标

[英]Using JPanel coordinates while drawing

I'm making simple game in Java and I'm using Swing. 我正在用Java开发简单的游戏,并且正在使用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. 我有JFrame,并且在其中想要两个JPanels-一个用于得分,依此类推,第二个用于实际游戏。 I read that every JPanel has its own coordinates, so point (0, 0) is on the upper-left corner of that panel. 我读到每个JPanel都有自己的坐标,因此点(0,0)在该面板的左上角。

I override method paintComponent() im my class GameView which displays the game (so it's the second JPanel from these I mentioned). 我在显示游戏的类GameView中重写了paintComponent()方法(因此是我提到的第二个JPanel)。 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. 但是,当我想在gameView的左上角绘制某些东西并将该图像的坐标设置为(0,0)时,它将在Ba​​rView上绘制。

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? 所以我的问题是,如何使用JPanel坐标而不是JFrame坐标绘制东西? Here's some code: 这是一些代码:

Adding objects extending JPanel to JFrame: 添加将JPanel扩展到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: 在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: 和gameScene中模型的方法绘制通常如下所示:

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);
}

It looks like you haven't specifed a LayoutManager for your frame , so it will default to BorderLayout . 看来您尚未为frame指定LayoutManager ,因此它将默认为BorderLayout

When you subsequently call frame.getContentPane().add(component) without passing in a position constant, the position BorderLayout.CENTER will be defaulted. 当您随后在不传递位置常量的情况下调用frame.getContentPane().add(component) ,将默认使用BorderLayout.CENTER位置。

The result is that your GameView and BarView components will be rendered on top of each other. 其结果是,你GameViewBarView组件将在彼此的顶部渲染。

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. 除非您的UI非常简单,否则您可能会发现需要使用其他布局管理器。 Refer to 'How to Use Various Layout Managers' for more on this subject. 有关此主题的更多信息,请参考“如何使用各种布局管理器”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM