画板

窗体类

package com032802;

import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JSlider;

public class DrawBorder extends JFrame{
	//定义主函数
	public static void main(String[] args){
		//实例化DrawBorder类的对象
	DrawBorder db=new DrawBorder();
	//调用initUI函数
	db.initUI();	
	}
	//定义一个initUI函数
	public void initUI(){
		//设置属性值
		this.setTitle("");
		this.setSize(800, 900);
		this.setDefaultCloseOperation(3);
		this.setResizable(true);
		this.setLocationRelativeTo(null);
		this.setLayout(new FlowLayout());
		//实例化按钮
		JButton butLine=new JButton("Line");
		JButton butRect=new JButton("Rect");
		JButton butOval=new JButton("Oval");
		JButton butColor=new JButton("Color");
		JButton butOval0=new JButton("Oval0");
		JButton butRect0=new JButton("Rect0");
		JButton butSanJiao=new JButton("SanJiao");
		JButton butRand=new JButton("Rand");
		JButton butWall=new JButton("Wall");
		JButton butRing=new JButton("Ring");
		//将按钮添加到窗体上		
		this.add(butLine);
		this.add(butRect);
		this.add(butOval);
		this.add(butColor);
		this.add(butOval0);
		this.add(butRect0);
		this.add(butSanJiao);
		this.add(butRand);
		this.add(butWall);
		this.add(butRing);
				
		ActionListener a=new ActionListener(){
			public void actionPerformed(ActionEvent e){
				if(e.getActionCommand().equals("Color")){
					color=JColorChooser.showDialog(null, "颜色的选择", Color.black);
						System.out.println("Color="+color);
				}
				else{
					str=e.getActionCommand();
					System.out.println("str="+str);
				}
			}
			
	  };
           //给按钮添加监听器
	  butLine.addActionListener(a);
		butRect.addActionListener(a);
		butOval.addActionListener(a);
		butColor.addActionListener(a);
		butOval0.addActionListener(a);
		butRect0.addActionListener(a);
		butSanJiao.addActionListener(a);
		butRand.addActionListener(a);
		butWall.addActionListener(a);
		butRing.addActionListener(a);
		
		
		
		
		
		
		this.setVisible(true);
		 //取得画布
		Graphics g=this.getGraphics();
       		DrawListener d=new DrawListener(g);
		this.addMouseListener(d);
		//实例化拉杆并设置拉杆属性
		JSlider js=new JSlider();
		js.setMaximum(300);
		js.setValue(10);
		//给拉杆添加监听器
		MyChangeListener mcl=new MyChangeListener(js,this,g);
		js.addChangeListener(mcl);
		this.add(js);
	
	}
	public static Color color=Color.black;
	public static String str="Line";
	
}


监听器
package com032802;

import com032802.DrawBorder;

