Java将矩形添加到ArrayList然后绘制它们

标签 java swing arraylist

我真的需要你们的帮助。 我尝试将矩形添加到 ArrayList,然后循环遍历列表以绘制所有矩形。我不确定我是否使用了正确的方法,但这是迄今为止我的代码。

编辑:代码不会绘制我添加到 ArrayList 的矩形。我什至不知道它们是否以正确的方式添加,或者以正确的方式通过 for 循环访问。

在测试程序中

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.util.ArrayList;
import javax.swing.JFrame;


public class TestProgram extends JFrame {
    private ShapeRectangle rectangle;
    public ArrayList<Shapes> list = new ArrayList<Shapes>();


    public TestProgram(String title) {
        super(title);
        setLayout(new BorderLayout());
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        initComponents();
        setSize(500, 700);
        setVisible(true);
  }

    private void initComponents() {
        rectangle = new ShapeRectangle();    
        add(rectangle, BorderLayout.CENTER);

        list.add(rectangle);        
        Graphics g = getGraphics();
        for (int i = 0; i < list.size(); i++) {
            rectangle.draw(g);
        }
  }

    public static void main(String args[]) {
        new TestProgram("Drawing program");
    }
}

在 ShapeRectangles 类中:

import java.awt.Graphics;

public class ShapeRectangle extends Shapes {

    public ShapeRectangle() {}    

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponents(g);
        draw(g);        
    }

    @Override
    public void draw(Graphics g) {
       g.drawLine(20,20,60,60);
       g.drawLine(130,30,80,11);
       g.drawRect(200,30,20,140);        
   }
}

在形状类中:

import java.awt.Graphics;
import javax.swing.JPanel;

public abstract class Shapes extends JPanel {

    abstract public void draw(Graphics g); 

}

最佳答案

这很糟糕,你要么想把它添加为具有绝对定位的组件,要么想绘制它,选择一个。

this.add(rectangle, BorderLayout.CENTER); // Add as component of the panel.
list.add(rectangle);                      // Add as method of drawing on the panel.

后者效果会更好,因为你正在画画。如果需要拖放,请将其添加为子项,但将绘图添加到子项中。

更改矩形的坐标和大小后,您可以在 JFrame 上调用 repaint() 来更新图形。

绘制形状

import java.awt.BorderLayout;
import java.awt.Graphics;
import java.util.ArrayList;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class DrawShapes extends JFrame {
    public ArrayList<Shape> shapeList = new ArrayList<Shape>();

    public DrawShapes(String title) {
        super(title);

        this.setLayout(new BorderLayout());
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(500, 700);
        this.setLocationRelativeTo(null);

        this.initComponents();
    }

    private void initComponents() {
        shapeList.add(new RectangleShape(20, 20, 60, 60));
        shapeList.add(new RectangleShape(130, 30, 80, 11));
        shapeList.add(new RectangleShape(200, 30, 20, 140));
    }

    @Override
    public void paint(Graphics g) {
        for (Shape s : shapeList) {
            s.draw(g);
        }
    }

    public static void main(String args[]) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                DrawShapes d = new DrawShapes("Drawing program");
                d.setVisible(true);
            }
        });
    }
}

矩形形状

import java.awt.Graphics;

public class RectangleShape extends Shape {
    public RectangleShape(int x, int y, int width, int height) {
        super(x, y, width, height);
    }

    public RectangleShape() {
        super();
    }

    @Override
    public void draw(Graphics g) {
        g.drawRect(getX(), getY(), getWidth(), getHeight());
    }
}

形状

import java.awt.Graphics;

public abstract class Shape {
    private int x;
    private int y;
    private int width;
    private int height;

    public Shape() {
        this(0, 0, 1, 1);
    }

    public Shape(int x, int y, int width, int height) {
        this.x = x;
        this.y = y;
        this.width = width;
        this.height = height;
    }

    public abstract void draw(Graphics g);

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }
}

关于Java将矩形添加到ArrayList然后绘制它们,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26638620/

相关文章:

java - Android 和 Lambda

java - .java 使用未经检查且不安全的操作

java - 从数组列表中返回某个 'type'的对象

java - 如何在Java中创建缓存来存储用户 session

java - 对整数数组列表的数组列表进行排序

java - 当我使用 @IdClass LinkedHashSet 时包含重复的项目

java - 我们如何优化 CPU 密集型 Java 应用程序?

java - 定义常见的swagger注解

java - Struts 中的 JOptionPane?

java - jTextArea 垂直打印文本