有关swing中的鼠标画图问题

Sleeping0804 2009-10-10 12:06:58
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;



public class xt extends JFrame implements ActionListener{

static JPanel jp;
JWindow jw;
JButton jb1,jb2;

static Container cp;
static int x0,y0,x,y;
static boolean flag=false;
static JLabel jl;

xt(){
cp=getContentPane();
cp.setBackground(Color.gray);
cp.setLayout(new FlowLayout(2,0,0));


jw=new JWindow(this);
jw.setSize(600,500);
Container jwcp=jw.getContentPane();

jp=new jj();
jwcp.add(jp);

jb1=new JButton("open"); jb1.addActionListener(this);
jb2=new JButton("close"); jb2.addActionListener(this);

jl=new JLabel("在这里拖动鼠标...",JLabel.CENTER);
jl.setVisible(false);


cp.add(jl);
cp.add(jb1);
cp.add(jb2);

cp.addMouseListener(new m());
cp.addMouseMotionListener(new mm());

setSize(400,300);
setVisible(true);
setLocation(650,550);

addWindowListener(
new WindowAdapter(){
public void windowClosing(WindowEvent we){
System.exit(0);
}
}
);
}

public void actionPerformed(ActionEvent ae){
if(ae.getSource()==jb1){
jw.setVisible(true);
}else if(ae.getSource()==jb2){
jw.setVisible(false);
}
}

public static void main(String in[]){

xt xto=new xt();
}

}


class jj extends JPanel{

public void paintComponent(Graphics g){
if(xt.flag==true)
g.setColor(Color.blue);
else
g.setColor(Color.black);

int left=0;
int top=0;
int wt=0;
int ht=0;

if(xt.x0<xt.x){
left=xt.x0;
wt=xt.x-xt.x0;
}else{
left=xt.x;
wt=xt.x0-xt.x;
}

if(xt.y0<xt.y){
top=xt.y0;
ht=xt.y-xt.y0;
}else{
top=xt.y;
ht=xt.y0-xt.y;
}


g.drawRect(left,top,wt,ht);
}
}


class mm extends MouseMotionAdapter{
public void mouseDragged(MouseEvent me){
xt.x=me.getX();
xt.y=me.getY();
xt.jp.repaint(100l,0,0,800,600); //(1)

}
}

class m extends MouseAdapter{
public void mousePressed(MouseEvent me){
xt.x0=me.getX();
xt.y0=me.getY();
xt.flag=false;
xt.jp.repaint(100l,0,0,800,600); //(2)
}

public void mouseEntered(MouseEvent me){
xt.cp.setBackground(Color.lightGray);
xt.jl.setVisible(true);
}

public void mouseExited(MouseEvent me){
xt.cp.setBackground(Color.gray);
xt.jl.setVisible(false);
}

public void mouseReleased(MouseEvent me){
xt.flag=true;
xt.jp.repaint(100l,0,0,800,600); //(3)
}
}



这段程序是要画一个橡皮筋矩形,
画橡皮筋矩形的关键是:
1.不断地画出新的矩形;
2.不断地清除旧的矩形;

以上这段代码显然没有做到第2点,
因此在窗口上留下了一系列的矩形(乌漆抹黑的效果)。

但同样是这段代码,若改为awt组件,就没有这个问题,
也就是说,只要把上面这段代码中的:
JPanel改为Panel
paintCompoment改为paint
则可以正常地画出橡皮筋矩形。

估计问题出在JComponent和Component
两者的repaint()方法有区别, (应该有区别,因为JComponent重写了这个方法)

我调用repaint()方法的地方分别在代码的(1),(2),(3)处,
请各位帮忙看看问题出在哪里?

期盼着结果...




...全文
131 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
realreachard 2009-10-10
  • 打赏
  • 举报
回复
刚才的不是解决的最好的办法,看了下JComponent的代码,里面会判断 isOpaque()

if (c.isOpaque()) {
g.setColor(c.getBackground());
g.fillRect(0, 0, c.getWidth(),c.getHeight());
}
paint(g, c);

然后调用paint方法, 所以只要把组件设置好opaque就可以了
代码如下:
jp=new jj();
jp.setOpaque(false);
jwcp.add(jp);
realreachard 2009-10-10
  • 打赏
  • 举报
回复
g.clearRect(0,0,getWidth(),getHeight());
g.drawRect(left,top,wt,ht);


自己看看java的源码啊,不清楚你要干什么,加句上面的代码能清了Rect,

还有个问题就是 , 记录的鼠标的坐标没清除
realreachard 2009-10-10
  • 打赏
  • 举报
回复
鼠标点击也会画出一个矩形,
Sleeping0804 2009-10-10
  • 打赏
  • 举报
回复
这个方法可以,
学习了。

[Quote=引用 1 楼 realreachard 的回复:]

还有个问题就是 , 记录的鼠标的坐标没清除
[/Quote]
"坐标没清除"?
这个指的是什么?没明白.

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