【问题标题】:Paint ArrayList of Shape Objects to Frame将形状对象的 ArrayList 绘制到框架
【发布时间】:2013-11-01 21:37:30
【问题描述】:

我在将形状(圆形和矩形)绘制到框架中时遇到了困难。

我创建了一个圆形和一个矩形类来实现形状接口。然后我创建了 DrawableCircle 和 DrawableRectangle 类,它们适当地扩展了 Circle 和 Rectangle 类,并实现了 Drawable 接口。

我现在正在尝试创建一个 ShapesDriver 类,它扩展了 Frame 并在其中包含 main 方法和 paint(Graphics g) 方法。在 ShapesDriver 中,我需要创建一个 Drawable 类型的 ArrayList。此 ArrayList 包含 DrawableCircle 和 DrawableRectangle 的实例。在 paint 方法中,我必须遍历 ArrayList 并为每个形状调用 draw 方法。

这就是我卡住的地方......

任何帮助将不胜感激!

形状界面

public interface Shape {

    public double area();

}

圈类

 public class Circle implements Shape{

        private int radius;
        private double area;

        public Circle(int r){
            radius = r;

        }

        @Override
        public double area() {

            area = Math.PI * (radius * radius);
            return area;
        }

}

矩形类

public class Rectangle implements Shape{

    double height;
    double width;
    double area;

    public Rectangle(double h, double w){
        height = h;
        width = w;
    }

    @Override
    public double area() {
        area = height * width;
        return area;
    }

}

可绘制的界面

import java.awt.Color;
import java.awt.Graphics;

public interface DrawableInterface {

    public void setColor(Color c);
    public void setPosition(int x, int y);
    public void draw(Graphics g);

}            

可画圆

import java.awt.Color;
import java.awt.Graphics;


public class DrawableCircle extends Circle implements DrawableInterface{

    private Color col;
    private int posX;
    private int posY;

    public DrawableCircle(int r){
        super(r);
    }

    @Override
    public void setColor(Color c) {
        col = c;
    }

    @Override
    public void setPosition(int x, int y) {
        posX = x;
        posY = y;
    }

    @Override
    public void draw(Graphics g) {

        g.setColor(col);
        g.drawOval(posX, posY, 15, 15);
    }



}

可绘制矩形

import java.awt.Color;
import java.awt.Graphics;


public class DrawableRectangle extends Rectangle implements DrawableInterface{

    private Color col;
    private int posX;
    private int posY;

    public DrawableRectangle(double h, double w){
        super(h, w);
    }

    @Override
    public void setColor(Color c) {
        col = c;
    }

    @Override
    public void setPosition(int x, int y) {
        posX = x;
        posY = y;
    }

    @Override
    public void draw(Graphics g) {

    g.setColor(col);
    g.drawRect(posX,posY,10,10);

    }

}

ShapesDriver

import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;


public class ShapesDriver extends Frame {

static ArrayList<DrawableInterface> shapesArr = new ArrayList<DrawableInterface>();

    public ShapesDriver() {
        super("Shapes Object Array");
        setSize(400, 300);
        setLocation(200, 200);

        setVisible(true);
    }

    public static void main(String[] args) {


        DrawableCircle c = new DrawableCircle(500);
        c.setColor(Color.GREEN);
        c.setPosition(25, 25);

        DrawableRectangle r = new DrawableRectangle(100, 50);
        r.setColor(Color.RED);
        r.setPosition(75, 75);

        shapesArr.add(c);
        shapesArr.add(r);


 ShapesDriver shapeFrame = new ShapesDriver();

         shapeFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                System.exit(0);
            }
        });

    }

    @Override
    public void paint(Graphics g) {

        for (DrawableInterface s : shapesArr) {
            super.paint(g);

            s.draw(g);
        }
    }
}

【问题讨论】:

    标签: java interface arraylist drawable draw


    【解决方案1】:

    "This is where I am stuck..." -- 你到底卡在哪里了?

    我自己,我会:

    • 使用 Swing GUI 而不是 AWT GUI,因为 Swing 比 AWT 更强大、更灵活。几乎不需要创建 AWT GUI。
    • 您的 GUI 类 ShapesDriver 什么都不做。它扩展了 Frame,但您永远不会创建 ShapesDriver 的实例。相反,它有一个 main 方法,您可以在其中创建一个单独的 Frame。在我的 GUI 代码中,我将创建一个真正的 GUI 类,它具有实例字段和方法,并且一定会在某处创建此类的实例。
    • 在我的 GUI 类中,我将拥有 Shape 的 ArrayList 和绘图方法,并在绘图方法中循环遍历 ArrayList,在循环时绘制每个形状。
    • 由于我更喜欢​​使用 Swing,我的 GUI 类将扩展 JPanel,而我的绘图方法将是 paintComponent(Graphics g) 方法。如果您需要使用 AWT,那么您可以改用 Panel 并在其 paint(Graphics g) 方法中进行绘制。
    • 然后我将有一个 main 方法来创建 GUI,使其可见,并且什么都不做。

    【讨论】:

    • 感谢您的回复!是的,鉴于我给出的规范,看起来我被困在使用框架而不是 JFrame 上。我对 JFrames 有一些经验,而对 awt Frames 没有经验。
    • 我不确定paint方法需要做什么,也不确定需要采取哪些步骤来遍历ArrayList绘制其中包含的对象?
    • @user2904978:我尽量避免用勺子喂食,所以让我们看看你的最佳尝试。
    • 所以不是Frame shapeFrame = new Frame();我应该改用 ShapesDriver shapeFrame = new ShapesDriver();
    • @user2904978:是的,您的主要方法需要这样做,以创建和显示 ShapesDriver 的实例。
    猜你喜欢
    • 2016-05-05
    • 1970-01-01
    • 1970-01-01
    • 2015-06-19
    • 2012-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多