简体   繁体   English

如何在另一个上绘制图像?

[英]How to draw an image over another?

So I am working on a project for a class. 所以我正在为一个班级做一个项目。 And I've been struggling with this issue for a while now. 我一直在努力解决这个问题。 The code below is a start screen, and when the enter key is pressed (when atTitle turns to false) I would like it to draw the next image. 下面的代码是一个开始屏幕,按下回车键(当atTitle变为false时),我希望它绘制下一张图像。 The problem with that is I can not think of a way for it to draw the next image when it turns to false. 这样做的问题是,当它变成false时,我想不出办法绘制下一张图像。 I've tried using ifs and whiles. 我试过使用ifs和whiles。 Mainly the problem is that you obviously put another public void paintComponent in an if statement. 主要的问题是,您显然在if语句中放置了另一个public void paintComponent And I can't carry the Graphics g variable to the KeyPressed method. 而且我无法将Graphics g变量携带到KeyPressed方法中。 I'm stuck. 我被卡住了。

public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D)g;
    AffineTransform at = new AffineTransform();
    g2.setTransform(at);
    if (atTitle == true) {
        g.drawImage(titlescreen, 0, 0, this);
        if (start_visible == true) {
            g.drawImage(start_symbol, -70, 30, this);
            jf.addKeyListener(this);
        }
    }
}

public void keyTyped(KeyEvent e) {}
public void keyReleased(KeyEvent e) {}

public void keyPressed(KeyEvent e) {
    //int keyCode = e.getKeyCode();
    if (atTitle == true) {
        if (e.getKeyCode() == KeyEvent.VK_ENTER) {
            atTitle = false;
            System.out.println("It Works.");
        }
    }
}

If you are using JPanel you should: 如果使用的是JPanel,则应:

panel.setFocusable(true);
panel.requestFocusInWindow();

Referenced here 这里引用

Also you can call panel.revalidate() and/or panel.repaint() in the key press. 您也可以在按键中调用panel.revalidate()和/或panel.repaint()。

Maybe try to stay away from JPanel and Swing altogether (not suited for gaming, but for forms) and just use the graphics2D functionality with a Window, Frame and Canvas, as suggested in previous comment. 也许尝试完全远离JPanel和Swing(不适合游戏,但适合表单),而仅将Graphics2D功能与Window,Frame和Canvas一起使用,如先前评论中所建议。

you need to call repaint and wait system calls to paint function. 您需要调用repaint并等待系统调用来paint函数。 you should not keep graphic object and use it in a function is not child function of paint 您不应该保留graphic对象并将其用于功能不是paint子功能

as a simple canvas component, you can use this one 作为一个简单的画布组件,您可以使用此组件

class Game extends Canvas implements Runnable {
        private Thread thread;
        public Game(){
            thread = new Thread(this);
            thread.start();
        }
        @Override
        public void paint(Graphics g) {
            // paint your game
        }
        public void stop(){
            thread = null;
        }
        @Override
        public void run() {
            while (thread == Thread.currentThread()){
                long ti = System.currentTimeMillis();
                repaint();
                long ti2 = System.currentTimeMillis();
                long waitTime = 60 - (ti2-ti);
                if (waitTime > 0){
                    try {
                        Thread.sleep(waitTime);
                    } catch (Exception e){

                    }

                }
            }
        }
    }

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

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