package org.vito.draw.temp.test;import java.applet.Applet;import java.awt.BorderLayout;import java.awt.Color;import java.awt.Frame;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;/** * 一个可以写写画画的板子,有修改. * @author vito * */public class Paintcha extends Applet{ /** * */ private static final long serialVersionUID = 4221836743513294553L; MyPanel panel; ShowPanel showPanel; public void init() { setLayout(new BorderLayout()); panel = new MyPanel(); panel.setForeground(Color.RED); showPanel = new ShowPanel(panel); add("North",showPanel); add("Center", panel); } public void destroy() { remove(panel); } public static void main(String args[]) { Paintcha paint = new Paintcha(); Frame frame = new Frame("Ha ha ha ... "); paint.init(); paint.start(); frame.add("Center", paint); frame.setSize(660, 400); frame.setVisible(true); frame.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e){ System.exit(0); } }); }}package org.vito.draw.temp.test;import java.awt.Color;import java.awt.Graphics;import java.awt.Panel;import java.awt.Rectangle;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.util.Vector;/** * 画画面板,有修改. * @author vito * */class MyPanel extends Panel implements MouseListener, MouseMotionListener { /** * */ private static final long serialVersionUID = 9123686144551555715L; //向量储存特征 Vector<Rectangle> lines = new Vector<Rectangle>(); Vector<Color> colors = new Vector<Color>(); //位置 int x1,y1; int x2,y2; public MyPanel() { setBackground(Color.WHITE); addMouseMotionListener(this); addMouseListener(this); } /** * 拖 */ public void mouseDragged(MouseEvent e) { e.consume(); colors.addElement(getForeground()); lines.addElement(new Rectangle(x1, y1, e.getX(), e.getY())); x1 = e.getX(); y1 = e.getY(); repaint(); } /** * 按住鼠标 */ public void mousePressed(MouseEvent e) { e.consume(); colors.addElement(getForeground()); lines.addElement(new Rectangle(e.getX(), e.getY(), -1, -1)); x1 = e.getX(); y1 = e.getY(); repaint(); } //Further ... public void mouseMoved(MouseEvent e) {} public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public void paint(Graphics g) { int points = lines.size(); g.setColor(getForeground()); for (int i=0; i < points; i++) { Rectangle rec = (Rectangle)lines.elementAt(i); g.setColor((Color)colors.elementAt(i)); //画法. if (rec.width != -1) { g.drawLine(rec.x, rec.y, rec.width, rec.height); } else { g.drawLine(rec.x, rec.y, rec.x, rec.y); } } }}package org.vito.draw.temp.test;import java.awt.Button;import java.awt.Label;import java.awt.Panel;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Vector;/** * 显示面板. * @author vito * */class ShowPanel extends Panel implements ActionListener{ /** * */ private static final long serialVersionUID = 6044523251854800419L; MyPanel panel; Button redo; Label showLabel; public ShowPanel(MyPanel panel){ this.panel = panel; showLabel = new Label("可以用鼠标在下面写写画画...点击...拖动"); redo = new Button("Clean ..."); redo.addActionListener(this); add("West",showLabel); add("East",redo); } public void actionPerformed(ActionEvent e) { if(e.getSource() == redo){ panel.lines = new Vector(); //redefine panel.colors = new Vector(); panel.repaint(); } }}
引用第2楼vito于2007-11-04 10:11发表的 :我这个 是响应版主的号召
引用第7楼lsy于2007-11-04 13:11发表的 :这段代码怎么看着这么眼熟