import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class DrawListener implements MouseListener{
    public int x1,x2,y1,y2,x0,y0,xp,yp;
    public double x3,x4,y3,y4;
  public int x,y;
  public static float a=1.7f,b=1.7f,c=0.06f,d=1.2f;
  public static float a1=1.4f,b1=1.56f,c1=1.4f,d1=-6.56f;
  
  //实例化随机数
  Random rd=new Random();
   Graphics g;

   public DrawListener(Graphics g){
		this.g=g;
	}
	public void mousePressed(MouseEvent e) {
		x1=e.getX();
		y1=e.getY();
	}

	public void mouseReleased(MouseEvent e) {
		System.out.print("ssss");
		x2=e.getX();
		y2=e.getY();
		g.setColor(DrawBorder.color);
		if(DrawBorder.str.equals("Rect0")){
		this.DrawRect0(e);
		}else if(DrawBorder.str.equals("Line")){
			g.drawLine(x1,y1,x2,y2);
		}else if(DrawBorder.str.equals("Rect")){
			g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
		}else if(DrawBorder.str.equals("Oval")){
			g.drawOval(x1, y1,Math.abs(x1-x2), Math.abs(y1-y2));
		}else if(DrawBorder.str.equals("Oval0")){
			this.DrawOval0(e);
		}else if(DrawBorder.str.equals("SanJiao")){
			this.DrawSanJiao(e);
		}else if(DrawBorder.str.equals("Rand")){
			this.DrawRand(e);
		}else if(DrawBorder.str.equals("Wall")){
			this.DrawWall(e);
		}else if(DrawBorder.str.equals("Ring")){
			this.DrawRing(e);
		}
}
		
	
	
	public void DrawRect0(MouseEvent e){
		for(int i=0;i<100;i++){
			if(x1!=x2||y1!=y2){
			
				g.drawRect(x1, y1,Math.abs(x1-x2),Math.abs(y1-y2));
				
				x1=x1+2;y1=y1+2;x2=x2-2;y2=y2-2;
			}		
				
		}	
	}
	public void DrawOval0(MouseEvent e){
		for(int i=0;i<50;i++){
			g.drawOval(x1, y1,Math.abs(x1-x2), Math.abs(y1-y2));
			
			x1=x1+2;y1=y1+2;x2=x2-2;y2=y2-2;
			
		}
}
	public void DrawSanJiao(MouseEvent e){
		for(int i=0;i<100;i++){
			g.drawLine(x1, y1,x2,y1);
			
			x1--;y1=y1+2;x2++;
			}
	}
	public void DrawRand(MouseEvent e){
		x0=rd.nextInt(1000);
		y0=rd.nextInt(1000);
		x1=rd.nextInt(1000);
		y1=rd.nextInt(1000);
		x2=rd.nextInt(1000);
		y2=rd.nextInt(1000);
		xp=rd.nextInt(1000);
		yp=rd.nextInt(1000);
		for(int i=0;i<10000;i++){
		int r=rd.nextInt(3);
		switch(r){
		case 0: {xp=(x0+xp)/2;
			 	yp=(y0+yp)/2;
			 	g.drawLine(xp, yp, xp, yp);break;
				}
			 	
		case 1: {xp=(x1+xp)/2;
	 			yp=(y1+yp)/2;
	 			
	 			g.drawLine(xp, yp, xp, yp);break;
				}
		case 2: {xp=(x2+xp)/2;
	 			yp=(y2+yp)/2;
	 			g.drawLine(xp, yp, xp, yp);break;}
		}
		}
	}
	
	public void DrawWall(MouseEvent e){
		for(int i=0;i<1000000;i++){
		x4=Math.sin(a*y3)+Math.cos(a*x3)*c;
		y4=Math.sin(b*x3)+Math.cos(b*y3)*d;
		x3=x4;
		y3=y4;
		x=(int)(x3*100)+500;
		y=(int)(y3*100)+500;
		g.drawLine(x, y, x, y);
	}
}
	public void DrawRing(MouseEvent e){
		for(int i=0;i<50000;i++){
		x4=d1*Math.sin(a1*x3)-Math.sin(b1*y3);
		y4=c1*Math.cos(a1*x3)+Math.cos(b1*y3);
		x3=x4;
		y3=y4;
		x=(int)(x3*10)+500;
		y=(int)(y3*10)+500;
		g.drawLine(x, y, x, y);
	}
}
	public void mouseEntered(MouseEvent e) {
		
	}

	public void mouseExited(MouseEvent e) {
		
	}
	public void mouseClicked(MouseEvent e) {
		
	}
}

拉杆
package com032802;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;

public class MyChangeListener implements javax.swing.event.ChangeListener{
public JSlider js;
public JFrame jf;
public Graphics g;
int x,y,x1=50,x2=500,y1=50,y2=500;
         //构造方法
	public MyChangeListener(JSlider js,JFrame jf,Graphics g){
		this.js=js;
		this.jf=jf;
		this.g=g;
		
	}
	public void stateChanged(ChangeEvent e) {
		js.getValue();
		int w=jf.getWidth();
		int h=jf.getHeight();
	          	for(int i=0;i<255;i++){
			    Color c=new Color(0,i,i);
		             g.setColor(c);
			    g.fillOval(x1,y1,Math.abs(x1-x2),Math.abs(y2-y1));
			    x1++;y1++;x2--;y2--;
			}
			
			ImageIcon im=new ImageIcon("images/签名0.jpg");
			Image ima=im.getImage();
			g.drawImage(ima,w/4+300,h/4+100,null);
		
	}
}

你可能感兴趣的:(画板)