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

📄 drawpanel.java

📁 Java实现的常用数据结构算法
💻 JAVA
字号:
package Graphics;
import java.awt.*;
import java.awt.event.*;
import java.util.*;

public class DrawPanel extends Panel implements MouseListener, MouseMotionListener {
    public static final int LINES = 0;
    public static final int POINTS = 1;
    public static final int VERTEX= 2;
    public static DrawPanel panel=new DrawPanel();
    int	   mode = LINES;
    Vector lines = new Vector();
    Vector colors = new Vector();
    Vector Points= new Vector();
    int x1,y1;
    int x2,y2;

    public DrawPanel() {
	setBackground(Color.white);
	addMouseMotionListener(this);
	addMouseListener(this);
    }

    public void setDrawMode(int mode) {
	switch (mode) {
	  case LINES: this.mode=mode;break;
	  case POINTS:
	    this.mode = mode;
	    break;
	  case VERTEX: this.mode=mode;break;  
	  default:
	    throw new IllegalArgumentException();
	}
    }


    public void mouseDragged(MouseEvent e) {
        e.consume();
        switch (mode) {
            case LINES:
                x2 = e.getX();
                y2 = e.getY();
                break;    
            case POINTS:
            case VERTEX:
            default:
                colors.addElement(getForeground());
               // lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
                x1 = e.getX();
                y1 = e.getY();
                break;
        }
        repaint();
    }

    public void mouseMoved(MouseEvent e) {
    }

    public void mousePressed(MouseEvent e) {
        e.consume();
        switch (mode) {
            case LINES:
                x1 = e.getX();
                y1 = e.getY();
                x2 = -1;
                break;
            case POINTS:break;
            default:
                colors.addElement(getForeground());
                //lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1));
                x1 = e.getX();
                y1 = e.getY();
                repaint();
                break;
        }
    }

    public void mouseReleased(MouseEvent e) {
        e.consume();
        switch (mode) {
            case LINES:
                colors.addElement(getForeground());
                lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));
                x2 = -1;
                break;
            case POINTS:
            default:
                break;
        }
        repaint();
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    	if(this.mode==VERTEX){
    		x1=e.getX();
    		y1=e.getY();
    	Point v=new Point(x1,y1);	
    	Points.addElement(v);
    	}
    	
    }

    public void paint(Graphics g) {
	int np = lines.size();

	// draw the current lines 
	g.setColor(getForeground());
	for (int i=0; i < np; i++) {
	    Rectangle p = (Rectangle)lines.elementAt(i);
	    g.setColor((Color)colors.elementAt(i));
	    if (p.width != -1) {
		g.drawLine(p.x, p.y, p.width, p.height);
	    } else {
		g.drawLine(p.x, p.y, p.x, p.y);
	    }
	}
	if (mode == LINES) {
	    g.setColor(getForeground());
	    if (x2 != -1) {
                g.drawLine(x1, y1, x2, y2);
	    }
	}
	if(mode==VERTEX){
		int len=Points.size();
		Point v;
		for(int i=0;i<len;i++){
			v=(Point)Points.elementAt(i);
			g.drawOval(v.x,v.y,6,6);
		}
		
	}
    }
}

class DrawControls extends Panel implements ItemListener {
    DrawPanel target;
    public DrawControls(DrawPanel target) {
	this.target = target;
	setLayout(new FlowLayout());
	setBackground(Color.lightGray);
	target.setForeground(Color.red);
	CheckboxGroup group = new CheckboxGroup();
	Checkbox b;
	add(b = new Checkbox(null, group, false));
	b.addItemListener(this);
	b.setForeground(Color.red);
	add(b = new Checkbox(null, group, false));
	b.addItemListener(this);
	b.setForeground(Color.green);
	add(b = new Checkbox(null, group, false));
	b.addItemListener(this);
	b.setForeground(Color.blue);
	add(b = new Checkbox(null, group, false));
	b.addItemListener(this);
	b.setForeground(Color.pink);
	add(b = new Checkbox(null, group, false));
	b.addItemListener(this);
	b.setForeground(Color.orange);
	add(b = new Checkbox(null, group, true));
	b.addItemListener(this);
	b.setForeground(Color.black);
	target.setForeground(b.getForeground());
	Choice shapes = new Choice();
	shapes.addItemListener(this);
	shapes.addItem("Lines");
	shapes.addItem("Points");
	shapes.addItem("Vertex");
	shapes.setBackground(Color.lightGray);
	add(shapes);
    }

    public void paint(Graphics g) {
	Rectangle r = getBounds();
	g.setColor(Color.lightGray);
	g.draw3DRect(0, 0, r.width, r.height, false);

        int n = getComponentCount();
        for(int i=0; i<n; i++) {
            Component comp = getComponent(i);
            if (comp instanceof Checkbox) {
                Point loc = comp.getLocation();
                Dimension d = comp.getSize();
                g.setColor(comp.getForeground());
                g.drawRect(loc.x-1, loc.y-1, d.width+1, d.height+1);
            }
        }
    }

  public void itemStateChanged(ItemEvent e) {
    if (e.getSource() instanceof Checkbox) {
      target.setForeground(((Component)e.getSource()).getForeground());
    } else if (e.getSource() instanceof Choice) {
      String choice = (String) e.getItem();
      if (choice.equals("Lines")) {
	target.setDrawMode(DrawPanel.LINES);
      } else if (choice.equals("Points")) {
	target.setDrawMode(DrawPanel.POINTS);
      }else if (choice.equals("Vertex")) {
	target.setDrawMode(DrawPanel.VERTEX);
      }
    }
  }
  
}

⌨️ 快捷键说明

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