再描画する


図面描画-再描画の概要
1.シェイプshapeインタフェースを作成し、すべてのグラフィックを描画してこのインタフェースを継承します.
import java.awt.Color;

/**
 *      ,            
 * @author wenxiaodong
 *
 */
public abstract class Shape {
	int x1,x2,y1,y2;
	Color color;
	/**
	 *        
	 */
	public abstract void draw(java.awt.Graphics g);

}

 2.キューインタフェースの作成、サブクラス実装の追加、削除、変更、呼び出しなどの機能
public interface ListInterface <E>{
//  
	public void add(E e);
//  
	public E get(int index);
//    
	public int size();
//      
	public void deleteAll();
//        
	public void deleteone(int index);
//         
	public void add(E e,int index);
//      
	public void modefy(E e,int index);
}

 
3.クラス実装マウスリスナーを定義する
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.MouseEvent;

public class DrawListener implements java.awt.event.MouseListener{

	private int x1,y1,x2,y2;
	private Graphics g;
	private javax.swing.ButtonGroup group;
	private String type="Line";//      
	private Color color=Color.green;
	
	//                 
	private ListImp<Shape> shapes ;//  

	
	public DrawListener(Graphics g,javax.swing.ButtonGroup group,ListImp<Shape> shapes){
		this.g= g;
		this.group = group;
		this.shapes = shapes;
	}
	
	

	public void mousePressed(MouseEvent e) {
		x1 = e.getX();
		y1 = e.getY();
		//       
		type = group.getSelection().getActionCommand();
	}

	
	
	public void mouseReleased(MouseEvent e) {
		x2 = e.getX();
		y2 = e.getY();
		
		//sh shape   

		Shape sh=null;
		
		if(type.equals("Line")){
			//      
			sh = new Line(x1,y1,x2,y2,color);	
		}else if(type.equals("Rect")){
			sh = new Rect(x1,y1,x2,y2,color); 
		}else if(type.equals("Oval")){
			sh = new Oval(x1,y1,x2,y2,color);	
		}else if(type.equals("Arc")){
			sh=new Arc(x1,y1,x2,y2,color);
		}
						
		
		//    
		sh.draw(g);
		
		//      
		shapes.add(sh);	
	}

		
		
	public void mouseEntered(MouseEvent e) {

	}

	public void mouseExited(MouseEvent e) {

	}

	public void mouseClicked(MouseEvent e) {

	}
}

 
4.クラスを定義し、フォームを作成し、フォーム上でキャンバスを切り取り、リスナーをキャンバスに追加し、最後にShapeオブジェクトをキューで保存し、再描画を実現する
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionEvent;

import javax.swing.JFrame;
/**
 *    
 * @author wenxiaodong
 *2011-10-20   09:20:32
 */
public class Draws extends JFrame{
	
	private ListImp<Shape> shapes=new ListImp<Shape>() ;
	private java.awt.Graphics g;
	
	public static void main(String[] args){
		Draws ds=new Draws();
		ds.dra();
	}
	
	public void dra(){
		this.setTitle("   ");
		this.setSize(600,500);
		
		//      
		this.setResizable(false);
		
		this.setLayout(new FlowLayout(0,0,0));
		
		
		
		javax.swing.JPanel left=new javax.swing.JPanel();
		//      
		Dimension  di=new Dimension(80,400);
		//      
		left.setPreferredSize(di);
		//     
		left.setBackground(Color.green);
		
		
		javax.swing.JPanel right=new javax.swing.JPanel();
		//      
		Dimension  di2=new Dimension(514,400);
		//      
		right.setPreferredSize(di2);
		//     
		right.setBackground(Color.white);
		
		
		javax.swing.JPanel bottom=new javax.swing.JPanel();
		//      
		Dimension  di3=new Dimension(595,80);
		//      
		bottom.setPreferredSize(di3);
		//     
		bottom.setBackground(Color.GRAY);	
		
		this.add(left);
		this.add(right);
		this.add(bottom);
		

		
		
		javax.swing.ButtonGroup group=new javax.swing.ButtonGroup();
		
		javax.swing.JRadioButton line=new javax.swing.JRadioButton("Line");
		line.setActionCommand("Line");
		line.setSelected(true);
		
		javax.swing.JRadioButton rect=new javax.swing.JRadioButton("Rect");
		rect.setActionCommand("Rect");
		
		javax.swing.JRadioButton oval=new javax.swing.JRadioButton("Oval");
		oval.setActionCommand("Oval");
		
		javax.swing.JRadioButton arc=new javax.swing.JRadioButton("Arc");
		arc.setActionCommand("Arc");
		
	
		group.add(line);
		group.add(rect);
		group.add(oval);
		group.add(arc);
	
		left.add(line);
		left.add(rect);
		left.add(oval);
		left.add(arc);
		
		javax.swing.JButton bu_color=new javax.swing.JButton("   ");
		
		bottom.add(bu_color);
	
		
		this.setDefaultCloseOperation(3);
		this.setVisible(true);	
		g=right.getGraphics();
		DrawListener lis=new DrawListener(g,group,shapes);
		right.addMouseListener(lis);
		
	}
	

		
		/**
		 *            
		 */
		public void paint(java.awt.Graphics g){
			
			//               
			super.paint(g);
			
			//         
			drawShape(this.g);
		}
		
		
		//         
		public void drawShape(Graphics g){
			//    
			for(int i=0;i<shapes.size();i++){
				//      
				Shape sh = shapes.get(i);
				//  
				sh.draw(g);
			}
		}
	

		
	
	}