I am having problem with the coordinates not being exactly correct after the rotation as I change the values of the polygons, since many of my calculation use doubles and the Polygon
constructor only takes in int[]
. Is there a better way to go about this and achieve the same results?
Note that rotations of vertices into the “z-direction” (outside of the frame) are achieved by scaling; and that the coordinate axes for each letter has a relative position with respect to the coordinate axes of the frame.
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import javax.swing.*;
import java.lang.reflect.Array;
public class test extends JPanel implements ActionListener{
double[] p1x = {200,200,260,260,220,220,260,260,200};
//int[] p1y = {300,360,360,340,340,320,320,300,300};
double[] p1y = {240,300,300,280,280,260,260,240,240};
double[] p2x = {600,600,620,620,640,640,660,660,600};
double[] p2y = {500,520,520,560,560,520,520,500,500};
double[] p3x = {500,500,560,560,540,540,520,520,500};
double[] p3y = {400,460,460,400,400,440,440,400,400};
int delay = 500;
int dx=0;
int dy=5;
int steps = 120;
Polygon t;
Timer tim = new Timer(delay,this);
public void actionPerformed(ActionEvent event){
for (int i=0; i<Array.getLength(p2x);i++){
//p2x[i] = (int) (p2x[i]*Math.cos(Math.toRadians(1))- p2y[i]*Math.sin(Math.toRadians(1)));
//p2y[i] = (int) (p2x[i]*Math.sin(Math.toRadians(1))+ p2y[i]*Math.cos(Math.toRadians(1)));;
//p2x[i] -=10;
//p3x[i]-= 10;
if(steps>100){
p2y[i] -=10;
p1y[i] +=10;
p3x[i] -=10;
if(i==0){
p1y[i] += 6;
}
if(i==1){
p2y[i] -= 1.5;
System.out.println("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] -= 1.5;
p3x[i] -= 6;
}
if(i==3){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 6;
}
if(i==4){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 4.5;
}
if(i==5){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 4.5;
}
if(i==6){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 1.5;
}
if(i==7){
p1y[i] += 6;
p3x[i] -= 1.5;
}
if(i==8){
p1y[i] += 6;
}
}
else if((steps<=100) && (steps>80)){
p2y[i] +=10;
p1y[i] -=10;
p3x[i] +=10;
if(i==0){
p1y[i] -= 6;
}
if(i==1){
p2y[i] += 1.5;
System.out.println("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] += 1.5;
p3x[i] += 6;
}
if(i==3){
p2y[i] += 6;
p1y[i] -= 1.5;
p3x[i] += 6;
}
if(i==4){
p3x[i] += 4.5;
p2y[i] += 6;
p1y[i] -= 1.5;
//p3x[i] += 4.5;
}
if(i==5){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 4.5;
}
if(i==6){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 1.5;
}
if(i==7){
p1y[i] -= 6;
p3x[i] += 1.5;
}
if(i==8){
p1y[i] -= 6;
}
}
else if((steps<=80) && (steps>60)){
p2y[i] -=10;
p1y[i] +=10;
p3x[i] -=10;
if(i==0){
p1y[i] += 6;
}
if(i==1){
p2y[i] -= 1.5;
System.out.println("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] -= 1.5;
p3x[i] -= 6;
}
if(i==3){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 6;
}
if(i==4){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 4.5;
}
if(i==5){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 4.5;
}
if(i==6){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 1.5;
}
if(i==7){
p1y[i] += 6;
p3x[i] -= 1.5;
}
if(i==8){
p1y[i] += 6;
}
}
else if((steps<=60) && (steps>40)){
p2y[i] +=10;
p1y[i] -=10;
p3x[i] +=10;
if(i==0){
p1y[i] -= 6;
}
if(i==1){
p2y[i] += 1.5;
System.out.println("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] += 1.5;
p3x[i] += 6;
}
if(i==3){
p2y[i] += 6;
p1y[i] -= 1.5;
p3x[i] += 6;
}
if(i==4){
p3x[i] += 4.5;
p2y[i] += 6;
p1y[i] -= 1.5;
//p3x[i] += 4.5;
}
if(i==5){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 4.5;
}
if(i==6){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 1.5;
}
if(i==7){
p1y[i] -= 6;
p3x[i] += 1.5;
}
if(i==8){
p1y[i] -= 6;
}
}
else if((steps<=40) && (steps>20)){
p2y[i] -=10;
p1y[i] +=10;
p3x[i] -=10;
if(i==0){
p1y[i] += 6;
}
if(i==1){
p2y[i] -= 1.5;
System.out.println("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] -= 1.5;
p3x[i] -= 6;
}
if(i==3){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 6;
}
if(i==4){
p2y[i] -= 6;
p1y[i] += 1.5;
p3x[i] -= 4.5;
}
if(i==5){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 4.5;
}
if(i==6){
p2y[i] -= 1.5;
p1y[i] += 4.5;
p3x[i] -= 1.5;
}
if(i==7){
p1y[i] += 6;
p3x[i] -= 1.5;
}
if(i==8){
p1y[i] += 6;
}
}
else if((steps<=20) && (steps>0)){
p2y[i] +=10;
p1y[i] -=10;
p3x[i] +=10;
if(i==0){
p1y[i] -= 6;
}
if(i==1){
p2y[i] += 1.5;
System.out.pri开发者_StackOverflow中文版ntln("steps: " + steps +" "+ p2y[i]);
}
if(i==2){
p2y[i] += 1.5;
p3x[i] += 6;
}
if(i==3){
p2y[i] += 6;
p1y[i] -= 1.5;
p3x[i] += 6;
}
if(i==4){
p3x[i] += 4.5;
p2y[i] += 6;
p1y[i] -= 1.5;
//p3x[i] += 4.5;
}
if(i==5){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 4.5;
}
if(i==6){
p2y[i] += 1.5;
p1y[i] -= 4.5;
p3x[i] += 1.5;
}
if(i==7){
p1y[i] -= 6;
p3x[i] += 1.5;
}
if(i==8){
p1y[i] -= 6;
}
}
}
repaint();
if (--steps ==0) tim.stop();
}
public void paintComponent(Graphics g ) {
super.paintComponent(g);
this.setBackground(Color.white);
g.drawLine(400, 0,400, 800);
g.drawLine(0, 400, 800, 400);
int[] p2xintarray =new int[9];
int[] p2yintarray =new int[9];
int[] p1xintarray =new int[9];
int[] p1yintarray =new int[9];
int[] p3xintarray =new int[9];
int[] p3yintarray =new int[9];
for (int i=0; i<Array.getLength(p2x);i++){
int p2xint= (int)p2x[i];
p2xintarray[i]=p2xint;
int p2yint= (int)p2y[i];
p2yintarray[i]=p2yint;
int p1xint=(int)p1x[i];
p1xintarray[i]=p1xint;
int p1yint= (int)p1y[i];
p1yintarray[i]=p1yint;
int p3xint=(int)p3x[i];
p3xintarray[i]=p3xint;
int p3yint= (int)p3y[i];
p3yintarray[i]=p3yint;
}
Polygon t = new Polygon(p2xintarray, p2yintarray, 9);
g.drawPolygon(t);
//Polygon ti = new Polygon(p2xi, p2yi, 9);
//g.drawPolygon(ti);
Polygon u = new Polygon(p3xintarray, p3yintarray, 9);
g.drawPolygon(u);
Polygon l = new Polygon(p1xintarray, p1yintarray, 9);
g.drawPolygon(l);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Drawing line and a moving polygon");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
test sl = new test();
frame.getContentPane().add(sl);
frame.setSize(700,700);
frame.setVisible(true);
sl.tim.start();
}
}
In addition to refactoring and changing magic numbers, consider using a Path2D.Double object instead of a Polygon.
Use AffineTransform
to scale in the direction orthogonal to the desired axis, as mentioned in this answer to your question. Also, note that Polygon
has a translate()
method that would significantly simplify your code.
Addendum: Here's two more techniques that may enhance the illusion:
Alternately show and hide the chosen axis as the
Shape
changes direction at the limits of its travel. This can be accomplished by changing the order in which each is drawn.Invert the
Shape
as it changes direction by negating the scale in the orthogonal direction. For example, when theShape
is "behind" the x axis, usescale(1, -scaleFactor)
, which affects the vertical size.
精彩评论