[英]Java add rectangles to ArrayList and then draw them
我真的很需要你們的幫助。 我嘗試將矩形添加到ArrayList中,然后遍歷列表以繪制所有矩形。 我不確定是否使用了正確的方法,但是到目前為止,這是我的代碼。
編輯:代碼不會繪制我添加到ArrayList的矩形。 我什至不知道是否以正確的方式添加它們,或者以正確的方式通過for循環訪問它們。
在TestProgram中
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);
}
}
在Shapes類中:
import java.awt.Graphics;
import javax.swing.JPanel;
public abstract class Shapes extends JPanel {
abstract public void draw(Graphics g);
}
不要使Shapes
成為JPanel
。 還要取出其paintComponent
方法。
而是有一個JPanel
類,它是主繪圖表面。 將List<Shapes>
保留在類中。 遍歷該類的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);
}
}
}
然后只需將該面板添加到框架中即可。
請記住,所有繪畫都應通過傳遞到paintComponent
方法中的Graphics上下文完成。 我們不使用getGraphics
進行自定義繪畫。
另外,您可能需要在Shapes
類中存儲一些狀態,以便可以更改x / y / width / height狀態。 現在,您正在為所有形狀使用相同的硬編碼值。
abstract class Shapes {
int x, y;
public Shapes(int x, int y) {
this.x = x; this.y = y;
}
protected abstract void draw(Graphics g);
}
class RectangleShape extends Shape {
int width, height;
public RectangleShape(int x, int y, int width, int height) {
super(x, y);
this.width = width; this.height = height;
}
@Override
public void draw(Graphics g) {
g.fillRect(x, y, width, height);
}
}
這很不好,您要么想要將其添加為具有絕對定位的組件,要么想要繪制它,請選擇一個。
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;
}
}
2D圖形API定義了Shape
API,該API除其他外還包括Rectangle
類
例如...
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();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.