gpt4 book ai didi

java - 找不到符号: implements Drawable

转载 作者:行者123 更新时间:2023-12-01 18:47:54 27 4
gpt4 key购买 nike

我正在尝试为我创建的每个角斗士对象进行自定义绘图,然后移动它们。我目前在有关关键字 Drawable 的“public class Gladiator Implements Drawable{”上的 Gladiator 类中遇到找不到符号错误。我很确定我正在导入我需要的东西(可能更多)...

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 ?

最佳答案

任何属于核心 Java 一部分的“事物”都可以在 Java API 中找到。如果你查找 API,你会发现 Drawable 不存在。当您遇到此类错误时,API 应该是您首先查看的地方。

<小时/>

编辑
您在编辑中声明:

My error now is on line "g2.draw(s);" and says "actual argument Gladiator cannot be converted to Shape by method invocation conversion". Thanks for any help... how to convert my Gladiator to a shape? Or should I be extending Shape somehow?

再次查看 API,这次是 Graphics2D 类。如果您使用其 draw(...) 方法,则必须遵循其规则,API 声明需要一个 Shape。您可以让 Gladiator 实现 Shape,但随后您需要实现许多方法。您还可以让它扩展从 Shape 派生的更具体的类之一。

或者也许您真正想要做的是调用传入 Graphics 对象的 Gladiator 对象上的绘制方法。

所以不是:

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Gladiator s : gladiators){
g2.draw(s);
}
}

而是

public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
for (Gladiator s : gladiators){
s.draw(g2); // note draw should *not* be capitalized!
}
}

关于java - 找不到符号: implements Drawable,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16909147/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com