?? o.java
字號:
import java.applet.*;import java.lang.*;import java.awt.*;import java.util.*;import java.awt.event.*;import java.awt.geom.*;import javax.swing.JColorChooser;/*class Point //定義點類{ int x,y,con,cho;Color col; //X,Y軸坐標,點的大小,自動或手動,點的顏色 Point(int x,int y,Color col,int con,int cho) { this.x=x;this.y=y;this.col=col;this.con=con;this.cho=cho; //構造函數初始化 } int dis(Point p) //求兩點之間的距離 { double a,b,c,d; a=p.x;b=this.x;c=p.y;d=this.y; a=Math.pow(a-b,2);c=Math.pow(c-d,2); d=(int)Math.sqrt(a+c); return (int)d; }}*/public class o extends Applet implements MouseMotionListener,ActionListener,ItemListener,MouseListener{ Panel pan1=new Panel();Panel pan2=new Panel(); //設置兩個面板 Choice choice1,choice2,choice3,choice4; Label label1,label2,label3,label4; int x=-1,y=-1,rubberanouce=0,clearanouce=0; Vector v1=null;Vector v2=null; //設置兩個向量,一個存用來存儲點對象,一個用來判斷 Color c=new Color(255,0,0,0); //設置一個顏色對象,初始化為紅色 int cho=0;int con=1;int xin=0;int r=0;int temp=0;int t=0; int n1=1;int n2=1; Button clear,rubber,tiao; public void init() //初始化程序 { rubber=new Button("橡皮擦"); clear=new Button("清除器"); tiao=new Button("調色板"); Label label1=new Label("選擇:"); Label label2=new Label("顏色:"); Label label3=new Label("粗細:"); Label label4=new Label("圖形:"); choice1=new Choice(); choice2=new Choice(); choice3=new Choice();choice4=new Choice(); v1=new Vector();v2=new Vector(); choice1.add("藍色");choice1.add("綠色");choice1.add("紅色");choice1.add("橙色"); choice2.add("細線");choice2.add("中線");choice2.add("粗線"); choice3.add("直線");choice3.add("圓形");choice3.add("矩形"); choice4.add("手動");choice4.add("自動"); // setLayout(new BorderLayout()); pan1.add(label1);pan1.add(choice4);pan1.add(label2);pan1.add(choice1);pan1.add(label3); pan1.add(choice2);pan1.add(label4);pan1.add(choice3);add(pan1,BorderLayout.CENTER); pan2.add(rubber);pan2.add(tiao);pan2.add(clear);add(pan2,BorderLayout.SOUTH); choice1.addItemListener(this); choice2.addItemListener(this); choice3.addItemListener(this); choice4.addItemListener(this); rubber.addActionListener(this); clear.addActionListener(this); tiao.addActionListener(this); addMouseMotionListener(this); addMouseListener(this); } //*********************************************************************************************************************// public void itemStateChanged(ItemEvent e) { int index1=choice1.getSelectedIndex(); //下拉框1設置顏色 if(index1==1){c= new Color(0,255,0,0);} else if(index1==2){c=new Color(255,0,0,0);} else if(index1==0){c=new Color(0,0,255,0);} else if(index1==4){c=new Color(0,0,0,255);} int index2=choice2.getSelectedIndex(); //下拉框2設置點的大小(線的粗細) if(index2==2){con=5;} else if(index2==1) {con=3;} else {con=1;} int index3=choice3.getSelectedIndex(); //下拉框3設置畫的圖形(直線,圓形,矩形) if(index3==0){xin=0;cho=1;} else if(index3==1){xin=1;cho=1;} else {xin=2;cho=1;} int index4=choice4.getSelectedIndex(); //下拉框4設置手工或是自動 if(index4==1)cho=1; if(index4==0)cho=0; rubberanouce=0;clearanouce=0; } //*********************************************************************************************************************// public void paint(Graphics g) //畫圖主程序,重畫的調用 { Graphics2D g2d=(Graphics2D)g; //轉化化2D圖 if(clearanouce==1)g2d.clearRect(0,0,getSize().width,getSize().height);clearanouce=0; if(x!=-1&&y!=-1) { n1=v1.size(); //取向量的個數 for(int i=0;i<n1-1;i++) { Point p1=(Point)v1.elementAt(i); Point p2=(Point)v1.elementAt(i+1); BasicStroke bs=new BasicStroke(p1.con); //設置線的粗細 g.setColor(p1.col); //設置線的顏色 int flag=1;n2=v2.size(); temp=p1.cho; //用來判斷怎樣畫圖 for(int j=0;j<n2;j++) //判斷是否鼠標放點 {Point p=(Point)v2.elementAt(j);if((p1.x==p.x)&&(p1.y==p.y))flag=0;} if(temp==0&&flag==1) //手動畫圖 { Line2D line=new Line2D.Double(p1.x,p1.y,p2.x,p2.y); g2d.draw(line); } if(flag==1||temp>=1) //自動畫圖 { if(temp>=1)i++; //自動畫圖時每畫一個跳過一個點 g2d.setStroke(bs); //取點的大小作為線的粗細 r=p1.dis(p2); //當畫圓的時候求出p1與p2之間的距離作為半徑 if(temp==1) //畫直線 {Line2D line=new Line2D.Double(p1.x,p1.y,p2.x,p2.y); g2d.draw(line);} if(temp==2) //畫圓形 { Ellipse2D ellipse=new Ellipse2D.Double(p1.x-r,p1.y-r,r*2,r*2); g2d.draw(ellipse); } if(temp==3) //畫矩形 { Rectangle2D rect=new Rectangle2D.Double(0,0,0,0);//畫矩形的四種可能 if((p1.x<p2.x)&&(p1.y<p2.y)) rect=new Rectangle2D.Double(p1.x,p1.y,p2.x-p1.x,p2.y-p1.y); if((p1.x>p2.x)&&(p1.y>p2.y)) rect=new Rectangle2D.Double(p2.x,p2.y,p1.x-p2.x,p1.y-p2.y); if((p1.x<p2.x)&&(p1.y>p2.y)) rect=new Rectangle2D.Double(p1.x,p2.y,p2.x-p1.x,p1.y-p2.y); if((p1.x>p2.x)&&(p1.y<p2.y)) rect=new Rectangle2D.Double(p2.x,p1.y,p1.x-p2.x,p2.y-p1.y); g2d.draw(rect); } } } }}//*************************************************************************************************************************//public void actionPerformed(ActionEvent e){ if(e.getSource()==rubber) //響應橡皮擦事件 { rubberanouce=1;clearanouce=0 ;c= new Color(255,255,255);con=10; } if(e.getSource()==clear) //響應清除器事件 { clearanouce=1; rubberanouce=0;v1.removeAllElements();v2.removeAllElements(); repaint(); } if(e.getSource()==tiao) //響應調色板事件 { c=JColorChooser.showDialog(this,"",c); }}public void mouseDragged(MouseEvent e){ if(cho==0) //控制自動畫圖還是手動畫圖的參數 { x=(int)e.getX();y=(int)e.getY(); //取當前鼠標前的X軸和Y軸坐標用來存入點中 Point p=new Point(x,y,c,con,cho); //X坐標,Y坐標,顏色,點的大小,用來作自動畫圖還是手動畫圖 v1.addElement(p); //把點對象存入向量中 repaint(); }}public void update(Graphics g){ paint(g);} public void mouseClicked(MouseEvent e){}public void mousePressed(MouseEvent e){ if(cho==1) //用來判斷自動畫圖 { t=xin+1; //臨時變量t為控制畫圖的參數 x=(int)e.getX();y=(int)e.getY(); Point p=new Point(x,y,c,con,t); v1.addElement(p); repaint(); }} public void mouseReleased(MouseEvent e){ x=(int)e.getX();y=(int)e.getY(); //用來斷開連續的向量 Point p=new Point(x,y,c,con,cho); v2.addElement(p); repaint();} public void mouseMoved(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){}}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -