1. 程式人生 > >Java小程式之高階畫板重繪篇I

Java小程式之高階畫板重繪篇I

package com.huaxin.zhou;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Stroke;
import java.util.ArrayList;

import javax.swing.ButtonGroup;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.border.BevelBorder;

public class Mypaint extends JFrame{
	
	public ArrayList list = new ArrayList();
	
	public Color color=Color.BLACK;
	
	public JButton btt;
	
	
	
	public void initFrame(){
		//設定窗體屬性
		this.setSize(600,500);
		this.setDefaultCloseOperation(3);
		this.setTitle("我的畫板");
		this.setLocationRelativeTo(null);
		//給窗體新增主面板
		JPanel panel = new JPanel();
		//設定窗體佈局
		panel.setLayout(new BorderLayout());
		//新增主面板
		this.add(panel);
		
		
		//給窗體新增選單
		JMenuBar bar= new JMenuBar();
		panel.add(bar,BorderLayout.NORTH);
		String []str={"檔案","檢視","幫助","檢視"}; 
		  String []str1={"開啟檔案","檢視檔案","幫助","檢視"}; 
	    for(int i=0;i<4;i++){
	    	JMenu menu = new JMenu(str[i]);
	    	  for(int j=0;j<4;j++){
	  	    	JMenuItem item = new JMenuItem(str1[j]);
	  	    	menu.add(item);
	  	    }
	    	bar.add(menu);
	    }
	    
	  
		
		//新增中間面板
		JPanel panelcenter = new JPanel();
		panelcenter.setLayout(new FlowLayout(FlowLayout.LEFT,10,10));
		panel.setBackground(Color.gray);
		
		panel.add(panelcenter);
		
		
		//新增中間面板的子面板
		JPanel panelcenterchild = new JPanel(){
			//畫板重繪
			public void paint(Graphics g1) {
				super.paint(g1);
				Graphics2D g=(Graphics2D)g1;
				for (int i = 0; i <list.size(); i++) {
					//如果容器中使直線物件,則重新繪製直線
					if(list.get(i) instanceof Line){
						Line line = (Line)list.get(i);
						g.setColor(line.color);
						Stroke s =(BasicStroke)(line.s); 
						g.setStroke(s);
						g.drawLine(line.x1, line.y1, line.x2, line.y2);
					}
					//重新繪製矩形
					else if(list.get(i) instanceof Rect){
						Rect rect = (Rect)list.get(i);
						g.setColor(rect.color);
						Stroke s =(BasicStroke)(rect.s); 
						g.setStroke(s);
						g.drawRect(rect.x1, rect.y1,rect.x2, rect.y2);
					}//重繪橢圓
					else if(list.get(i) instanceof Oval){
						Oval oval = (Oval)list.get(i);
						g.setColor(oval.color);
						Stroke s =(BasicStroke)(oval.s); 
						g.setStroke(s);
						g.drawOval(oval.x1, oval.y1, oval.x2, oval.y2);
					}
					//噴桶重繪
					else if(list.get(i) instanceof Point){
						Point point = (Point)list.get(i);
						g.setColor(point.color);
						Stroke s =(BasicStroke)(point.s); 
						g.setStroke(s);
						g.drawLine(point.x1, point.y1,point.x1,point.y1);
					}
					//圓角矩形重繪
					else if(list.get(i) instanceof RoundRect){
						RoundRect rr = (RoundRect)list.get(i);
						g.setColor(rr.color);
						Stroke s =(BasicStroke)(rr.s); 
						g.setStroke(s);
						g.drawRoundRect(rr.x1,rr.y1,rr.x2,rr.y2,rr.i,rr.j);
					}
				}
			}			
				
		};
		panelcenterchild.setBackground(Color.WHITE);
		panelcenterchild.setPreferredSize(new Dimension(500,330));
		panelcenter.add(panelcenterchild);
		
		//新增左邊面板
		JPanel panelleft = new JPanel();
		panelleft.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		panelleft.setBackground(new Color(235,233,238));
		panelleft.setPreferredSize(new Dimension(50, 0));
		
		//給左邊面板新增按鈕
		ButtonGroup bg = new ButtonGroup();
		for(int i=0;i<16;i++){
			//給每個按鈕新增四種不同的圖片
			ImageIcon img1 = new ImageIcon("images/draw"+i+".jpg");
			ImageIcon img2 = new ImageIcon("images/draw"+i+"-1"+".jpg");
			ImageIcon img3 = new ImageIcon("images/draw"+i+"-2"+".jpg");
			ImageIcon img4 = new ImageIcon("images/draw"+i+"-3"+".jpg");
			JRadioButton jrb = new JRadioButton();
			jrb.setActionCommand("pic"+i);
			
			//設定預設選中項
			if(i==10){
				jrb.setSelected(true);
			}
			
			bg.add(jrb);
			
			jrb.setIcon(img1);//設定預設圖譜按
			jrb.setRolloverIcon(img2);//設定滑鼠停留在按鈕上的圖片
			jrb.setPressedIcon(img3);//設定滑鼠按下按鈕的圖譜按
			jrb.setSelectedIcon(img4);//設定滑鼠選中按鈕的圖片
			jrb.setBorder(null);//設定按鈕的邊框為空,按鈕凹下去效果
			panelleft.add(jrb);
		}
		
		//下邊面板
		JPanel paneldown = new JPanel();
		paneldown.setBackground(Color.gray);
		paneldown.setLayout(null);
		paneldown.setPreferredSize(new Dimension(0, 80));
		
		//下邊面板的子面板
		JPanel paneldownchild = new JPanel();
		paneldownchild.setLayout(null);
		paneldownchild.setBounds(15, 10, 300, 60);
		paneldownchild.setBackground(Color.green);
		
		//子面板中的左面板
		JPanel downleft = new JPanel();
		downleft.setLayout(null);
		downleft.setBackground(Color.white);
		downleft.setBounds(0, 0, 60, 60);
		
		//子面板中的右面板
		JPanel downright = new JPanel();
		downright.setBackground(Color.magenta);
		downright.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		downright.setBounds(60, 0, 240, 60);
		
		//子面板新增到相應的父面板中
		panel.add(panelleft,BorderLayout.WEST);
		panel.add(paneldown, BorderLayout.SOUTH);
		
		paneldown.add(paneldownchild);
		paneldownchild.add(downleft);
		paneldownchild.add(downright);
		
		//面板中新增兩個顏色按鈕
		btt = new JButton();
		btt.setBackground(Color.black);
		btt.setBounds(10, 10, 30, 30);
		
		
		JButton bt1 = new JButton();
		bt1.setBackground(Color.white);
		bt1.setBounds(25, 25, 30, 30);
		downleft.add(btt);
		downleft.add(bt1);
		
		//設定按鈕特效
		BevelBorder  bb = new BevelBorder(0,Color.gray,Color.white);
		BevelBorder  bb1 = new BevelBorder(1,Color.gray,Color.white);
		
		downleft.setBorder(bb);
		btt.setBorder(bb1);
		bt1.setBorder(bb1);
		
		
		//給新增右面板顏色按鈕
		ButtonListener bl= new ButtonListener(this);//按鈕監聽器
		
		Color colors[] ={Color.BLUE,Color.red,Color.black,Color.ORANGE,Color.gray,
				Color.CYAN,Color.GREEN,Color.YELLOW,Color.PINK,Color.magenta,new Color(234,45,78),new Color(67,123,9)};
		
		for(int i=0;i<12;i++){
			JButton bt3 = new JButton();
			bt3.setBackground(colors[i]);
			bt3.setBorder(bb);
			bt3.setPreferredSize(new Dimension(40,30));
			//給每個按鈕新增按鈕監聽器
			bt3.addActionListener(bl);
			downright.add(bt3);
		}
		
		this.setVisible(true);
		
		//拿到白色畫板的畫筆
	    Graphics g=panelcenterchild.getGraphics();
	    
	    DrawListener dl = new DrawListener(g,bg,this,list);
	    
	    //白色畫板新增
	    panelcenterchild.addMouseListener(dl);
	    panelcenterchild.addMouseMotionListener(dl);
		
	}

}

測試類: