我正在尝试为我创建的每个 Gladiator 对象进行自定义绘图,然后移动它们。我目前在“public class Gladiator implements Drawable{”的 Gladiator 类中遇到关于关键字 Drawable 的找不到符号错误。我很确定我正在导入我需要的东西(可能更多)......
public class Test2 extends JFrame {
private PaintPanel paintPanel;
public Test2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(800, 600));
paintPanel = new PaintPanel();
getContentPane().add(paintPanel, BorderLayout.CENTER);
pack();
paintPanel.initGame();
}
class PaintPanel extends JPanel implements ActionListener {
private List<Gladiator> gladiators;
private Timer timer;
public void initGame() {
timer = new Timer(50, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
System.out.println("Refreshing ");
}
public PaintPanel(){
super();
gladiators = new ArrayList<Gladiator>();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Gladiator s : gladiators){
g2.draw(s);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Test2 gamePanel = new Test2();
gamePanel.setVisible(true);
}
});
}
}
角斗士等级:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Gladiator implements Drawable {
int minreach = 60;
int maxreach = 100;
int z = maxreach * 2;
int n = minreach * 2;
int[] location = new int[] {25,25};
public void Draw(Graphics g){
g.setColor(Color.green);
g.fillArc(location[0], location[1], z, z, 45, 90);
g.setColor(Color.red);
g.fillArc((location[0]+(z-n)/2),(location[1]+(z-n)/2), n, n, 45, 90);
g.setColor(Color.black);
g.fillArc((location[0]+(z-30)/2),(location[1]+(z-30)/2), 30, 30, 0, 360);
}
}
为什么我会收到此错误?我需要能够把我的角斗士画到屏幕上。我的视觉表示(抽象?)和我的游戏对象是否需要单独的类,因为我读过的一些教程似乎暗示了这一点?
编辑:
我完全删除了drawable,因为我认为它只是java语法的一部分,我什至可能不需要它。这是代码:
public class Test2 extends JFrame {
private PaintPanel paintPanel;
public Test2() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setMinimumSize(new Dimension(800, 600));
paintPanel = new PaintPanel();
getContentPane().add(paintPanel, BorderLayout.CENTER);
pack();
paintPanel.initGame();
}
class PaintPanel extends JPanel implements ActionListener {
private List<Gladiator> gladiators;
private Timer timer;
public void initGame() {
timer = new Timer(50, this);
timer.start();
}
@Override
public void actionPerformed(ActionEvent e) {
repaint();
System.out.println("Refreshing ");
}
public PaintPanel(){
super();
gladiators = new ArrayList<Gladiator>();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Gladiator s : gladiators){
g2.draw(s);
}
}
}
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Test2 gamePanel = new Test2();
gamePanel.setVisible(true);
}
});
}
}
角斗士等级:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Shape;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
public class Gladiator {
int minreach = 60;
int maxreach = 100;
int z = maxreach * 2;
int n = minreach * 2;
int[] location = new int[] {25,25};
public void Draw(Graphics g){
g.setColor(Color.green);
g.fillArc(location[0], location[1], z, z, 45, 90);
g.setColor(Color.red);
g.fillArc((location[0]+(z-n)/2),(location[1]+(z-n)/2), n, n, 45, 90);
g.setColor(Color.black);
g.fillArc((location[0]+(z-30)/2),(location[1]+(z-30)/2), 30, 30, 0, 360);
}
}
我的错误现在在“g2.draw(s);”线上 并说“实际参数 Gladiator 无法通过方法调用转换转换为 Shape”。感谢您的帮助...如何将我的角斗士转换为形状?或者我应该以某种方式扩展 Shape 吗?