画板的重绘

   画板的重绘

    这一周学习了画板以及画板的重绘。
    画板就是针对JFrame的掌握,还有获取画布graphics,在画布上进行绘图。主要掌握MouseListener和ActinListener接口的使用。


     1.画板的完成及理解
             创建窗体->添加按钮->添加事件->对象间相互调用
     2.画板重绘关键点 
              1.定义一个JFrame的子类,重写JFrame父类的paint函数 
              2.要讲图形存放到队列里 
              3.在paint函数中将队列中保存的图形重新绘制

    重绘要使用自定义队列,所以要创建一个自定义的队列,来存放所画的图形。每一次对窗口的缩小或者别的使用,在显示窗口时就调用重绘。
   

package cn.java.hsm;

import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;


public class DrawTest extends JFrame {
	//自定义队列
	private MyArray array = new MyArray();
	//paint的方法
	public void paint(Graphics g){
		super.paint(g);//super关键字
		int x1,x2,y2,y1;
		String type = "line";
		
		//即重新绘制,画板的重绘,以数组的形式保存图形并重新绘制
		for(int i = 0; i < array.getLength(); i++)
		{
			shape sp= array.getAdd(i);
			x1=sp.getX1();
			y1=sp.getY1();
			x2=sp.getX2();
			y2=sp.getY2();
			type = sp.getType();
		
			if(type=="line"){
				g.drawLine(x1, y1, x2, y2);			
			}
			if(type=="oval"){
				g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
			}
			if(type=="rect"){
				g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
			}
		}
	
	}

	public static void main(String[] args)
	{
		DrawTest dt = new DrawTest();
		dt.showUI();	
	}

	//showUI的方法
	public void showUI() 
	{
		this.setTitle("简单画板");
		this.setSize(600,550);
		this.setDefaultCloseOperation(3);
		this.setLayout(new FlowLayout());
		//添加按钮
		JButton jButtonLine = new JButton("line");
		JButton jButtonOval = new JButton("oval");
		JButton jButtonRect = new JButton("rect");
		JButton jButtonColor = new JButton("color");
		
		this.add(jButtonColor);
		this.add(jButtonLine);
		this.add(jButtonOval);
		this.add(jButtonRect);
		//使其窗口可见
		this.setVisible(true);
		Graphics g = this.getGraphics();//获取画布
		
		MouseListenerImpl mouseListenerImpl= new MouseListenerImpl(g, array) ;
		this.addMouseListener(mouseListenerImpl);//画板上添加监听器
		
		ActionListenerImpl actionListenerImpl = new ActionListenerImpl(mouseListenerImpl);
		
		jButtonLine.addActionListener(actionListenerImpl);
		jButtonOval.addActionListener(actionListenerImpl);
		jButtonRect.addActionListener(actionListenerImpl);//按钮上添加监听器
		jButtonColor.addActionListener(actionListenerImpl);
	}
	
}


-----------------------------------------------------

package cn.java.hsm;

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



public class MouseListenerImpl implements MouseListener{
	private Graphics g;
	private int x1;
	private int y1;
	private int x2;
	private int y2;
	private String type = "line";
	private MyArray array;
	public MouseListenerImpl(Graphics g, MyArray array) {
		this.g = g;
		this.array = array;
	}

	
	public void mouseClicked(MouseEvent e) {
		
		
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		

	}

	@Override
	public void mouseExited(MouseEvent e) {
		
		}
		

	@Override
	public void mousePressed(MouseEvent e) {
		x1=e.getX();
		y1=e.getY();
		
	}

	@Override
	public void mouseReleased(MouseEvent e) {
		x2=e.getX();
		y2=e.getY();
		if(type.equals("line")){
			g.drawLine(x1, y1, x2, y2);			
		}
		if(type.equals("oval")){
			g.drawOval(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
		}
		if(type.equals("rect")){
			g.drawRect(x1, y1, Math.abs(x1-x2), Math.abs(y1-y2));
		}
		shape a= new shape(x1, y1, x2, y2, type);
		
		array.add(a);
		
	}
         
	public void setType(String type) {
		this.type = type;	
	}
}


-----------------------------------------------------------------

package cn.java.hsm;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;

public class ActionListenerImpl implements ActionListener{
	private MouseListenerImpl mouseListenerImpl ;
	
	
	//构造函数
	public ActionListenerImpl(MouseListenerImpl mouseListenerImpl ){
		this.mouseListenerImpl = mouseListenerImpl;
	}
	public void actionPerformed(ActionEvent e)
	{
		JButton button = (JButton) e.getSource();
		mouseListenerImpl.setType(button.getText());	
	}

}


--------------------------------------------------------------

package cn.java.hsm;

public class MyArray {
	//自定义队列
	private shape[] old = new shape[0];
	
	public void add(shape a){
		shape[] newly = new shape[old.length+1];
		newly[old.length] = a;
		for(int i=0;i<old.length;i++){
			newly[i] = old[i];
		}
		old = newly;
	}
	
	public shape getAdd(int i){
		return old[i];
	}
	
	public int getLength(){
		return old.length;
	}
	
	

}


--------------------------------------------------------------

package cn.java.hsm;

public class shape {
	private int x1;
	private int x2;
	private int y1;
	private int y2;
	private String type;
	
	//构造函数
	public shape(int x1,int y1,int x2,int y2, String type){
		this.x1=x1;
		this.x2=x2;
		this.y1=y1;
		this.y2=y2;
		this.type=type;
	}

	public int getX1() {
		return x1;
	}

	public void setX1(int x1) {
		this.x1 = x1;
	}

	public int getX2() {
		return x2;
	}

	public void setX2(int x2) {
		this.x2 = x2;
	}

	public int getY1() {
		return y1;
	}

	public void setY1(int y1) {
		this.y1 = y1;
	}

	public int getY2() {
		return y2;
	}

	public void setY2(int y2) {
		this.y2 = y2;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
}		

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值