1. 程式人生 > 實用技巧 >重繪完整程式碼

重繪完整程式碼

有關重繪的完整程式碼如下:

package drawFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Graphics;

import javax.swing.JFrame;

public class DrawFrame {
    public static void main(String[] args){
        DrawFrame df = new DrawFrame();
        df.showUI();
    }
    
public void showUI(){ MyFrame jf = new MyFrame(); jf.setSize(800,800); jf.setTitle("畫圖工具"); jf.setLocationRelativeTo(null); jf.setDefaultCloseOperation(3); //新增流式佈局。 FlowLayout flow = new FlowLayout(FlowLayout.LEFT);//設定對齊方式為左對齊,在括號裡面寫。
//上面只是建立這個物件,接下來要set一下。 jf.setLayout(flow); DrawMouse mouse = new DrawMouse(); jf.array = mouse.arr; String a[] = {"直線","圓","矩形","多邊形","三角形","曲線","點","精美的圖形","分形圖形","樹","康托爾集","謝賓斯基三角形"}; Color ch[] = {Color.blue,Color.green};
for(int i=0;i<a.length;i++){ javax.swing.JButton bh = new javax.swing.JButton(a[i]); //新建物件?(新建這個按鈕?) jf.add(bh); //在jf裡面add這個bh bh.addActionListener(mouse); //bh新增動作監聽器 } // javax.swing.JButton jbu6 = new javax.swing.JButton("曲線"); // jf.add(jbu6); for(int j=0;j<ch.length;j++){ javax.swing.JButton s = new javax.swing.JButton(); //新建顏色按鈕? s.setBackground(ch[j]); //新增背景顏色? s.setPreferredSize(new Dimension(30,30)); //設定顏色按鈕的大小 jf.add(s); //jf裡面加這個s s.addActionListener(mouse); //給s新增動作監聽器 } // javax.swing.JButton blue = new javax.swing.JButton(); //設定按鈕背景色 // blue.setBackground(Color.BLUE); // blue.setPreferredSize(new Dimension(30, 30)); // jf.add(blue); // // javax.swing.JButton green = new javax.swing.JButton(); // green.setBackground(Color.GREEN); // green.setPreferredSize(new Dimension(30,30)); // jf.add(green); jf.setVisible(true); //設定可見 Graphics g =jf.getGraphics(); jf.addMouseListener(mouse); mouse.gr = g; jf.addMouseMotionListener(mouse); mouse.gr = g; //上面DrawMouse裡面已經定義了一個mouse物件了,這裡直接用mouse. //因為DrawMouse同時監聽了 MouseListener,ActionListen兩種動作。 // for(int j = 0;j < 6;j++){ // b[j].addActionListener(mouse); // } // jbu.addActionListener(mouse); // green.addActionListener(mouse); } }
package drawFrame;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Random;
import javax.swing.JButton;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;

public class DrawMouse implements MouseListener, ActionListener, MouseMotionListener {
public Graphics gr; private int x1, x2, y1, y2; private int m, n, p, q; String ab; Color color; //剛才這裡遇到了一個問題,在drawShape裡面添加了color引數,但還是用不起來,這是因為,這裡定義的color跟下面的*行的color //不是同一個東西,要怎樣才能是同一個東西呢?很簡單,把*行的Color color = jbu.getBackground();改成 //color = jbu.getBackground();就可以了。 Shape arr[] = new Shape[1000]; int index = 0; public void actionPerformed(ActionEvent e) { if ("".equals(e.getActionCommand())) { JButton jbu = (JButton) e.getSource(); color = jbu.getBackground(); //* gr.setColor(color); }
else { ab = e.getActionCommand(); System.out.println("ab=" + ab); } }

public void mouseDragged(MouseEvent e) { } public void mouseMoved(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { if (flag == 1) { //這是畫三角形多邊形的,與這次重繪無關,不用管 x1 = e.getX(); y1 = e.getY(); } } public void mouseReleased(MouseEvent e) { if (flag == 1) { x2 = e.getX(); y2 = e.getY(); } else if (ab.equals("直線")) { gr.drawLine(x1, y1, x2, y2); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; } else if (ab.equals("圓")) { m = x1 < x2 ? x1 : x2; n = y1 < y2 ? y1 : y2; gr.drawOval(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1)); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; } else if (ab.equals("矩形")) { p = x1 < x2 ? x1 : x2; q = y1 < y2 ? y1 : y2; gr.drawRect(p, q, Math.abs(x2 - x1), Math.abs(y2 - y1)); Shape shape = new Shape(x1,y1,x2,y2,ab,color); arr[index++] = shape; }
}
public void mouseEntered(MouseEvent e) { // System.out.println("進入"); } public void mouseExited(MouseEvent e) { // System.out.println("退出"); } }
package drawFrame;
import java.awt.Graphics;
import javax.swing.JButton;
import java.awt.Color;
public class Shape {
    public int x1,y1,x2,y2;
    String name;
    Color color;
    public int m,n;
    public Shape(int xa,int ya,int xb,int yb,String n,Color co){
        x1 = xa;y1 = ya;
        x2 = xb;y2 = yb;
        name = n;color = co;
    }
    public void drawShape(Graphics g){
        m = x1 < x2 ? x1 : x2;
        n = y1 < y2 ? y1 : y2;
        switch(name){
        case "直線":
            g.setColor(color);
            g.drawLine(x1, y1, x2, y2);
            break;
        case "圓":
            g.setColor(color);
            g.drawOval(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1));
            break;
        case "矩形":
            g.setColor(color);
            g.drawRect(m, n, Math.abs(x2 - x1), Math.abs(y2 - y1));
            break;
        }
    }
}    
package drawFrame;
import java.awt.Graphics;
import javax.swing.JFrame;

public class MyFrame extends JFrame{    //extends這裡是繼承的意思,這裡用了extends,在DrawFrame的showUI裡就可以把JFrame改成MyFrame了
    Shape array[] = null;
    public void paint(Graphics g){
        super.paint(g);
        for(int i = 0;i < array.length;i++){    //array.length表示這個陣列的長度
            Shape shape = array[i];
            if(shape != null){
                shape.drawShape(g);
            }
        }
    }
}