⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shapepanel.java

📁 采用基础Java语言实现的一个类似于Windows中的画图程序。
💻 JAVA
字号:
/*
 * ShapePanel.java
 *
 * Created on 2007年6月15日, 下午4:08
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package huaban;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.util.Vector;
import javax.swing.JPanel;
public class ShapePanel extends JPanel implements MouseListener,MouseMotionListener{
    int x,y;
    int x1,y1;
    int x2,y2;
    int a,b;
    int m1,n1;
    int m2,n2;
    boolean pressed;
    Vector vector;
    quxian quxian1;
    int state;
    Color color1;
    color cl;
    String sr;
    
    /** Creates a new instance of ShapePanel */
    public ShapePanel(color cl){
        pressed = false;
        this.cl = cl;
        color1 = this.getBackground();
        this.setVisible(true);
        x = 0;y = 0;
        x1 = 0; y1 = 0;
        x2 = 0; y2 = 0;
        a = 0; b = 0;
        m1 = 0; m2 = 0;
        n1 = 0; n2 = 0;
        state = 0;
        vector = new Vector();
    }
    public void set(int i){
        state = i;
    }


    public void mouseClicked(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        if(state == 5 && !sr.equals(null)){
            character ch = new character();
            ch.setstring(sr);
            ch.setpoint(x,y);
            ch.setcolor(cl.getstate());
            ch.setstate(5);
            vector.add(ch);
            repaint();
        }
    }

    public void mousePressed(MouseEvent e) {
        x = e.getX();
        y = e.getY();
        x1 = e.getX();
        y1 = e.getY();
        x2 = e.getX();
        y2 = e.getY();
        if(state == 2){
        quxian1 = new quxian();
        quxian1.setpoint(x,y,2);
        }
    }

    public void mouseReleased(MouseEvent e) {
        x2 = e.getX();
        y2 = e.getY();
        if(state == 1){
            zhixian line = new zhixian();
            line.setString(x,y,x2,y2);
            line.setstate(1);
            line.setcolor(cl.getstate());
            vector.add(line);
        }
        if(state == 2){
            quxian1.add(x2,y2);
            quxian1.setcolor(cl.getstate());
            vector.add(quxian1);       
        }
        if(state == 3){
            int m,n;
            yuan circle = new yuan();
            if(x2 > x){
                m = x;
            }
            else{
                m = x2;
            }
            if(y2 > y){
                n = y;
            }
            else{
                n = y2;
            }
            int r = (int)Math.sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y));
            circle.setyuan(m,n,r);
            circle.setstate(3);
            circle.setcolor(cl.getstate());
            vector.add(circle);    
        }
        if(state == 4){
            int m ,n;
            if(x2 - x > 0){
                a = x2 - x;
                m = x;
            }
            else{
                a = x - x2;
                m = x2;
            }
            if(y2 - y > 0){
                b = y2 - y;
                n = y;
            }
            else{
                b = y - y2;
                n = y2;
            }
            juxing ju = new juxing();
            ju.setjuxing(m,n,a,b);
            ju.setstate(4);
            ju.setcolor(cl.getstate());
            vector.add(ju);      
        }
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseDragged(MouseEvent e) {
        x1 = x2;
        y1 = y2;
        x2 = e.getX();
        y2 = e.getY();
        switch(state ){
            case 1 :
                repaint();
                break;
            case 2 :
                quxian1.add(x2,y2);
                repaint();
                break;
            case 3 :
                m1 = m2; n1 = n2;
                if(x2 > x){
                    m2 = x;
                }
                else{
                    m2 = x2;
                }
                if(y2 > y){
                    n2 = y;
                }
                else{
                    n2 = y2;
                }
                a = (int)Math.sqrt((x1 - x)*(x1 - x) + (y1 - y)*(y1 - y)); 
                b = (int)Math.sqrt((x2 - x)*(x2 - x) + (y2 - y)*(y2 - y));
                repaint();
                break;
            case 4 :
                m1 = m2; n1 = n2;
                if(x2 > x){
                    m2 = x;
                }
                else{
                    m2 = x2;
                }
                if(y2 > y){
                    n2 = y;
                }
                else{
                    n2 = y2;
                }
                repaint();
        }
    
    } 

    public void mouseMoved(MouseEvent e) {
    }
    public void paint(Graphics g){
        if(state == 0){
            Draw(g);
        }
        if(state == 1){
            if(pressed){
                Draw(g);
                pressed = false;
            }
            else{
            g.setColor(color1);
            g.drawLine(x,y,x1,y1);
            Draw(g);
            g.setColor(cl.getstate());
            g.drawLine(x,y,x2,y2);
            }           
        }
        if(state == 2){
            if(pressed){
                Draw(g);
                pressed = false;
            }
            else{
            g.setColor(cl.getstate());
            g.drawLine(x1,y1,x2,y2);
            }
        }
        if(state == 3){
            if(pressed ){
                Draw(g);
                pressed = false;
            }
            else{
            g.setColor(color1);
            g.drawOval(m1,n1,a,a);
            Draw(g);
            g.setColor(cl.getstate());
            g.drawOval(m2,n2,b,b);          
            }
        }
        if(state == 4){
            if(pressed){
                Draw(g);
                pressed = false;
            }
            else{
            g.setColor(color1);
            g.drawRect(m1, n1, Math.abs(x1 - x), Math.abs(y1 - y));
            Draw(g);
            g.setColor(cl.getstate());
            g.drawRect(m2, n2, Math.abs(x2 - x), Math.abs(y2 - y)); 
            }
        }
        if(state == 5){
            if(pressed){
                Draw(g);
                pressed = false;
            }
            else{
                Draw(g);
                g.setColor(cl.getstate());
                g.drawString(sr,x,y);
            }
        }
    }
    private void Draw(Graphics h){
        shape shape2;
        zhixian line1;yuan cir;juxing xing;quxian qu;
        h.setColor(Color.RED);
        for(int i = 0; i < vector.size(); i++){
            shape2 = (shape)vector.get(i);
            switch(shape2.state){
                case 1 :
                    line1 = (zhixian)shape2;
                    h.setColor(line1.getcolor());
                    h.drawLine(line1.getdot().getx(),line1.getdot().getY(),line1.getdot1().getx(),line1.getdot1().getY());
                    break;
                case 2 :
                   qu = (quxian)shape2;
                   h.setColor(qu.getcolor());
                    point point3,point2;
                    point2 = qu.getdot();
                    for(int j = 1; j < qu.getVector().size(); j++){
                      point3 = point2;
                      point2 = (point)qu.getVector().get(j);
                      h.drawLine(point2.getx(),point2.getY(),point3.getx(),point3.getY());
                    } 
                    break;
                case 3 :
                    cir = (yuan)shape2;
                    h.setColor(cir.getcolor());
                    h.drawOval(cir.getdot().getx(),cir.getdot().getY(),cir.getR(),cir.getR());
                    break;
                case 4 :
                    xing = (juxing)shape2;
                    h.setColor(xing.getcolor());
                    h.drawRect(xing.getdot().getx(),xing.getdot().getY(),xing.getx1(),xing.gety1());
                    break;  
                case 5 :
                    character chr;
                    chr = (character)shape2;
                    h.setColor(chr.getcolor());
                    h.drawString(chr.getstring(),chr.getdot().getx(),chr.getdot().getY());
                    break;
            }
        } 
    }
    
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -