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) 展开
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) 展开
1个回答
展开全部
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
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询