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

Java将矩形添加到ArrayList,然后绘制它们,java,swing,arraylist,Java,Swing,Arraylist,我真的需要你们的帮助。 我尝试将矩形添加到ArrayList中,然后在列表中循环绘制所有矩形。我不确定我是否使用了正确的方法,但这是我到目前为止的代码 编辑:代码没有绘制我添加到ArrayList中的矩形。我甚至不知道它们是以正确的方式添加的,还是通过for循环以正确的方式访问的 在测试程序中 import java.awt.BorderLayout; import java.awt.Graphics; import java.util.ArrayList; import javax.swing

我真的需要你们的帮助。 我尝试将矩形添加到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");
    }
}
类内形状:

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

public abstract class Shapes extends JPanel {

    abstract public void draw(Graphics g); 

}不要做
形状
a
JPanel
。同时取出其
paintComponent
方法

取而代之的是一个
JPanel
类,它是主绘图面。将
列表
保留在课堂中。迭代该类的
paintComponent
方法中的列表,调用每个
Shapes
的draw方法

class DrawingPanel extends JPanel {
    List<Shapes> shapes;
    // add shapes

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Shapes shape: shapes) {
            shape.draw(g);
        }
    }
}

请参见a不要制作
形状
a
JPanel
。同时取出其
paintComponent
方法

取而代之的是一个
JPanel
类,它是主绘图面。将
列表
保留在课堂中。迭代该类的
paintComponent
方法中的列表,调用每个
Shapes
的draw方法

class DrawingPanel extends JPanel {
    List<Shapes> shapes;
    // add shapes

    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        for (Shapes shape: shapes) {
            shape.draw(g);
        }
    }
}

请参见a

这很糟糕,您要么想将其添加为具有绝对定位的组件,要么想绘制它,请选择一个

this.add(rectangle, BorderLayout.CENTER); // Add as component of the panel.
list.add(rectangle);                      // Add as method of drawing on the panel.
后者将更好地工作,因为您正在绘图。如果需要拖放,请将其作为子对象添加,但将图形添加到子对象

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

拉丝 形状
这是不好的,您要么要将其添加为具有绝对定位的组件,要么要绘制它,请选择一个

this.add(rectangle, BorderLayout.CENTER); // Add as component of the panel.
list.add(rectangle);                      // Add as method of drawing on the panel.
后者将更好地工作,因为您正在绘图。如果需要拖放,请将其作为子对象添加,但将图形添加到子对象

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

拉丝 形状
首先看一下

2D图形API定义了
形状
API,其中包括
矩形

例如

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestRectangles {

    public static void main(String[] args) {
        new TestRectangles();
    }

    public TestRectangles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Rectangle> rectangles;

        public TestPane() {
            rectangles = new ArrayList<>(25);
            Random ran = new Random();

            for (int index = 0; index < 10; index++) {
                int x = ran.nextInt(200 - 10);
                int y = ran.nextInt(200 - 10);
                int width = ran.nextInt(200 - x);
                int height = ran.nextInt(200 - y);
                rectangles.add(new Rectangle(
                        x, 
                        y, 
                        width, 
                        height));
            }
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Rectangle rect : rectangles) {
                g2d.draw(rect);
            }
            g2d.dispose();
        }

    }

}

导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类测试矩形{
公共静态void main(字符串[]args){
新的test矩形();
}
公共测试矩形(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类TestPane扩展了JPanel{
私有列表矩形;
公共测试窗格(){
矩形=新阵列列表(25);
Random ran=新的Random();
对于(int-index=0;index<10;index++){
int x=ran.nextInt(200-10);
int y=ran.nextInt(200-10);
int width=ran.nextInt(200-x);
int height=ran.nextInt(200-y);
矩形。添加(新矩形)(
x,,
Y
宽度,
身高),;
}
}
@凌驾
公共维度getPreferredSize(){
返回新维度(200200);
}
@凌驾
受保护组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
用于(矩形:矩形){
g2d.draw(rect);
}
g2d.dispose();
}
}
}

首先看一下

2D图形API定义了
形状
API,其中包括
矩形

例如

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestRectangles {

    public static void main(String[] args) {
        new TestRectangles();
    }

    public TestRectangles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Rectangle> rectangles;

        public TestPane() {
            rectangles = new ArrayList<>(25);
            Random ran = new Random();

            for (int index = 0; index < 10; index++) {
                int x = ran.nextInt(200 - 10);
                int y = ran.nextInt(200 - 10);
                int width = ran.nextInt(200 - x);
                int height = ran.nextInt(200 - y);
                rectangles.add(new Rectangle(
                        x, 
                        y, 
                        width, 
                        height));
            }
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Rectangle rect : rectangles) {
                g2d.draw(rect);
            }
            g2d.dispose();
        }

    }

}

导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.util.ArrayList;
导入java.util.List;
导入java.util.Random;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公共类测试矩形{
公共静态void main(字符串[]args){
新的test矩形();
}
公共测试矩形(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(newtestpane());
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(真)
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;
    }
}
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestRectangles {

    public static void main(String[] args) {
        new TestRectangles();
    }

    public TestRectangles() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private List<Rectangle> rectangles;

        public TestPane() {
            rectangles = new ArrayList<>(25);
            Random ran = new Random();

            for (int index = 0; index < 10; index++) {
                int x = ran.nextInt(200 - 10);
                int y = ran.nextInt(200 - 10);
                int width = ran.nextInt(200 - x);
                int height = ran.nextInt(200 - y);
                rectangles.add(new Rectangle(
                        x, 
                        y, 
                        width, 
                        height));
            }
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            for (Rectangle rect : rectangles) {
                g2d.draw(rect);
            }
            g2d.dispose();
        }

    }

}