Java 窗口 绘制图形 #2

写在前面:

  高考结束咧,爽到啊,好耶

  完善了Java 窗口 绘制图形 #1里面的程序

  加入了缩放平移功能,给代码加了注释

  1 package my_package;
  2 
  3 import java.awt.*;
  4 import java.awt.Graphics;
  5 import java.awt.event.MouseAdapter;
  6 import java.awt.event.MouseEvent;
  7 import java.awt.event.MouseMotionAdapter;
  8 import java.awt.event.MouseWheelEvent;
  9 import java.awt.event.MouseWheelListener;
 10 import java.io.*; 
 11 import javax.swing.JFrame;
 12 import javax.swing.JPanel;
 13 
 14 public class PonysAlgorithm extends JFrame
 15 {
 16     private static final long serialVersionUID = 1L;
 17     static int pony_winx=800,pony_winy=600;//窗口的长和宽
 18     static double c_Pi=3.1415926f;//常数π
 19     static int pony_scale=80;//窗口缩放变量
 20     static int pony_x0=pony_winx/2,pony_y0=pony_winy/2;//坐标原点
 21     static int pony_x0_tmp,pony_y0_tmp;//坐标原点随鼠标移动变化辅助变量
 22     static int pony_precision=1000;//绘制时的精度
 23     static int pony_framex_ori,pony_framey_ori;//鼠标移动辅助变量
 24     
 25     static double pony_x(double i){//i是参数,此处为x变量表达式
 26         return i;
 27     }
 28     static double pony_y(double i){//i是参数,此处为y变量表达式
 29         return (6*(2*Math.pow(i, 7)+Math.pow(i, 4)-i))/Math.pow((Math.pow(i , 6)+2*Math.pow(i, 3)+1), 2);
 30     }
 31     
 32     public static void main(String[] args) throws Exception
 33     {
 34         JFrame ponyFrame=new JFrame("Pony's Algorithm");
 35         ponyFrame.setLayout(null);
 36         JPanel ponyJPanel=new JPanel(){
 37             private static final long serialVersionUID = 1L;
 38 
 39             public void paint(Graphics g){
 40                 super.paint(g);
 41                 
 42                 g.setColor(Color.GRAY);
 43                 /*Coordinate Axis*/
 44                 g.drawLine(pony_x0, 0, pony_x0, pony_winy);//绘制y轴
 45                 g.drawLine(0, pony_y0, pony_winx, pony_y0);//绘制x轴
 46                 
 47                 for(int i=pony_x0+pony_scale;i<=pony_winx;i+=pony_scale)//绘制x、y轴刻度
 48                     g.drawLine(i, pony_y0-5, i, pony_y0+5);
 49                 for(int i=pony_x0-pony_scale;i>0;i-=pony_scale)
 50                     g.drawLine(i, pony_y0-5, i, pony_y0+5);
 51                 for(int i=pony_y0+pony_scale;i<=pony_winy;i+=pony_scale)
 52                     g.drawLine(pony_x0-5, i, pony_x0+5, i);
 53                 for(int i=pony_y0-pony_scale;i>0;i-=pony_scale)
 54                     g.drawLine(pony_x0-5, i, pony_x0+5, i);
 55                 
 56                 for(int i=1;pony_x0+i*pony_scale<pony_winx;++i)//绘制x、y轴刻度值
 57                     g.drawString(""+i, pony_x0+i*pony_scale-4, pony_y0+25);
 58                 for(int i=-1;pony_x0+i*pony_scale>0;--i)
 59                     g.drawString(""+i, pony_x0+i*pony_scale-4, pony_y0+25);
 60                 for(int i=1;pony_y0+i*pony_scale<pony_winy;++i)
 61                     g.drawString(""+(-i), pony_x0-25, pony_y0+i*pony_scale+4);
 62                 for(int i=-1;pony_y0+i*pony_scale>0;--i)
 63                     g.drawString(""+(-i), pony_x0-25, pony_y0+i*pony_scale+4);
 64                 
 65                 /*CORE*/
 66                 int tmpa,tmpb;
 67                 g.setColor(Color.BLUE);
 68                 for(int i=-100000;i<=100000;++i)//参数i,通过参数i的变化绘制出参数方程图像
 69                 {
 70                     tmpa=(int)(pony_x(i/(double)pony_precision)*pony_scale)+pony_x0;
 71                     tmpb=(int)(-pony_y(i/(double)pony_precision)*pony_scale)+pony_y0;if(tmpb==0) continue;
 72                     g.drawLine(tmpa, tmpb, tmpa, tmpb);
 73                 }
 74             }
 75         };
 76         
 77         /*Whole*/
 78         ponyFrame.setSize(pony_winx,pony_winy);
 79         ponyFrame.setResizable(false);
 80         ponyFrame.setLocationRelativeTo(null);
 81         ponyFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
 82         
 83         /*ICON*/
 84         File directory = new File("");
 85         ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\32.png"));
 86         
 87         /*DRAW*/
 88         ponyJPanel.setBounds(2, 2, pony_winx-10, pony_winy-10);
 89         ponyFrame.add(ponyJPanel);
 90         ponyFrame.addMouseListener(new MouseAdapter() {//当鼠标键按下并移动时,记录变量
 91             @Override
 92             public void mousePressed(MouseEvent e) {
 93                 pony_framex_ori=e.getXOnScreen();
 94                 pony_framey_ori=e.getYOnScreen();
 95                 pony_x0_tmp=pony_x0;
 96                 pony_y0_tmp=pony_y0;
 97             }
 98         });
 99         ponyFrame.addMouseMotionListener(new MouseMotionAdapter() {//坐标原点的改变
100             @Override
101             public void mouseDragged(MouseEvent e) {
102                 pony_x0=pony_x0_tmp+(e.getXOnScreen()-pony_framex_ori)/1;
103                 pony_y0=pony_y0_tmp+(e.getYOnScreen()-pony_framey_ori)/1;
104                 ponyJPanel.repaint();
105             }
106         });
107         ponyFrame.addMouseWheelListener(new MouseWheelListener() {//鼠标滚轮滚动时,对图像进行缩放
108             @Override
109             public void mouseWheelMoved(MouseWheelEvent e) {
110                     if(e.getWheelRotation()==1) {
111                         if(pony_scale>10) {
112                             pony_scale-=1;
113                             ponyJPanel.repaint();
114                         }
115                     }
116                     if(e.getWheelRotation()==-1) {
117                         pony_scale+=1;
118                         ponyJPanel.repaint();
119                     }
120             }
121         });
122         
123         ponyFrame.setVisible(true);
124         return;
125     }
126 }

 

附件:

*jar所在目录*\\icons\\

32.png

 

 

posted @ 2020-11-10 13:41  Antigonae  阅读(182)  评论(0编辑  收藏  举报