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

📄 drawtest.java

📁 这是一个java程序
💻 JAVA
字号:
import java.awt.*;import java.awt.event.*;import java.applet.*;import java.util.Vector;public class DrawTest extends Applet{    DrawPanel panel;    DrawControls controls;    public void init() {		setLayout(new BorderLayout());		panel = new DrawPanel();		controls = new DrawControls(panel);		add("Center", panel);		add("North",controls);	}    public static void main(String args[]) {		Frame f = new Frame("DrawTest");		DrawTest drawTest = new DrawTest();		drawTest.init();		drawTest.start();			f.add("Center", drawTest);		f.addWindowListener(new WindowAdapter(){			public void windowClosing(WindowEvent e){				System.exit(1);				}		});		f.setSize(400, 250);		f.show();    }    public String getAppletInfo() {        return "A simple drawing program.";    }}class DrawPanel extends Panel implements MouseListener, MouseMotionListener {    public static final int LINES = 0;    public static final int POINTS = 1;    public static final int RECTANGLES = 2;    public static final int OVALS = 3;        int mode = LINES;    int x1,y1;    int x2,y2;        Vector lines = new Vector();    Vector rectangles = new Vector();    Vector ovals = new Vector();       Vector Lcolors = new Vector();    Vector Rcolors = new Vector();    Vector Ocolors = new Vector();     public DrawPanel() {		setBackground(Color.white);		addMouseMotionListener(this);		addMouseListener(this);    }    public void setDrawMode(int mode) {		switch (mode) {		  case LINES:		  case POINTS:		  case RECTANGLES:		  case OVALS:		    this.mode = mode;		    break;		  default:		    throw new IllegalArgumentException();		}    }    public void mouseDragged(MouseEvent e) {        e.consume();        switch (mode) {            case LINES:		  	case RECTANGLES:		  	case OVALS:                            x2 = e.getX();                y2 = e.getY();                break;            case POINTS:            default:                Lcolors.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:            case RECTANGLES:		  	case OVALS:                x1 = e.getX();                y1 = e.getY();                x2 = -1;                break;            case POINTS:            default:                Lcolors.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:                Lcolors.addElement(getForeground());                lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));                x2 = -1;                break;                			case RECTANGLES:                Rcolors.addElement(getForeground());                rectangles.addElement(new Rectangle(x1, y1, e.getX(), e.getY()));                x2 = -1;                break;                					  	case OVALS:                Ocolors.addElement(getForeground());                ovals.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) { }    public void paint(Graphics g) {		int np = lines.size();			for (int i=0; i < np; i++) {		    Rectangle p = (Rectangle)lines.elementAt(i);		    g.setColor((Color)Lcolors.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);		    }		}				np = rectangles.size();		for (int i=0; i < np; i++) {		    Rectangle p = (Rectangle)rectangles.elementAt(i);		    g.setColor((Color)Rcolors.elementAt(i));		    if (p.width != -1) {				g.drawRect(p.x, p.y, p.width, p.height);		    }		}				np = ovals.size();		for (int i=0; i < np; i++) {		    Rectangle p = (Rectangle)ovals.elementAt(i);		    g.setColor((Color)Ocolors.elementAt(i));		    if (p.width != -1) {				g.drawOval(p.x, p.y, p.width, p.height);		    }		}						g.setColor(getForeground());				switch(mode){			case LINES:			    if (x2 != -1) {		    	g.drawLine(x1, y1, x2, y2);			    }			    break;			case RECTANGLES:			    if (x2 != -1) {		    	g.drawRect(x1, y1, x2, y2);			    }			    			    break;			case OVALS:			    if (x2 != -1) {		    	g.drawOval(x1, y1, x2, y2);			    }			    			    break;					}    }}class DrawControls extends Panel 	implements ItemListener,MouseListener{	DrawPanel target;		public DrawControls(DrawPanel target) {		this.target = target;		setLayout(new FlowLayout());		setBackground(Color.lightGray);		target.setForeground(Color.red);					Panel pcolor = new Panel();		pcolor.setLayout(new GridLayout(1,8));		Label l;				pcolor.add(l = new Label());		l.setBackground(Color.red);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.green);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.blue);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.yellow);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.pink);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.orange);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.cyan);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.magenta);		l.addMouseListener(this);				pcolor.add(l = new Label());		l.setBackground(Color.gray);		l.addMouseListener(this);								pcolor.add(l = new Label());		l.setBackground(Color.black);		l.addMouseListener(this);										add(pcolor);					Choice shapes = new Choice();		shapes.addItemListener(this);		shapes.addItem("Lines");		shapes.addItem("Points");		shapes.addItem("Rectangles");		shapes.addItem("Ovals");		shapes.setBackground(Color.lightGray);		add(shapes);    }	public void itemStateChanged(ItemEvent e) {		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("Rectangles")) {				target.setDrawMode(DrawPanel.RECTANGLES);			}else if (choice.equals("Ovals")) {				target.setDrawMode(DrawPanel.OVALS);			}		}	}		public void mouseEntered(MouseEvent e) { }		public void mouseExited(MouseEvent e) {	}	public void mouseMoved(MouseEvent e) { }	public void mousePressed(MouseEvent e) { }	public void mouseClicked(MouseEvent e) { 		if (e.getSource() instanceof Label) {			target.setForeground(((Component)e.getSource()).getBackground());		}		}	public void mouseReleased(MouseEvent e) { }		}

⌨️ 快捷键说明

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