用鼠标事件
例子,JavaWebStart中的draw

解决方案 »

  1.   

    element.java
    这里主要定义画基本图案的接口
    画曲线的是内类curve
    package jtest.geom;import java.awt.*;
    import java.awt.geom.*;public abstract class Element {  protected Color color;  public Element(Color color) {
        this.color = color;
      }  public Color getColor() {
        return color;
      }  //public abstract Shape getShape();  public abstract java.awt.Rectangle getBounds();  public abstract void modify(Point start, Point last);  public abstract void draw(Graphics2D g2d);
    public static class Curve
          extends Element {    private GeneralPath curve;    public Curve(Point start, Point next, Color color) {
          super(color);
          curve = new GeneralPath();
          curve.moveTo(start.x, start.y);
          curve.lineTo(next.x, next.y);
        }    public Shape getShape() {
          return curve;
        }    public java.awt.Rectangle getBounds() {
          return curve.getBounds();
        }    public void modify(Point start, Point next) {
          curve.lineTo(next.x, next.y);
        }    public void draw(Graphics2D g2d) {
          g2d.setPaint(color);
          g2d.draw(curve);
        }
      }鼠标事件部分代码 class MouseHandler
          extends MouseInputAdapter {    private Point start;
        private Point last;
        private Element tempElement;
        private Graphics2D g2d;    public void mouseClicked(MouseEvent e) {
          int modifier = e.getModifiers();
          if ( (modifier & e.BUTTON1_MASK) != 0 &&
              (theApp.getWindow().getElementType() == TEXT)) {        if(e.getClickCount() ==2) {
              //do Sth
              start = e.getPoint();
              String text = JOptionPane.showInputDialog( (Component) e.getSource(),
                  "Enter Text:", "Text input",
                  JOptionPane.PLAIN_MESSAGE);
              if (text != null) {
                g2d = (Graphics2D) getGraphics();
                Font font = theApp.getWindow().getCurrentFont();            tempElement = new Element.Text(font, text, start,
                                               theApp.getWindow().getElementColor(),
                                               font.getStringBounds(text,
                    g2d.getFontRenderContext()).getBounds());
                if (tempElement != null) {
                  theApp.getModel().add(tempElement);
                }
                tempElement = null;
                g2d.dispose();
                g2d = null;
                start = null;            //JOptionPane.showMessageDialog(theApp.getWindow(),text,"About Ali",JOptionPane.INFORMATION_MESSAGE);
              }
            }
          }
        }    public void mousePressed(MouseEvent e) {
          start = e.getPoint();
          int modifier = e.getModifiers();
          if ( (modifier & e.BUTTON1_MASK) != 0 && (theApp.getWindow().getElementType() != TEXT)) {
            g2d = (Graphics2D) getGraphics();
            if (theApp.getWindow().getElementType() == CURVE) {
              g2d.setPaintMode();
            }
            else {
              g2d.setXORMode(getBackground());
            }
            g2d.setPaint(theApp.getWindow().getElementColor());
          }
        }    public void mouseReleased(MouseEvent e) {
          int modifier = e.getModifiers();
          if ( (modifier & e.BUTTON1_MASK) != 0) {
            if (tempElement != null) {
              theApp.getModel().add(tempElement);
              tempElement = null;
            }
            if (g2d != null) {
              g2d.dispose();
              g2d = null;
            }
            start = last = null;
          }
        }    public void mouseDragged(MouseEvent e) {
          last = e.getPoint();
          int modifier = e.getModifiers();
          if ( (modifier & e.BUTTON1_MASK) != 0 && (theApp.getWindow().getElementType() != TEXT)) {
            if (tempElement == null) {
              tempElement = createElement(start, last);
            }
            else {
              //g2d.draw(tempElement.getShape());
              tempElement.draw(g2d);
              tempElement.modify(start, last);
            }
            //g2d.draw(tempElement.getShape());
            tempElement.draw(g2d);
          }
        }    private Element createElement(Point start, Point end) {
          switch (theApp.getWindow().getElementType()) {
            case LINE:
              return new Element.Line(start, end,
                                      theApp.getWindow().getElementColor());
            case RECTANGLE:
              return new Element.Rectangle(start, end,
                                           theApp.getWindow().getElementColor());
            case CIRCLE:
              return new Element.Circle(start, end,
                                        theApp.getWindow().getElementColor());
            case CURVE:
              return new Element.Curve(start, end,
                                       theApp.getWindow().getElementColor());
          }
          return null;
        }  }
      

  2.   

    扩充话题老大是21bird不是你21birds这年头马甲泛滥了。----------------------搂住找一个绘图的例子吧。楼上的那个就不错。
    主要是用Graphics类绘制,用鼠标侦听器得到鼠标坐标。
      

  3.   

    你先看一下以下代码,不行再问我import java.awt.*;
    import java.awt.event.*;
    import java.util.*;public class Test extends Frame
    {
       public static void main(String[] args)
       {
          new Test();
       }
       public Test()
       {
          this.addWindowListener(new WindowAdapter(){public void windowClosing(WindowEvent e){dispose();System.exit(0);}});
          this.setSize(300,300);
          this.setLocation(300,0);
          this.add(new Draw());
          this.show();
       }
       protected void processWindowEvent(WindowEvent event)
       {
          super.processWindowEvent(event);
          if(event.getID() == WindowEvent.WINDOW_CLOSING)
          {
             System.exit(0);
          }
       }
    }
    class Draw extends Canvas
    {
       int x1=-1;
       int y1=-1;
       int x2=-1;
       int y2=-1;
       public Draw()
       {
          this.setBackground(Color.white);
          this.addMouseListener(new MyMouseListener(this));
          this.addMouseMotionListener(new MyMouseMotionListener(this));
       }
       public void paint(Graphics g)
       {
          if(x1==-1 || y1==-1)
          {
             x1=x2;
             y1=y2;
          }
          g.drawLine(x1,y1,x2,y2);
          x1=x2;
          y1=y2;
       }
       public void update(Graphics g)
       {
          this.paint(g);
       }
    }
    class MyMouseListener implements MouseListener
    {
       Draw draw;
       public MyMouseListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseClicked(MouseEvent e)
       {
          //System.out.println("mouseClicked");
       }
       public void mouseEntered(MouseEvent e)
       {
          //System.out.println("mouseEntered");
       }
       public void mouseExited(MouseEvent e)
       {
          //System.out.println("mouseExited");
       }
       public void mousePressed(MouseEvent e)
       {
          //System.out.println("mousePressed");
          draw.x1=-1;
          draw.y1=-1;
       }
       public void mouseReleased(MouseEvent e)
       {
          //System.out.println("mouseReleased");
          draw.x1=draw.x2;
          draw.y1=draw.y2;
       }
    }
    class MyMouseMotionListener implements MouseMotionListener
    {
       Draw draw;
       public MyMouseMotionListener(Draw draw)
       {
          this.draw=draw;
       }
       public void mouseDragged(MouseEvent e)
       {
          //System.out.println("mouseDragged");
          draw.x2=e.getX();
          draw.y2=e.getY();
          draw.repaint();
       }
       public void mouseMoved(MouseEvent e)
       {
          //System.out.println("mouseMoved");
       }
    }
      

  4.   

    我这里有个简单的绘图版程序,可以选择颜色画图,双击清除图象
    http://expert.csdn.net/Expert/topic/1330/1330628.xml?temp=4.870242E-02
      

  5.   

    主要是捕捉鼠标事件和Graphics的应用