【发布时间】:2016-05-13 17:14:08
【问题描述】:
我一直在尝试显示我的游戏的图形,但面板上没有显示任何图形。
以下是我的代码。主类调用其他两个类的绘制方法。
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Simulator extends JFrame implements KeyListener, Runnable, ActionListener {
private final int WIDTH, HEIGHT;
private Boolean right;
private int xMotion;
public Salt salt;
public Player playR;
Boolean running = false;
private Thread thread;
public static int score, highScore;
private int saltSpeed;
public Simulator(int width, int height) {
JPanel panel = new JPanel();
JFrame frame = new JFrame();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
frame.setSize(width, height);
panel.setBackground(Color.BLACK);
frame.add(panel);
playR = new Player();
this.HEIGHT = height;
this.WIDTH = width;
int xCordSalt = (int) (Math.random() * 631);
saltSpeed = 1;
salt = new Salt(saltSpeed);
right = true;
running = true;
}
public static void main(String[] args) {
Simulator game = new Simulator(640, 480);
game.start();
}
public void paintComponent(Graphics g)
{
salt.paint(g);
playR.paint(g);
}
public void start() {
running = true;
thread = new Thread(this);
thread.start();
repaint();
tick();
run();
}
public void stop() {
running = false;
System.exit(0);
}
@Override
public void keyPressed(KeyEvent e) {
if (e.getKeyCode() == KeyEvent.VK_D) {
right = true;
} else if (e.getKeyCode() == KeyEvent.VK_A) {
right = false;
}
}
public void tick() {
salt.tick(this, playR);
playR.tick();
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
if(e.getKeyCode() == KeyEvent.VK_D)
{
playR.setDirection(true);
}
else if(e.getKeyCode() == KeyEvent.VK_A)
{
playR.setDirection(false);
}
}
@Override
public void run() {
while (running) {
tick();
repaint();
try {
thread.sleep(7);
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void incrementScore() {
score++;
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
tick();
}
}
这里是方法 salt 的代码:
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.util.ArrayList;
public class Salt extends Rectangle{
private final int WIDTH = 10;
private final int HEIGHT = 10;
public int xCordSalt, yCordSalt;
private int speed;
Rectangle boundBox;
public Salt(int speedx)
{
xCordSalt = (int)Math.random()*641;
yCordSalt = 0;
speed = speedx;
boundBox = new Rectangle(xCordSalt, yCordSalt, WIDTH, HEIGHT);
boundBox.setBounds(xCordSalt, yCordSalt, WIDTH, HEIGHT);
}
public void tick(Simulator sim, Player playR)
{
boundBox.setBounds(xCordSalt, yCordSalt, WIDTH, HEIGHT);
if(yCordSalt >= 480)
{
//sim.stop();
}
else if(checkCollision(playR))
{
sim.incrementScore();
speed++;
yCordSalt = -speed;
}
yCordSalt = yCordSalt + speed;
}
public boolean checkCollision(Player playR)
{
if(this.getBoundBox().intersects(playR.getBoundBox()))
{
return true;
}
return false;
}
public void paint(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(xCordSalt, yCordSalt, WIDTH, HEIGHT);
}
public Rectangle getBoundBox()
{
return boundBox;
}
public double getSpeed()
{
return speed;
}
}
最后是方法 player,它使用 imageIcon 类来显示图像:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
public class Player extends JPanel {
private int xCord, yCord;
public Rectangle boundBox;
private static ImageIcon ryan;
boolean isRight;
public Player() {
ryan = new ImageIcon("E:\ryan.png");
xCord = 640/2;
yCord = 460;
boundBox = new Rectangle(xCord, yCord, 20, 20);
}
public static void main(String[] args) {
}
public void tick() {
}
public void setDirection(Boolean right)
{
if(right)
isRight = true;
else
isRight = false;
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D)g;
g2d.drawImage(ryan.getImage(), xCord, yCord, null);
}
public Rectangle getBoundBox()
{
return boundBox;
}
}
现在有些不完整,但我不明白为什么它没有显示任何图形。运行时,只出现一个黑框/面板。我在每个类的tick()方法、每个类的paint()方法和paintComponent()方法、start()方法中添加了一些打印语句。游戏将开始,运行每个类的tick 方法,但paintComponent() 或任何paint() 方法都会被调用!
【问题讨论】:
-
您需要将
"E:\ryan.png"更改为"E:\\ryan.png"。反斜杠 (\) 是转义字符;要在字符串中放置实际的反斜杠,您需要用另一个反斜杠转义反斜杠。 -
@ursinusTheStrong 哦,天哪,我怎么能忘记转义序列!谢谢你提醒我!
-
JFrame 没有paintComponent 方法,所以它永远不会被调用,这就是你应该使用@Override 的原因。创建从 JPanel 扩展的类,覆盖它的 paintComponent 方法并在那里执行您的自定义喘气,确保首先调用 super.paintComponent。一个想法是创建一个实体,它描述游戏中的某些元素,高做“东西”其中一个元素将是“可绘制的”,它有一些方法可以传递图形的引用并绘制自己,这个方式,您需要做的就是维护一个实体列表,更新并绘制它们
-
@MadProgrammer /@Override 给了我这个错误:gyazo.com/13aee2a2ac1ffa81669c38dfcb05def8 所以你是说我应该创建一个单独的类来处理图形?我有点困惑你说我应该做什么。
-
是的,我建议添加@Override 的原因是,当你“认为”你正在重写一个方法时,它会给你一个编译器警告,而你不是,这是一个很好的健全性检查。是的,我会为你所有的自定义绘画使用一个单独的类(我在这种情况下)
标签: java graphics graphics2d