Java 窗口 绘制图形 #1
写在前面:
editplus换成eclipse了
Sketchpad要钱,买不起
自己搞(rua)
by emeralddarkness
建立了一个平面直角坐标系
两个变元x,y,参数i
实现了以下功能:
调节窗口大小
放缩单位长度
改变原点位置
改变参数方程
离超过Sketchpad还有十万八千里路要走呃呃呃
运行效果:
1 static double ponyX(double i){ 2 return 4*Math.sin(i); 3 } 4 static double ponyY(double i){ 5 return 6*Math.cos(i); 6 }
1 static double ponyX(double i){ 2 return 4/Math.cos(i); 3 } 4 static double ponyY(double i){ 5 return 6*Math.tan(i); 6 }
1 static double ponyX(double i){ 2 return i; 3 } 4 static double ponyY(double i){ 5 return i*i-i*4+2; 6 }
1 static double ponyX(double i){ 2 return i; 3 } 4 static double ponyY(double i){ 5 return i*4+5; 6 }
1 static double ponyX(double i){ 2 return 4*Math.sin(i)+2; 3 } 4 static double ponyY(double i){ 5 return 4*Math.cos(i)+3; 6 }
还能画出来各种奇奇怪怪的图形,比如说胡子
代码如下:
1 import java.awt.*; 2 import java.awt.event.*; 3 import java.awt.Graphics; 4 import java.io.*; 5 import java.util.*; 6 import javax.swing.ImageIcon; 7 import javax.swing.JFrame; 8 import javax.swing.JPanel; 9 10 public class PonysAlgorithm extends JFrame 11 { 12 static int winX=800,winY=600; 13 static int ponyIE=30; 14 static int zeroX=winX/2,zeroY=winY/2; 15 static int ponyPrecision=100; 16 17 static double ponyX(double i){ 18 return 4*Math.sin(i)+2; 19 } 20 static double ponyY(double i){ 21 return 4*Math.cos(i)+3; 22 } 23 24 public static void main(String[] args) throws Exception 25 { 26 JFrame ponyFrame=new JFrame("Pony's Algorithm"); 27 ponyFrame.setLayout(null); 28 JPanel ponyJPanel=new JPanel(){ 29 public void paint(Graphics g){ 30 super.paint(g); 31 32 g.setColor(Color.GRAY); 33 /*Coordinate Axis*/ 34 g.drawLine(zeroX, 0, zeroX, winY); 35 g.drawLine(0, zeroY, winX, zeroY); 36 37 for(int i=zeroX+ponyIE;i<=winX;i+=ponyIE) 38 g.drawLine(i, zeroY-5, i, zeroY+5); 39 for(int i=zeroX-ponyIE;i>0;i-=ponyIE) 40 g.drawLine(i, zeroY-5, i, zeroY+5); 41 for(int i=winY+ponyIE;i<=winY;i+=ponyIE) 42 g.drawLine(zeroX-5, i, zeroX+5, i); 43 for(int i=winY-ponyIE;i>0;i-=ponyIE) 44 g.drawLine(zeroX-5, i, zeroX+5, i); 45 46 for(int i=1;zeroX+i*ponyIE<winX;++i) 47 g.drawString(""+i, zeroX+i*ponyIE-4, zeroY+20); 48 for(int i=-1;zeroX+i*ponyIE>0;--i) 49 g.drawString(""+i, zeroX+i*ponyIE-4, zeroY+20); 50 for(int i=1;zeroY+i*ponyIE<winY;++i) 51 g.drawString(""+(-i), zeroX-25, zeroY+i*ponyIE+4); 52 for(int i=-1;zeroY+i*ponyIE>0;--i) 53 g.drawString(""+(-i), zeroX-25, zeroY+i*ponyIE+4); 54 55 g.setColor(Color.BLACK); 56 /*CORE*/ 57 int tmpa,tmpb; 58 for(int i=-10000;i<=10000;++i) 59 { 60 tmpa=(int)(ponyX(i/(double)ponyPrecision)*ponyIE)+zeroX; 61 tmpb=(int)(-ponyY(i/(double)ponyPrecision)*ponyIE)+zeroY; 62 g.drawLine(tmpa, tmpb, tmpa, tmpb); 63 } 64 } 65 }; 66 67 /*Whole*/ 68 ponyFrame.setSize(winX,winY); 69 ponyFrame.setResizable(false); 70 ponyFrame.setLocationRelativeTo(null); 71 ponyFrame.addWindowListener(new PonyWinAdapter()); 72 73 /*ICON*/ 74 File directory = new File(""); 75 ponyFrame.setIconImage(Toolkit.getDefaultToolkit().getImage(directory.getCanonicalPath()+"\\icons\\32.png")); 76 77 /*DRAW*/ 78 ponyJPanel.setBounds(2, 2, winX-10, winY-10); 79 ponyFrame.add(ponyJPanel); 80 81 82 ponyFrame.setVisible(true); 83 return; 84 } 85 } 86 87 class PonyWinAdapter extends WindowAdapter 88 { 89 public void windowClosing(WindowEvent windowEvent){ 90 System.exit(0); 91 return; 92 } 93 }
附件:
*jar所在目录*\\icons\\
32.png