java写的一个画曲线的程序,请高手帮忙看看错在哪里。

importjava.awt.*;importjava.awt.event.MouseEvent;importjava.awt.event.MouseListener;i... import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;
import javax.swing.*;
public class PaintPanel extends JPanel{
Vector list=new Vector();
Point p1;
Point p2;
int x1,y1,x2,y2;/**
* @param args
*/
public PaintPanel()
{
setBackground(Color.white);
setPreferredSize(new Dimension(500,500));
listener l=new listener();
addMouseListener(l);
addMouseMotionListener(l);
}
public void paint(Graphics page)
{super.paint(page);
page.setColor(Color.black);
for(int i=0;i<list.size()-1;i++)
{if(list.get(i)==null)
continue;
p1=(Point)list.elementAt(i);
p2=(Point)list.elementAt(i+1);
page.drawLine(p1.x, p1.y, p2.x, p2.y);
}
super.paintComponent(page);
}
class Point implements Serializable
{int x,y;
Point(int x,int y)
{
this.x=x;
this.y=y;
}
}
class listener implements MouseListener,MouseMotionListener
{
public void mousePressed(MouseEvent e)
{
list.addElement(new Point(e.getPoint().x,e.getPoint().y));
}
public void mouseDragged(MouseEvent e)
{
list.addElement(new Point(e.getPoint().x,e.getPoint().y));
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
list.addElement(null);
repaint();
}
}
public static void main(String[] args) {
JFrame frame=new JFrame();
frame.add(new PaintPanel());
frame.setVisible(true);
frame.pack();
// TODO Auto-generated method stub
}
}

运行后报错:
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1000(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
展开
 我来答
百度网友845f74e61
2013-04-24 · TA获得超过6929个赞
知道大有可为答主
回答量:4050
采纳率:50%
帮助的人:1620万
展开全部

list.addElement(null);  鼠标释放时,添加了null....

super.paintComponent(page); 这一句也不需要,要调用也是在这个方法的最前面调用。

用来保留画面以前的操作。

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.io.Serializable;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class PaintPanel extends JPanel {
Vector list = new Vector();
Point p1;
Point p2;
int x1, y1, x2, y2;
/**
* @param args
*/
public PaintPanel() {
setBackground(Color.white);
setPreferredSize(new Dimension(500, 500));
listener l = new listener();
addMouseListener(l);
addMouseMotionListener(l);
}
public void paint(Graphics page) {
super.paint(page);
page.setColor(Color.black);
for (int i = 0; i < list.size() - 1; i++) {
if (list.get(i) == null)
continue;
p1 = (Point) list.elementAt(i);
p2 = (Point) list.elementAt(i + 1);
page.drawLine(p1.x, p1.y, p2.x, p2.y);
}
// super.paintComponent(page);
}
class Point implements Serializable {
int x, y;
Point(int x, int y) {
this.x = x;
this.y = y;
}
}
class listener implements MouseListener, MouseMotionListener {
public void mousePressed(MouseEvent e) {
list.addElement(new Point(e.getPoint().x, e.getPoint().y));
}
public void mouseDragged(MouseEvent e) {
list.addElement(new Point(e.getPoint().x, e.getPoint().y));
repaint();
}
@Override
public void mouseMoved(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
// list.addElement(null);
repaint();
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new PaintPanel());
frame.setSize(600, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// TODO Auto-generated method stub
}
}

追问

照您的方法改过后,画出的各条曲线之间都会首尾相连,非常有趣。

谢谢解答。

追答
不客气。
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式