import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class TestMouseLine extends JFrame
{
int x,y;
int endX,endY;
public TestMouseLine()
{
Container c=getContentPane();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(400,300);
setLocation(400,300);
show();
}
public void paint(Graphics g)
{
g.setColor(Color.red);
g.drawLine(x,y,endX,endY);
}
public static void main(String[] args)
{
new TestMouseLine();
}
}
为什么用鼠标画的时候有很多线呢?怎么样才能只有一跟线随着鼠标转呢?
----------------解决方案--------------------------------------------------------
import java.util.*;
import java.awt.*;
import java.awt.Point;
import java.awt.event.*;
import javax.swing.*;
import java.applet.*;
import javax.swing.event.*;
import java.awt.geom.*;
import java.io.*;
public class MyCanvas extends JPanel
{
private Point point; //坐标
private Point point2;
private ArrayList image;
private ArrayList old;
public MyCanvas() {
image = new ArrayList();
old = new ArrayList();
addMouseListener(new MouseHandle());
addMouseMotionListener(new MouseMotionHandle());
}
public void drawImage()
{
Line2D line1 = new Line2D.Double(point.x, point.y, point2.x, point2.y);
image.clear();
image.add(line1);
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
g2.setPaint(Color.red);
for (int i = 0; i < image.size(); i++) {
Shape s = (Shape)image.get(i);
g2.draw(s);
}
for (int i = 0; i < old.size(); i++) {
Shape s = (Shape)old.get(i);
g2.draw(s);
}
}
private class MouseHandle extends MouseAdapter
{
public void mouseClicked(MouseEvent event){
}
public void mousePressed(MouseEvent event)
{
point = event.getPoint(); //起始坐标
}
public void mouseReleased(MouseEvent event)
{
try
{
old.add(old.size(), image.get(0));
}
catch (IndexOutOfBoundsException e) {}
}
}
private class MouseMotionHandle implements MouseMotionListener
{
public void mouseMoved(MouseEvent event) {}
public void mouseDragged(MouseEvent event)
{
try {
point2 = event.getPoint();
drawImage();
} catch (NullPointerException e) {}
}
}
}
class PaintBoardFrame extends JFrame
{
MyCanvas canvas;
Container container;
public PaintBoardFrame()
{
container = this.getContentPane(); //获得窗口容器
canvas=new MyCanvas();
canvas.setBackground(Color.WHITE);
container.add(canvas);//增加画布
setTitle("简单小画板");//画板的标题
setSize(new Dimension(800, 600));//画板的大小
}
}
class PaintBoard {
public static void main(String[] args) {
// Create application frame.
PaintBoardFrame frame = new PaintBoardFrame();
frame.setVisible(true);
}
}
----------------解决方案--------------------------------------------------------
对了,上面的程序我忘了添加关闭处理的代码,楼主你就自己加了!
----------------解决方案--------------------------------------------------------
super.paintComponent(g);这句话是什么意思呢?我不太明白.super关键字是想父类传递数据的.在这里为什么这么用呢?我想不太明白,请教!
----------------解决方案--------------------------------------------------------
就是缓冲区被冲掉,然后每次paint的时候都重建缓冲区
如果不对 下面继续
----------------解决方案--------------------------------------------------------
我觉得不太是啊.在这段代码里,清空多余的线是:image.clear();这句代码.
所以要重新请教下xuyijin.....
----------------解决方案--------------------------------------------------------
我这个程序是创建一个定制的绘图区域。
在将定制的JPanel用作专门的绘图区域时,JComponent的子类应重写paintComponent方法,并且在重写的方法体中,第一条语句应该调用paintComponent方法的超类版本。这样能确保以正确的次序进行绘图。还有,当组件是透明组件时,在程序绘制该组件时,paintComponent方法将不会清除该组件的背景,否则,当组件是不透明组件时,paintComponent方法将在进行绘制操作之前会清除该组件的背景。如果没有调用paintComponent方法的超类版本,则不透明的组件将不正确的显示在用户界面上。此外,如果在定制绘制语句之后调用该方法的超类版本,则会擦除绘制的结果。
在这个程序中,如果没有用for语句,那么调用paintComponent方法的超类版本,一次只能画一条线,后面画的线会先清除前面的线!楼主可以试试看!
----------------解决方案--------------------------------------------------------
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.util.*;
public class TestMouseLine extends JFrame
{
int x,y;
int endX,endY;
ArrayList list;
public TestMouseLine()
{
Container c=getContentPane();
list=new ArrayList();
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e)
{
x=e.getX();
y=e.getY();
}
});
addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent e)
{
endX = e.getX();
endY = e.getY();
repaint();
}
});
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
setSize(400,300);
setLocation(400,300);
show();
}
public void paint(Graphics g)
{
super.paint(g);
list.add(new MyLine(x,y,endX,endY));
Iterator it=list.iterator();
while(it.hasNext())
{
MyLine line=(MyLine)it.next();
line.drawMe(g);
}
}
public static void main(String[] args)
{
new TestMouseLine();
}
}
class MyLine
{
private int x,y;
private int endX,endY;
public MyLine(int x,int y,int endX,int endY)
{
this.x = x;
this.y = y;
this.endX = endX;
this.endY = endY;
}
public void drawMe(Graphics g)
{
g.setColor(Color.RED);
g.drawLine(x,y,endX,endY);
}
};
我重新改了之后的代码,为什么还是有那个问题啊?就是有很多线跟着鼠标转.......
----------------解决方案--------------------------------------------------------