- package myproject.lesson;
- import myproject.*;
- import java.awt.*;
- import javax.swing.*;
- import java.awt.Graphics;
- import java.awt.Point;
- import java.awt.event.*;
- //
- public class Draw extends Canvas implements MouseListener,MouseMotionListener,ActionListener{
- private static int x0,x1,y0,y1,r=100;
- private float buffer1,buffer2,buffer3,buffer4;
- private static int type=1;
- private static Draw dr;
- private static float u1,u2;
- private static JButton button1,button2,button3,button4,button5,clear;
- private static User s;
- private static JSplitPane split;
- private int a,b;
- public Draw() //画布类的构造函数
- {
- //this.setSize(400,300);
- //this.setBounds(0,0,10,70);
- this.setBounds(50,50,700,780);
- this.setBackground(Color.white);
- this.addMouseListener(this);
- this.addMouseMotionListener(this);
- }
- //主函数
- public static void main (String[] args) {
- JFrame.setDefaultLookAndFeelDecorated(true);
- JFrame fr=new JFrame();
- fr.setTitle("画图小程序");
- //fr.setLayout(null);
- button1=new JButton(new ImageIcon("1.jpg"));
- //button1.setForeground(Color.blue);
- button2=new JButton(new ImageIcon("33.jpg"));
- //button2.setForeground(Color.blue);
- button3=new JButton(new ImageIcon("43.jpg"));
- //button3.setForeground(Color.blue);
- button4=new JButton("矩形");
- button5=new JButton("直线裁剪");
- clear=new JButton("清除");
- JToolBar bar=new JToolBar();
- bar.add(button1);
- bar.add(button2);
- bar.add(button3);
- bar.add(button4);
- bar.add(button5);
- bar.add(clear);
- dr=new Draw();
- TabbedPaneFrame tabbe=new TabbedPaneFrame();
- split=new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,tabbe,dr);
- split.setDividerLocation(200);
- fr.add(bar,BorderLayout.NORTH); //添加工具条
- //fr.add("West",new TabbedPaneFrame()); //图形面板
- button1.setEnabled(false);
- //添加事件
- button1.addActionListener(dr);
- button2.addActionListener(dr);
- button3.addActionListener(dr);
- button4.addActionListener(dr);
- button5.addActionListener(dr);
- clear.addActionListener(dr);
- //添加菜单栏
- fr.setJMenuBar(MenuExmple.MFrame());
- //添加画布
- //fr.add(dr,BorderLayout.CENTER);
- fr.add(split);
- fr.setBounds(50,50,1100,700);
- fr.setResizable(false);
- //fr.setLocation(150,30);
- fr.setVisible(true);
- //关闭窗口事件
- fr.addWindowListener(new WindowAdapter(){
- public void windowClosing(WindowEvent e){
- System.exit(0);
- }});
- // fr.validate();
- }
- //按钮触发事件
- public void actionPerformed(ActionEvent e){
- JButton btn=(JButton) e.getSource();
- if(btn==button1){
- type=1; //btn.setVisible(true);
- button1.setEnabled(false);
- button2.setEnabled(true);
- button3.setEnabled(true);
- button4.setEnabled(true);
- button5.setEnabled(true);
- }
- if(btn==button2){
- s=new User();
- s.setAlwaysOnTop(true);
- type=2;
- button2.setEnabled(false);
- button1.setEnabled(true);
- button3.setEnabled(true);
- button4.setEnabled(true);
- button5.setEnabled(true);
- }
- if(btn==button3){
- a=TabbedPaneFrame.getA();
- b=TabbedPaneFrame.getB();
- System.out.println(a);
- System.out.println(b);
- type=3;
- button3.setEnabled(false);
- button1.setEnabled(true);
- button2.setEnabled(true);
- button4.setEnabled(true);
- button5.setEnabled(true);
- }
- if(btn==button4){
- type=4;
- button4.setEnabled(false);
- button1.setEnabled(true);
- button2.setEnabled(true);
- button3.setEnabled(true);
- button5.setEnabled(true);
- }
- if(btn==button5){
- if(buffer1==0&&buffer2==0){
- JOptionPane.showMessageDialog(dr,"请先画出一条直线");
- }
- type=5;
- button5.setEnabled(false);
- button4.setEnabled(true);
- button1.setEnabled(true);
- button2.setEnabled(true);
- button3.setEnabled(true);
- }
- if(btn==clear){
- type=-1;
- buffer1=0;buffer2=0;
- button5.setEnabled(true);
- button4.setEnabled(true);
- button1.setEnabled(true);
- button2.setEnabled(true);
- button3.setEnabled(true);
- repaint();
- }
- }
- //鼠标按下时,返回鼠标的坐标。
- public void mousePressed(MouseEvent e){
- x0=e.getX();
- y0=e.getY();
- //repaint();
- }
- //鼠标释放时,放回鼠标的坐标。
- public void mouseReleased(MouseEvent e){
- x1=e.getX();
- y1=e.getY();
- repaint();
- }
- public void mouseEntered(MouseEvent e){
- }
- public void mouseExited(MouseEvent e){
- }
- public void mouseClicked(MouseEvent e){
- }
- public void mouseMoved(MouseEvent e){
- //int flag=1;
- //x1=e.getX();
- //y1=e.getY();
- //repaint();
- }
- //鼠标按下到释放的过程。
- public void mouseDragged(MouseEvent e){
- //int flag=2;
- x1=e.getX();
- y1=e.getY();
- //repaint();
- }
- //生成画布时调用的画图方法。
- public void paint(Graphics e){
- //super.paint(e);
- e.setColor(TabbedPaneFrame.getColor());
- switch(type){
- case 1: ddaline(x0,y0,x1,y1,e);buffer1=x0;
- break; //直线扫描
- case 2: midpointcircle(x0,y0, s.getRadius(),e);break; //中点画圆
- case 3: MidpointEllipe(x0,y0,a,b,e);break;//椭圆
- case 4: fillRectangle(x0, y0,x1,y1, e);
- break;//矩形的扫描
- case 5: e.clearRect(0,0,dr.getWidth(),dr.getHeight());
- //floodFill(x0,y0, e); //BoundaryFill4(x0,y0,Color.blue.getRGB(),Color.red,e);
- lineClip(buffer1,buffer2,buffer3,buffer4,x0,y0,x1,y1,e);
- type=0;
- break;
- default: e.clearRect(0,0,dr.getWidth(),dr.getHeight()); System.out.print("erro!");
- }
- }
- //刷新画布调用的方法
- public void update(Graphics e){
- //super.paint(e);
- paint(e);
- }
- //DDA画线方法
- public void DDAline(int x0,int y0,int x1,int y1,Graphics g){
- int x,y3;
- float dx=x1-x0,dy=y1-y0;
- float k=dy/dx,y=y0,x3=x0;
- if(k>=-1||k<=1)
- {
- for(x=x0;x<=x1;x++){
- g.drawLine(x,(int) (y+0.5),x,(int) (y+0.5));
- y=y+k;
- }
- }
- else
- for(y3=y0;y3<=y1;y3++){
- g.drawLine((int) (x3+0.5),y3,(int) (x3+0.5),y3);
- x3=x3+(1/k);
- }
- }
- //中点画椭圆算法
- public void pointellipse(int x0,int y0, int x1,int y1,Graphics g)//椭圆的对称性。
- {
- //g.setColor(Color.blue);
- g.drawLine(x0+x1,y0+y1,x0+x1,y0+y1); //椭圆上的一个点,
- g.drawLine(x0+x1,y0-y1,x0+x1,y0-y1); //跟给出点关于x轴对称的点;
- g.drawLine(x0-x1,y0+y1,x0-x1,y0+y1);
- g.drawLine(x0-x1,y0-y1,x0-x1,y0-y1);
- }
- //以点(x0,y0)为中心,画椭圆
- void MidpointEllipe(int x0,int y0,int a,int b,Graphics e)
- {
- int x,y; float d1,d2;
- x = 0; y = b;
- d1 = b*b +a*a*(-b+0.25f);
- pointellipse(x0,y0,x,y,e);
- while(b*b*(x+1)< a*a*(y-0.5f)) //用椭圆点上的法向量判断它是y分量增加的快的话
- { if (d1<0){
- d1+=b*b*(2*x+3); x++; }
- else { d1 +=(b*b*(2*x+3)+a*a*(-2*y+2)) ;
- x++; y--; }
- pointellipse(x0,y0,x,y,e);
- }//上部分
- //下部分
- d2=(float)Math.sqrt(b*(x+0.5f))+(float)Math.sqrt(a*(y-1))-(float)Math.sqrt(a*b);
- while(y >0)
- {
- if (d2 <0) { d2 +=b*b*(2*x+2f)+a*a*(-2*y+3f);
- x++; y--;}
- else {d2 += a*a*(-2*y+3f); y--; }
- pointellipse(x0,y0,x,y,e);
- }
- }
- //中点画圆算法。
- public void circlepoint(int x0,int y0,int x1,int y1,Graphics e) /*利用圆的八对称性*/
- {
- e.drawLine(x0+x1,y0+y1,x0+x1,y0+y1);
- e.drawLine(x0+y1,y0+x1,x0+y1,y0+x1);
- e.drawLine(x0+x1,y0-y1,x0+x1,y0-y1);
- e.drawLine(x0+y1,y0-x1,x0+y1,y0-x1);
- e.drawLine(x0-x1,y0+y1,x0-x1,y0+y1);
- e.drawLine(x0-y1,y0+x1,x0-y1,y0+x1);
- e.drawLine(x0-x1,y0-y1,x0-x1,y0-y1);
- e.drawLine(x0-y1,y0-x1,x0-y1,y0-x1);
- }
- public void midpointcircle(int x0,int y0,int r,Graphics g)
- {
- int x,y,d,same,change;
- x=0;y=r;d=5-4*r;same=12;change=20-8*r; /*从点(0,r)画到点(r/1.413,r/1.414)*/
- circlepoint(x0,y0,x,y,g);
- while(y>x)
- {
- if(d>0) /*下一点y要变*/
- {
- d+=change;
- change+=16;
- y--;
- }
- else /*下一点y不变*/
- {
- d+=same;
- change+=8;
- }
- x++;
- same+=8;
- circlepoint(x0,y0,x,y,g);
- }
- }
- //画矩形的算法
- public void fillRectangle(int x0,int y0,int width,int height,Graphics e){
- int x,y;
- // e.setColor(Color.blue);
- for(y=y0;y<=height;y++){
- for(x=x0;x<=width;x++){
- e.drawLine(x,y,x,y);
- }
- }
- }
- //边界填充算法
- void BoundaryFill4(int x,int y,int boundaryColor,Color red,Graphics e)
- { int color=0;
- try{
- Robot robot=new Robot();
- Color co=robot.getPixelColor(x,y);
- color=co.getRGB();
- }
- catch(AWTException ea){
- ea.printStackTrace();
- }
- e.setColor(red);
- if((color != boundaryColor) && (color != red.getRGB()))
- { e.drawLine(x,y,x,y);
- BoundaryFill4(x,y+1,boundaryColor,red,e);
- BoundaryFill4(x+1,y,boundaryColor,red,e);
- BoundaryFill4(x-1,y,boundaryColor,red,e);
- BoundaryFill4(x,y-1,boundaryColor,red,e);
- }
- else{
- System.out.print("erro!");
- }
- }/*end of BoundaryFill4() */
- //直线裁剪算法
- void lineClip(float x1,float y1,float x2,float y2,int XL,int XR,int YB,int YT,Graphics e)
- { System.out.println(buffer1);
- System.out.println(buffer2);
- float dx,dy;
- u1=0; u2=1 ;
- dx =x2-x1;dy=y2-y1;
- if(ClipT(-dx,x1-XL))
- if(ClipT(dx,XR-x1))
- if(ClipT(-dy,y1-YB))
- if(ClipT(dy,YT-y1))
- {
- e.setColor(Color.red);
- System.out.print(u2);
- System.out.print(u1);
- e.drawLine((int)(x1+u1*dx),(int)(y1+u1*dy),(int) (x1+u2*dx),(int)(y1+u2*dy));
- }
- }
- public boolean ClipT(float p,float q)
- { //System.out.print("dd");
- float r;
- if(p<0)
- { r=q/p;
- if(r> u2) return false;
- else if(r>u1)
- { u1=r; return true; }
- }
- else if(p>0)
- { r=p/q;
- if(r<u1)
- return false;
- else if(r<u2)
- { u2=r;return true;}
- }
- else if(q<0) return false;
- return true;
- }
- //dda直线扫描转换算法
- public void ddaline(int x0,int y0,int x1,int y1,Graphics g)
- {
- int t,dx,dy;
- float k,x,y;
- if(x0==x1)
- {
- if(y0>y1)
- {
- t=y0;
- y0=y1;
- y1=t;
- }
- for(y=y0;y<y1;y=y+1.0f)
- g.drawLine(x0,(int)y,x0,(int) y);
- }
- else
- {
- dx=x1-x0;
- dy=y1-y0;
- k=(float)((float)dy/(float)dx);
- if((-1.0<=k)&&(k<=1.0))
- {
- if(x0>x1)
- {
- t=x0;
- x0=x1;
- x1=t;
- t=y0;
- y0=y1;
- y1=t;
- }
- y=(float)y0;
- for(x=(float)x0;x<(float)x1;x=x+1.0f)
- {
- g.drawLine((int)x,(int)(y+0.5),(int) x,(int) (y+0.5));
- y+=k;
- }
- }
- else
- {
- if(y0>y1)
- {
- t=x0;
- x0=x1;
- x1=t;
- t=y0;
- y0=y1;
- y1=t;
- }
- k=(float)((float)dx/(float)dy);
- x=(float)x0;
- for(y=(float)y0;y<(float)y1;y=y+1.0f)
- {
- g.drawLine((int)(x+0.5),(int)y,(int) (x+0.5),(int) y);
- x+=k;
- }
- }
- }
- buffer1=x0;buffer2=y0;buffer3=x1;buffer4=y1;
- }
- public void fillPoly(int x1,int y1,Graphics e){
- ddaline(x0,x1,x1,y1,e);
- ddaline((int)buffer3,(int)buffer4,x1,y1,e);
- }
- }