java 绘制窗体_Java 窗口 绘制图形 #3

1 packagemy_package;2

3 import java.awt.*;4 importjava.awt.event.MouseAdapter;5 importjava.awt.event.MouseEvent;6 importjava.awt.event.MouseMotionAdapter;7 importjava.awt.event.MouseWheelEvent;8 importjava.awt.event.MouseWheelListener;9 import java.io.*;10 importjavax.swing.JFrame;11 importjavax.swing.JPanel;12

13 public class PonysAlgorithm2 extendsJFrame14 {15 private static final long serialVersionUID = 1L;16 static int pony_winx=800,pony_winy=600;//窗口的长和宽

17 static int pony_scale=2,pony_scale_tmp=4;//视图缩放变量

18 static int theta1=0,theta2=0;//水平旋转角度,铅直旋转角度

19 static int pony_square=250;//绘制xy正方形区域趋边长的一半

20 static int pony_px=pony_winx/2,pony_py=pony_winy/2;//屏幕投影变量px和py

21

22 static int pony_framex_ori,pony_framey_ori;//鼠标移动辅助变量

23 static int pony_theta1_tmp,pony_theta2_tmp;//角度随鼠标移动变化辅助变量

24 static int pony_px_tmp,pony_py_tmp;//屏幕投影量随鼠标移动变化辅助变量

25 static int pony_project_x_tmp,pony_project_y_tmp;//屏幕投影量计算辅助变量

26 static float pony_precision=40.0f;//绘制精度,但绘制点数只受pony_square影响,不受精度影响

27

28 static double pony_project_x(double x, double y){//计算在屏幕上的投影px

29 return (y*Math.cos(theta1/180.0f)-x*Math.sin(theta1/180.0f))*pony_scale;30 }31 static double pony_project_y(double z, double x, double y){//计算在屏幕上的投影py

32 return z*pony_scale;33 }34 static double pony_z(double x, double y){//计算函数值

35 return x*x/4-y*y/9;36 }37

38 public static void main(String[] args) throwsException39 {40 JFrame ponyFrame=new JFrame("Pony's Algorithm II");41 ponyFrame.setLayout(null);42 JPanel ponyJPanel=newJPanel(){43 private static final long serialVersionUID = 1L;44 public voidpaint(Graphics g){45 super.paint(g);46 for(int y=-pony_square;y<=pony_square;++y)47 {48 g.setColor(Color.getHSBColor(0.54f+0.2f*(y+pony_square)/(pony_square*2.0f), 1.0f, 1.0f));49 for(int x=-pony_square;x<=pony_square;++x)50 {51 pony_project_x_tmp=pony_px+(int)pony_project_x(x/pony_precision,y/pony_precision);52 pony_project_y_tmp=pony_py+(int)pony_project_y(pony_z(x/pony_precision,y/pony_precision),x/pony_precision,y/pony_precision);53 g.drawLine(pony_project_x_tmp, pony_project_y_tmp, pony_project_x_tmp, pony_project_y_tmp);54 }55 }56 }57 };58

59 /*Whole*/

60 ponyFrame.setSize(pony_winx,pony_winy);61 ponyFrame.setResizable(false);62 ponyFrame.setLocationRelativeTo(null);63 ponyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);64

65 /*ICON*/

66 File directory = new File("");67 ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\2.png"));68

69 /*DRAW*/

70 ponyJPanel.setBounds(0, 0, pony_winx, pony_winy);71 ponyFrame.add(ponyJPanel);72

73 /*MOUSE*/

74 ponyFrame.addMouseListener(newMouseAdapter() {75 @Override76 public void mousePressed(MouseEvent e) {//当任意鼠标键按下时,记录变量

77 pony_framex_ori=e.getXOnScreen();78 pony_framey_ori=e.getYOnScreen();79 pony_theta1_tmp=theta1;80 pony_theta2_tmp=theta2;81 pony_px_tmp=pony_px;82 pony_py_tmp=pony_py;83 }84 });85 ponyFrame.addMouseMotionListener(new MouseMotionAdapter() {//当鼠标键按下并移动时,记录变量

86 @Override87 public void mouseDragged(MouseEvent e) {//两个角度的改变

88 if(e.isShiftDown() && e.getButton()==0){89 theta1=pony_theta1_tmp-(e.getXOnScreen()-pony_framex_ori)/2;90 theta2=pony_theta2_tmp+(e.getYOnScreen()-pony_framey_ori)/2;91 }92 else if(e.getButton()==0){//两个投影坐标的改变

93 pony_px=pony_px_tmp+(e.getXOnScreen()-pony_framex_ori)/1;94 pony_py=pony_py_tmp+(e.getYOnScreen()-pony_framey_ori)/1;95 }96 ponyJPanel.repaint();97 }98 });99 ponyFrame.addMouseWheelListener(new MouseWheelListener() {//鼠标滚轮滚动时,对图像进行缩放

100 @Override101 public voidmouseWheelMoved(MouseWheelEvent e) {102 if(e.getWheelRotation()==1) {103 if(pony_scale_tmp>2) {104 pony_scale_tmp-=1;105 pony_scale=pony_scale_tmp/2;106 ponyJPanel.repaint();107 }108 }109 if(e.getWheelRotation()==-1) {110 pony_scale_tmp+=1;111 pony_scale=pony_scale_tmp/2;112 ponyJPanel.repaint();113 }114 }115 });116

117 ponyFrame.setVisible(true);118 return;119 }120 }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值