java,怎样清除已画图形?

下面的程序运行后,鼠标拖动画出的直线不会消失,坐标也是重叠的。有人说重画清屏,能不能给个代码?谢了!importjava.awt.*;importjava.awt.eve... 下面的程序运行后,鼠标拖动画出的直线不会消失,坐标也是重叠的。有人说重画清屏,能不能给个代码?谢了!
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseDemo3 extends JFrame implements MouseListener, MouseMotionListener
{
int flag;
int x = 0;
int y = 0;
int startx, starty, endx, endy;

public MouseDemo3(){
Container contentPane = getContentPane();
contentPane.addMouseListener( this );
contentPane.addMouseMotionListener( this );
setSize( 300, 300 );
//show(); 已过时的API,用setVisible代替
setVisible(true);
addWindowListener( new WindowAdapter(){
public void windowClosing( WindowEvent e ){
System.exit( 0 );
}});
}

public void mousePressed( MouseEvent e ){
startx = e.getX();
starty = e.getY();
}

public void mouseReleased( MouseEvent e ){
endx = e.getX();
endy = e.getY();
}

public void mouseEntered( MouseEvent e ){
}

public void mouseExited( MouseEvent e ){
}

public void mouseClicked( MouseEvent e ){
}

public void mouseMoved( MouseEvent e ){
flag = 1;
x = e.getX();
y = e.getY();
repaint();
}

public void mouseDragged( MouseEvent e ){
flag = 2;
x = e.getX();
y = e.getY();
repaint();
}

public void update( Graphics g ){
g.setColor( this.getBackground() );
g.fillRect( 0, 0, getWidth(), getHeight() );
paint( g );
}

public void paint( Graphics g ){
g.setColor( Color.black );
if( flag == 1 ){
g.drawString("鼠标坐标:("+x+","+y+")", 10, 50 );
g.drawLine( startx, starty, endx, endy );
}
if( flag == 2 ){
g.drawString("拖拽鼠标坐标:("+x+","+y+")", 10, 50 );
g.drawLine( startx, starty, x, y );
}
}

public static void main(String[] args)
{
new MouseDemo3();
}
}
展开
 我来答
skyyks0001
推荐于2017-11-24 · TA获得超过430个赞
知道小有建树答主
回答量:228
采纳率:100%
帮助的人:396万
展开全部
尽量不要重写JFrame的paint方法,会有各种问题,JFrame中的所有内容实际上都包含在contentPane里,所以定制contentPane是更好的办法

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MouseDemo3 extends JFrame implements MouseListener, MouseMotionListener {
int flag;
int x = 0;
int y = 0;
int startx, starty, endx, endy;

public MouseDemo3() {
//将contentPane修改为自己的Panel,而不是使用JFrame自带的
JPanel contentPane = new PaintPanel();
contentPane.addMouseListener(this);
contentPane.addMouseMotionListener(this);
setContentPane(contentPane);

setSize(300, 300);
// show(); 已过时的API,用setVisible代替
setVisible(true);
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}

public void mousePressed(MouseEvent e) {
startx = e.getX();
starty = e.getY();
}

public void mouseReleased(MouseEvent e) {
endx = e.getX();
endy = e.getY();
}

public void mouseEntered(MouseEvent e) {
}

public void mouseExited(MouseEvent e) {
}

public void mouseClicked(MouseEvent e) {
}

public void mouseMoved(MouseEvent e) {
flag = 1;
x = e.getX();
y = e.getY();
repaint();
}

public void mouseDragged(MouseEvent e) {
flag = 2;
x = e.getX();
y = e.getY();
repaint();
}

public void update(Graphics g) {
g.setColor(this.getBackground());
g.fillRect(0, 0, getWidth(), getHeight());
paint(g);
}
//继承JPanel,并重写paintComponent而不是paint方法
//重写paint方法会导致该容器中的其他组件无法绘制
private class PaintPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
g.setColor(Color.black);
if (flag == 1) {
g.drawString("鼠标坐标:(" + x + "," + y + ")", 10, 50);
g.drawLine(startx, starty, endx, endy);
}
if (flag == 2) {
g.drawString("拖拽鼠标坐标:(" + x + "," + y + ")", 10, 50);
g.drawLine(startx, starty, x, y);
}
}
}

public static void main(String[] args) {
new MouseDemo3();
}
}
小厨咖
2019-12-19
知道答主
回答量:13
采纳率:100%
帮助的人:2.1万
展开全部
update方法最后一行paint( g );删了 改成 g.setColor(Color.black);
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
刘维毅
2011-08-29
知道答主
回答量:32
采纳率:0%
帮助的人:17.4万
展开全部
public void update(Graphics g)Calls paint. Doesn't clear the background but see ComponentUI.update, which is called by paintComponent.
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

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

类别

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

说明

0/200

提交
取消

辅 助

模 式