0

我正在尝试为我创建的每个 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 吗?

4

3 回答 3

2

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


编辑
您在编辑中声明:

我的错误现在在“g2.draw(s);”线上 并说“实际参数 Gladiator 无法通过方法调用转换转换为 Shape”。感谢您的帮助...如何将我的角斗士转换为形状?或者我应该以某种方式扩展 Shape 吗?

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

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

所以不是:

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!
    }
}
于 2013-06-04T03:11:26.390 回答
2

Drawable 很可能是您正在遵循的教程实现的接口,以允许多态性,用于将所有可绘制对象保存在一个集合中,以便渲染器类轻松访问和渲染(就像您在此处尝试做的那样)。

尝试查看您正在遵循的教程,很可能接口已在那里声明,如果没有,您可以自己声明。这是一个非常简单的示例,实现了您想要的接口:

public static interface Drawable {
    public void draw(Graphics2D g);
}

public static class BlueRectangle implements Drawable {

    private Rectangle rectangle;

    public BlueRectangle(int x, int y, int width, int height) {
        this.rectangle = new Rectangle(x, y, width, height);
    }

    @Override
    public void draw(Graphics2D g) {
        Color gColor = g.getColor();
        g.setColor(Color.blue);
        g.fill(rectangle);
        g.setColor(gColor);
    }

}

public static class RedCircle implements Drawable {

    private Ellipse2D.Double circle;

    public RedCircle(double x, double y, double width, double height) {
        this.circle = new Ellipse2D.Double(x, y, width, height);
    }

    @Override
    public void draw(Graphics2D g) {
        Color gColor = g.getColor();
        g.setColor(Color.red);
        g.fill(circle);
        g.setColor(gColor);
    }

}

// This allows you to for instance create collections of Drawables for
// rendering and a renderer class that doesn't care how each specific class
// implements rendering, it just uses its graphics object to render them

public static class Renderer extends JPanel {
    List<Drawable> drawables;
    private Random random = new Random();

    public Renderer() {
        this.drawables = new ArrayList<>();
        setUpDrawAbles();
        setBackground(Color.black);
    }

    @Override
    @Transient
    public Dimension getPreferredSize() {
        return new Dimension(1000, 1000);
    }

    private void setUpDrawAbles() {
        // Add some circles
        for (int i = 0; i < 5; i++) {
            Drawable circle = new RedCircle(random.nextDouble() * 1000,
                    random.nextDouble() * 1000, random.nextInt(200),
                    random.nextInt(200));
            drawables.add(circle);
        }

        // Add some rectangles;
        for (int i = 0; i < 5; i++) {
            Drawable rectangle = new BlueRectangle(random.nextInt(1000),
                    random.nextInt(1000), random.nextInt(200),
                    random.nextInt(200));
            drawables.add(rectangle);
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g.create();

        // This class doesn't know what a BlueRectangle or a RedCircle is,
        // it just tells them
        // "here is the graphicsobject to my canvas, use it to draw yourself"
        for (Drawable drawable : drawables) {
            drawable.draw(g2d);
        }
    }

}

public static void main(String[] args) {
    JFrame frame = new JFrame();
    Renderer render = new Renderer();
    frame.getContentPane().add(render);
    frame.pack();
    frame.setVisible(true);
}
}

编辑:是的,确实,您可能根本不需要它,因为您只有一个需要渲染的对象(角斗士),但是如果将来您决定实现许多具有通用渲染界面的视觉对象,将使生活更轻松对你来说,添加内容更容易和更快,当他们可以通过合同(界面)进行沟通时,它会减少游戏管理员之间的相互依赖关系,该合同告诉他们应该如何表现,而不是如何构建。

于 2013-06-04T03:31:32.393 回答
0

正如评论中所讨论的,由于您使用的是 Swing ,因此将JComponent用于图形。

于 2013-06-04T03:11:09.740 回答