java - 如何初始化Graphics g?

标签 java swing graphics awt

我想在吃 bean 人游戏结束后显示 GameOver 图像。但是我调用了 paintGameOverScreen(Graphics g) 然后我需要初始化 g。还有其他方法吗?

这是我的生活课

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;


public class Lives{

private int lives;


public Lives() {
    lives = 1;
}

public void removeLife() {

        lives--;
        if(lives==0){
            System.out.println("END GAME");
            paintGameOverScreen(g);
            System.exit(0);
        }
}

public void paintGameOverScreen(Graphics g) {


            ImageIcon i = new ImageIcon("src\image");
            Image image = i.getImage();
            int x = 0;
            int y = 0;
            g.drawImage(image, x, y, 100,100,null);
}


public void paint(Graphics g) {
    g.setColor(Color.WHITE);
    g.fillRect(5*20, 25*20, 100, 30);
    g.setColor(Color.BLACK);
    String result = "Lives: " + lives;
    g.drawString(result, 6*20, 26*20);
}
}

最佳答案

你永远不会自己调用 paint()paintComponent(),你总是通过 repaint() 来进行设置适当的图形

只是为了说明@mKorbel 指的是什么:

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class Lives extends JPanel {
    private int lives;
    private ImageIcon gameOverImage;

    public Lives() {
        try {
            gameOverImage = new ImageIcon(new URL("http://imgup.motion-twin.com/dinorpg/0/f/77acf80b_989624.jpg"));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        lives = 5;
    }

    public void removeLife() {
        if (lives > 0) {
            lives--;
            System.out.println("Left lives: " + lives);
            repaint();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        if (lives > 0) {
            System.out.println("Still have " + lives + " lives");
            g.setColor(Color.WHITE);
            g.fillRect(5 * 20, 25 * 20, 100, 30);
            g.setColor(Color.BLACK);
            String result = "Lives: " + lives;
            g.drawString(result, 6 * 20, 26 * 20);
        } else if (gameOverImage != null) {
            System.out.println("Game over");
            int x = (getWidth() - gameOverImage.getIconWidth()) / 2;
            int y = (getHeight() - gameOverImage.getIconHeight()) / 2;
            g.drawImage(gameOverImage.getImage(), x, y, gameOverImage.getIconWidth(), gameOverImage.getIconHeight(), this);
        }
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(800, 600);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame(Lives.class.getSimpleName());
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                final Lives lives = new Lives();
                frame.add(lives);
                frame.pack();
                frame.setVisible(true);
                // Dummy timer that reduces the lives every second. For demo purposes only of course
                Timer t = new Timer(1000, new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        lives.removeLife();
                    }
                });
                t.start();
            }
        });
    }
}

关于java - 如何初始化Graphics g?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13497289/

相关文章:

java - 使用自定义时元素单击不起作用 "keyword driver framework"

java - 以不同的顺序从测试方法返回输入,然后将它们提供给方法

Java JPanel 闪烁

ios - 未调用 Swift 4 GLKViewController 更新方法

qt - 关于游戏引擎的 SDL + Qt + OpenGL 的一些想法

java - 在 Servlet 的 JSP 页面中显示 UTF-8 文本

java - 使用抽象类和接口(interface)的设计问题

Java JTextPane + JScrollPane : de/activate automatic scrolling

java - 更改 ContentPane 的背景颜色

java - 传递绘制的图形