為什麼矩形的座標越轉怪? |
尚未結案
|
Onz
一般會員 發表:1 回覆:2 積分:0 註冊:2004-11-27 發送簡訊給我 |
我在練習座標轉換的簡單繪圖程式,讓矩形每按一次button就左或右旋轉45度,
可是debug不出來到底是哪邊造成矩形座標越轉越奇怪?請各位大大指導! package event02_2; import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.math.*;
public class home24 extends Frame
{
a p2 = new a();
JButton btn1 = new JButton("Turn left 45");
JButton btn2 = new JButton("Turn right 45");
static int array_x[] = {50,100,100,50};
static int array_y[] = {50,50,100,100};
public class a extends JPanel
{
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.red);
g.drawPolygon(array_x,array_y,4);
}
}
class listen implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
int temp_x[] = new int[4];
int temp_y[] = new int[4];
for(int x=0 ; x<4 ; x )
{
temp_x[x]=array_x[x]-100;
}
for(int x=0 ; x<4 ; x )
{
temp_y[x]=array_y[x]-100;
} if(e.getSource() == btn1)
{
for(int x=0;x<4;x )
{
array_x[x] = 100 (int)(temp_x[x] * (Math.cos(Math.PI/4)) temp_y[x] * (Math.sin(Math.PI/4)));
System.out.println(array_x[x]);
array_y[x] = 100 (int)(-temp_x[x] * (Math.cos(Math.PI/4)) temp_y[x] * (Math.sin(Math.PI/4)));
System.out.println(array_y[x]);
}
}
else if(e.getSource() == btn2)
{
for(int x=0;x<4;x )
{
array_x[x] = 100 (int)(temp_x[x] * (Math.cos(Math.PI/4)) - temp_y[x] * (Math.sin(Math.PI/4)));
System.out.println(array_x[x]);
array_y[x] = 100 (int)(temp_x[x] * (Math.cos(Math.PI/4)) temp_y[x] * (Math.sin(Math.PI/4)));
System.out.println(array_y[x]);
} }
p2.repaint();
}
}
public home24()
{
JFrame frm = new JFrame(); JPanel p1 = new JPanel();
p1.add(btn1);
p1.add(btn2); listen lis = new listen();
btn1.addActionListener(lis);
btn2.addActionListener(lis);
frm.getContentPane().setLayout(new BorderLayout());
frm.getContentPane().add(p1,new BorderLayout().NORTH);
frm.getContentPane().add(p2,new BorderLayout().CENTER);
frm.setSize(400,400);
frm.setLocation(100,100);
frm.setVisible(true);
}
public static void main ( String arg[] )
{
home24 mine = new home24();
} }
|
richtop
資深會員 發表:122 回覆:646 積分:468 註冊:2003-06-10 發送簡訊給我 |
Onz 您好: 您因為一直用旋轉後的座標,所以誤差一再累積,終致圖形變形。
不好意思,我修改了您的程式,您參考看看!
import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.math.*; public class home24 extends Frame { a p2 = new a(); JButton btn1 = new JButton("Turn left 45"); JButton btn2 = new JButton("Turn right 45"); static int array_x[] = {0,100,100,0}; static int array_y[] = {0,0,-100,-100}; int temp_x[] = new int[4]; int temp_y[] = new int[4]; static int dir=0; public class a extends JPanel { public void paint(Graphics g) { super.paint(g); g.setColor(Color.red); //g.drawPolygon(array_x,array_y,4); g.drawPolygon(temp_x, temp_y, 4); } } class listen implements ActionListener { public void actionPerformed(ActionEvent e) { if(e.getSource() == btn1) dir--; else if(e.getSource() == btn2) dir ; double angle = dir*Math.PI/4; for(int x=0;x<4;x ) { double tx,ty; tx = array_x[x]; ty = array_y[x]; temp_x[x] = 150 (int)(tx * (Math.cos(angle)) - ty * (Math.sin(angle))); System.out.println(temp_x[x]); temp_y[x] = 150 (int)(tx * (Math.sin(angle)) ty * (Math.cos(angle))); System.out.println(temp_y[x]); } p2.repaint(); } } public home24() { JFrame frm = new JFrame(); JPanel p1 = new JPanel(); p1.add(btn1); p1.add(btn2); listen lis = new listen(); btn1.addActionListener(lis); btn2.addActionListener(lis); frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Exit the program completely. for (int k=0; k<4; k ) { temp_x[k] = 150 array_x[k]; temp_y[k] = 150 array_y[k]; } frm.getContentPane().setLayout(new BorderLayout()); frm.getContentPane().add(p1,new BorderLayout().NORTH); frm.getContentPane().add(p2,new BorderLayout().CENTER); frm.setSize(400,400); frm.setLocation(100,100); frm.setVisible(true); } public static void main ( String arg[] ) { home24 mine = new home24(); } }RichTop 敬上 =====***** 把數學當工具,可以解決問題;將數學變能力,能夠發現並解決問題! =====##### 發表人 - richtop 於 2004/12/09 23:19:46 |
Onz
一般會員 發表:1 回覆:2 積分:0 註冊:2004-11-27 發送簡訊給我 |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |