加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 如何初始化Graphics g?

发布时间:2020-12-14 06:00:05 所属栏目:Java 来源:网络整理
导读:我希望在生命结束后在pacman游戏中显示GameOver图像.但我调用paintGameOverScreen(Graphics g),然后我需要初始化g.有没有其他方法可以做到这一点? 这是我的Lives课程 import java.awt.Color;import java.awt.Graphics;import java.awt.Image;import java.aw
我希望在生命结束后在pacman游戏中显示GameOver图像.但我调用paintGameOverScreen(Graphics g),然后我需要初始化g.有没有其他方法可以做到这一点?

这是我的Lives课程

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("srcimage");
            Image image = i.getImage();
            int x = 0;
            int y = 0;
            g.drawImage(image,x,y,100,null);
}


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

解决方法

你永远不会自己调用paint()或paintComponent(),你总是通过repaint()来处理设置相应的Graphics

只是为了展示@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,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(),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();
            }
        });
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读