java通过鼠标事件绘制图形,绘制第二个时怎么让之前的不消失
java通过鼠标事件绘制图形,绘制第二个时用什么方法让之前绘的图形依然保持在面板上?classPanelMextendsJPanel{ImageIconicon=newI...
java通过鼠标事件绘制图形,绘制第二个时用什么方法让之前绘的图形依然保持在面板上?
class PanelM extends JPanel
{
ImageIcon icon = new ImageIcon("app.gif");
JLabel jl = new JLabel(icon,SwingConstants.CENTER);
int x,y;
boolean boo = false;
public PanelM()
{
add(jl);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
int mods = m.getModifiers();
if((mods & InputEvent.BUTTON1_MASK) != 0)
{
x = m.getX();
y = m.getY();
repaint();
boo = true;
}
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(boo)
g.fillOval(x, y, 20, 20);
}
} 展开
class PanelM extends JPanel
{
ImageIcon icon = new ImageIcon("app.gif");
JLabel jl = new JLabel(icon,SwingConstants.CENTER);
int x,y;
boolean boo = false;
public PanelM()
{
add(jl);
addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent m)
{
int mods = m.getModifiers();
if((mods & InputEvent.BUTTON1_MASK) != 0)
{
x = m.getX();
y = m.getY();
repaint();
boo = true;
}
}
});
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
if(boo)
g.fillOval(x, y, 20, 20);
}
} 展开
1个回答
展开全部
public void paintComponent(Graphics g)
{
if(boo)
g.fillOval(x, y, 20, 20);
}
paintComponent方法默认清屏,试着去掉super.paintComponent(g);
{
if(boo)
g.fillOval(x, y, 20, 20);
}
paintComponent方法默认清屏,试着去掉super.paintComponent(g);
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |