我正在尝试创建一个程序,使得有一个正方形进入另一个正方形(稍微旋转),另一个正方形进入内部,依此类推。我设法创建了一个程序来做到这一点。但我希望程序可以制作几个这样的盒子。看看插图以获得更好的想法:
我的问题是,在绘制第一个框后,然后尝试绘制第二个框时,绘制线条的“光标”有点卡在最后一点,所以它从第一个方格的最后一个点绘制线条到第二个方块的第一点,像这样:
因为你可以看到里面的pointPanel构造函数我已经尝试清除列表,并重置计数器和i变量,但没有运气。我可以做任何修改,以便我没有这个问题吗?我已经尝试将clear() - 指令放在其他地方。但也许我错过了一些东西。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Art {
ArrayList<Point> points = new ArrayList<>();
int i =0;
public static void main(String[] args) {
new Art();
}
public Art() {
JFrame frame = new JFrame("Art");
frame.add(new pointPanel());
frame.pack();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public class pointPanel extends JPanel{
//Constructor
public pointPanel(){
points(0, 100);
//points.clear();
points(500, 200);
}
@Override
//Dimension
public Dimension getPreferredSize() {
return new Dimension(800, 600);
}
public void points(double x, double y){
double t=0.1; //constant
final int side = 100;
int counter=0;
while (counter<20){
//Init the first points-->
Point p = new Point((int)(x), (int) (y) );
Point p1 = new Point((int)(x+side), (int)(y) );
Point p2 = new Point((int)(x+side), (int) (y-side) );
Point p3 = new Point((int)(x), (int)(y-side) );
Point p4 = new Point((int)(x), (int)(y) );
//-->and adding them to the list
if (counter == 0) {
points.add(p);
points.add(p1);
points.add(p2);
points.add(p3);
points.add(p4);
}
//Dynamic part:
//If the method has been run earlier - place points making it possible to draw lines
if (counter>0){
x=(1-t)*points.get(i).x + t * points.get(i+1).x;
y=(1-t)*points.get(i).y + t * points.get(i+1).y;
points.add( new Point( (int) x, (int) y) );
i++;
}
if (counter>0){
x=(1-t)*points.get(i).x + t * points.get(i+1).x;
y=(1-t)*points.get(i).y + t * points.get(i+1).y;
points.add( new Point( (int) x, (int) y) );
i++;
}
if (counter>0){
x=(1-t)*points.get(i).x + t * points.get(i+1).x;
y=(1-t)*points.get(i).y + t * points.get(i+1).y;
points.add( new Point( (int) x, (int) y) );
i++;
}
if (counter>0){
x=(1-t)*points.get(i).x + t * points.get(i+1).x;
y=(1-t)*points.get(i).y + t * points.get(i+1).y;
points.add( new Point( (int) x, (int) y) );
i++;
}
if (counter>0){
x=(1-t)*points.get(i).x + t * points.get(i-3).x;
y=(1-t)*points.get(i).y + t * points.get(i-3).y;
points.add( new Point( (int) x, (int) y) );
i++;
}
counter++;
}//while
//counter=0;
//i=0;
x=0;
y=0;
}//metode
//Paint-method
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.RED);
for (int i = 0; i < points.size()-1; i++) {
g2d.drawLine(points.get(i).x, points.get(i).y, points.get(i+1).x, points.get(i+1).y);
}
g2d.dispose();
}
}
}
答案 0 :(得分:1)
你必须分开你的方块的点,例如创建一个具有四个点的类Square和一个在Graphics2d中绘制该正方形的方法。你是如何做到的,无论它是否属于同一个方块,每一点都与它的继承者有关。
public class Square {
private final Point v1;
private final Point v2;
private final Point v3;
private final Point v4;
public Square(Point v1, Point v2, Point v3, Point v4) {
this.v1 = v1;
this.v2 = v2;
this.v3 = v3;
this.v4 = v4;
}
public void paint(Graphics2D g) {
g.setColor(Color.RED);
g.drawLine(v1.x, v1.y, v2.x, v2.y);
g.drawLine(v2.x, v2.y, v3.x, v3.y);
g.drawLine(v3.x, v3.y, v4.x, v4.y);
g.drawLine(v4.x, v4.y, v1.x, v1.y);
}
public void nextSquare() {
int x1=(1-t)*v1.x + t * v2.x;
int y1=(1-t)*v1.y + t * v2.y;
int x2=(1-t)*v2.x + t * v3.x;
int y2=(1-t)*v2.y + t * v3.y;
int x3=(1-t)*v3.x + t * v4.x;
int y3=(1-t)*v3.y + t * v4.y;
int x4=(1-t)*v4.x + t * v1.x;
int y4=(1-t)*v4.y + t * v1.y;
return new Square(
new Point(x1, y1),
new Point(x2, y2),
new Point(x3, y3),
new Point(x4, y4));
}
}
创建正方形:
List<Square> squares = new ArrayList<>();
Point p = new Point((int)(x), (int) (y) );
Point p1 = new Point((int)(x+side), (int)(y) );
Point p2 = new Point((int)(x+side), (int) (y-side) );
Point p3 = new Point((int)(x), (int)(y-side) );
Square s = new Square(p, p1, p2, p3);
squares.add(s);
for (int i = 1; i < 20; i++) {
s = s.nextSquare();
squares.add(s);
}
代码不是100%,因为我在我的手机上输入了这个...
Paint-method,假设square是一个实例变量:
//Paint-method
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
for (Square s : squares) {
s.paint(g2d);
}
g2d.dispose();
}