如何更改java.awt.Graphics绘制对象的方式?

时间:2013-05-19 06:42:40

标签: java graphics awt draw

我正在创建一个游戏,并有一个Player类,Enemy类和Bullet类。通过我的引擎设计,通过简单调用绘制我的图形是最合乎逻辑的     for (Shape s : shapes { g.fill(s) } 其中shapes是我游戏中每个对象的已存在List<Shapes>。这样做很好,但是我希望子弹的颜色与玩家不同,而玩家则来自敌人。如何修改我的Player,Bullet和Enemy类,以便awt.Graphics知道将它们绘制为哪种颜色?

编辑:另一个可接受的答案可能涉及找到一种方法对shapes进行排序并按类别进行分类,尽管这似乎与现有的完整列表相反。

4 个答案:

答案 0 :(得分:1)

您需要使用OOP 示例

 Interface Shapes{
  public Color getColor() ;
 }
 class Player implements Shapes{ 
     public Color getColor() {
        return Color.red;
     }
 }
 class Enemy implements Shapes{ 
     public Color getColor() {
        return Color.green;
     }
 }
 //  now the Paint

 for (Shape s : shapes} { g.fill(s.getColor())};

答案 1 :(得分:1)

如果您只想区分颜色,请在致电g.setColor(Color)

之前致电g.fill(s)

答案 2 :(得分:1)

您可以使用某种地图将每个形状与颜色相关联。然后,当你绘制每个形状时,你只需要做类似......的事情。

for (Shape shape : shapes) {
    Color color = mapColors.get(shape)(
    if (color != null) {
        g.setColor(color);
    } else{
        // set the color to a default value 
    }
    g.fill(shape);
}

对我来说,这种方法的问题在于它将形状与颜色分开。它还限制了资产的绘制方式

更好的解决方案是设计Asset类,提供简单的paint方法。允许每个资产定义它想要呈现的方式

public interface Asset {
    public void paint(Graphics g);
}

然后您只需使用List资产,类似于您的形状列表,但调用paint(g)(在Asset的实例上),允许资产决定如何它应该被涂上......

答案 3 :(得分:1)

这样的事情:

Colored Shapes

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.*;

class ColoredShapes {

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JPanel gui = new JPanel(new BorderLayout(5,5));

                final ArrayList<Drawable> drawables = new ArrayList<Drawable>();
                final PaintPanel paintPanel = new PaintPanel(drawables);
                gui.add(paintPanel, BorderLayout.CENTER);

                final Random r = new Random();

                JToolBar tools = new JToolBar();
                gui.add(tools, BorderLayout.PAGE_START);

                JButton addCircle = new JButton("Add Circle");
                ActionListener addCircleListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Point p = new Point(r.nextInt(400), r.nextInt(400));
                        int s = r.nextInt(50) + 50;
                        Color c = new Color(
                                r.nextInt(255), r.nextInt(255), r.nextInt(255));
                        drawables.add(new DrawableCircle(p, s, c));
                        paintPanel.repaint();
                    }
                };
                tools.add(addCircle);
                addCircle.addActionListener(addCircleListener);

                JButton addSquare = new JButton("Add Square");
                ActionListener addSquareListener = new ActionListener() {

                    @Override
                    public void actionPerformed(ActionEvent e) {
                        Point p = new Point(r.nextInt(400), r.nextInt(400));
                        int s = r.nextInt(50) + 50;
                        Color c = new Color(
                                r.nextInt(255), r.nextInt(255), r.nextInt(255));
                        drawables.add(new DrawableSquare(p, s, c));
                        paintPanel.repaint();
                    }
                };
                tools.add(addSquare);
                addSquare.addActionListener(addSquareListener);

                JOptionPane.showMessageDialog(null, gui);
            }
        };
        // Swing GUIs should be created and updated on the EDT
        // http://docs.oracle.com/javase/tutorial/uiswing/concurrency/initial.html
        SwingUtilities.invokeLater(r);
    }
}

interface Drawable {

    public abstract void draw(Graphics2D g);

    public abstract Color getColor();
}

class DrawableCircle implements Drawable {

    private Color color;
    private Shape shape;

    DrawableCircle(Point topLeft, int size, Color color) {
        shape = new Ellipse2D.Double(topLeft.x, topLeft.y, size, size);
        this.color = color;
    }

    public void draw(Graphics2D g) {
        g.setColor(getColor());
        g.fill(shape);
    }

    public Color getColor() {
        return color;
    }
}

class DrawableSquare implements Drawable {

    private Color color;
    private Shape shape;

    DrawableSquare(Point topLeft, int size, Color color) {
        shape = new Rectangle2D.Double(topLeft.x, topLeft.y, size, size);
        this.color = color;
    }

    public void draw(Graphics2D g) {
        g.setColor(getColor());
        g.fill(shape);
    }

    public Color getColor() {
        return color;
    }
}

class PaintPanel extends JPanel {

    Dimension preferredSize = new Dimension(400,400);
    ArrayList<Drawable> drawables;

    PaintPanel(ArrayList<Drawable> drawables) {
        this.drawables = drawables;
    }

    public Dimension getPreferredSize() {
        return preferredSize;
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
                RenderingHints.KEY_ANTIALIASING, 
                RenderingHints.VALUE_ANTIALIAS_ON);
        for (Drawable drawable : drawables) {
            drawable.draw(g2);
        }
    }
}