所以我一直在尝试做一个精灵的对象(称为心脏),可以移动周围的玩家在jpanel“战斗盒”。非常相似的代码在过去对我有用,但奇怪的是现在不行。我需要澄清的是,这是一个项目的一部分,不是全部。从程序的不同部分调用battlebox jpanel对象,尽管从技术上讲,错误可能不在这些类中
另外顺便说一句,并不是所有的代码都是为新的,因为我需要让它出现在第一个之前,我可以开始让它移动,这样的xd无论如何任何帮助的人可以给是感激的,只是以防万一,它不是显而易见的,我自学成才,很抱歉如果答案是超级明显
还有一点小提示,时钟也没有显示,但我已经有点接受命运与这一个了
战斗屏幕类
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
import MainMenu.MainMenu;
@SuppressWarnings("serial")
public class BattleScreen extends JPanel {
private Heart heart;
private Thread thread;
private Clock clock;
public BattleScreen() {
this.setBounds(0, 0, MainMenu.frame.getWidth(), MainMenu.frame.getHeight());
this.setBackground(Color.black);
clock = new Clock(this);
heart = new Heart();
heart.allowMovement(true);
MainMenu.con.add(this);
this.repaint();
this.setVisible(true);
thread = new Thread() {
public void run() {
repaint();
}
};
thread.start();
}
@Override
public void paintComponent (Graphics g) {
super.paintComponent(g);
heart.draw(g, this);
clock.draw(g, this);
}
}
心脏类
import java.awt.Component;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Heart {
int xPos, yPos, width, height;
Thread thread;
BufferedImage image;
public Heart() {
width = 44;
height = 44;
xPos = (MainMenu.MainMenu.con.getWidth() - width) / 2;
yPos = (MainMenu.MainMenu.con.getHeight() - height) / 2;
try {
image = ImageIO.read(new File("src/BattleScreen/heart.png"));
} catch (IOException e) {
e.printStackTrace();
}
this.setBackground(Color.BLUE);
this.repaint();
thread = new Thread() {
public void run() {
updateMovement();
}};
}
void draw (Graphics g, Component c) {
g.drawImage(image, xPos, yPos, width, height, c);
}
}
过去有效的课程:
旧战网
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
import MainMenu.MainMenu;
@SuppressWarnings("serial")
public class BattleScreen extends JPanel implements KeyListener {
private static JFrame frame = MainMenu.frame;
static Heart sprite = new Heart(frame.getWidth()/2, frame.getHeight()/2, 20, 20);
public BattleScreen() {
addKeyListener(this);
setFocusable(true);
setBounds(0, 0, frame.getWidth(), frame.getHeight());
setBackground(Color.black);
MainMenu.con.add(this);
new Thread(() -> {
while (true) {
sprite.updateMovement();
repaint();
try {
Thread.sleep(20);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
public void paintComponent (Graphics g) {
super.paintComponent(g);
sprite.draw(g, this);
}
}
旧心脏病
package DEADBattleScreen;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Image;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Heart {
Image img;
int xPos, yPos, width, height;
Thread thread;
Heart(int x, int y, int w, int h) {
this.xPos = x;
this.yPos = y;
this.width = w;
this.height = h;
try {
img = ImageIO.read(new File("src/battlescreen/heart.png"));
} catch (IOException e ) {
e.printStackTrace();
}
}
void draw (Graphics g, Component c) {
g.drawImage(img, xPos, yPos, width, height, c);
}
}
暂无答案!
目前还没有任何答案,快来回答吧!