怎么用鼠标进行画图? 用鼠标事件例子,JavaWebStart中的draw 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 element.java这里主要定义画基本图案的接口画曲线的是内类curvepackage 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; } } 扩充话题老大是21bird不是你21birds这年头马甲泛滥了。----------------------搂住找一个绘图的例子吧。楼上的那个就不错。主要是用Graphics类绘制,用鼠标侦听器得到鼠标坐标。 你先看一下以下代码,不行再问我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"); }} 我这里有个简单的绘图版程序,可以选择颜色画图,双击清除图象http://expert.csdn.net/Expert/topic/1330/1330628.xml?temp=4.870242E-02 主要是捕捉鼠标事件和Graphics的应用 请教Lucene的检索机制 程序可读性 如何去掉applet弹出窗口下面的提示“Java Applet Window" 那位兄台能指点下 那个网站能大量下载好的JAVA源程序 ==和equals() 简单问题,怎样退出一个方法 想学java,大侠们指点一二,省得偶走弯路! java写文件问题 static问题??? Application的参数在jdk1.3的命令行下如何设定? 关于包(及其与classpath的关系)的迷惑? jbuilder怎么生成可执行文件?请高手指点,高分等待!
这里主要定义画基本图案的接口
画曲线的是内类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;
} }
主要是用Graphics类绘制,用鼠标侦听器得到鼠标坐标。
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");
}
}
http://expert.csdn.net/Expert/topic/1330/1330628.xml?temp=4.870242E-02