找不到符号:实现Drawable

时间:2013-06-04 02:58:56

标签: java graphics 2d drawable symbols

我正在尝试为我创建的每个角斗士对象做一个自定义绘图,然后移动它们。我目前在“公共类Gladiator实现Drawable {”关于关键字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?

3 个答案:

答案 0 :(得分:2)

任何属于核心Java的“东西”都可以在Java API中找到。如果您查找API,您将看到Drawable不存在。当您遇到此类错误时,应该首先查看API。


修改
您在编辑中说明:

  

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

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

或者你 真正 试图做的就是在你的Gladiator对象上调用你的Draw方法传入Graphics对象。

所以不是:

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!
    }
}

答案 1 :(得分:2)

Drawable很可能是你正在实现的interface实现以允许多态,用于保持一个集合中的所有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);
}
}

编辑是的,你可能根本不需要它,因为你只有一个需要渲染的对象(角斗士),但如果将来你决定实现很多可视对象拥有一个通用的渲染界面将使你的生活变得更轻松,内容的添加更容易,更快,它可以减少游戏管理员之间的相互依赖性,当他们可以通过合同(界面)进行交流,告诉他们应该如何表现,而不是如何建成。

答案 2 :(得分:0)

正如评论中所讨论的,因为您使用的是Swing,所以请使用JComponent作为图形。