怎么样刷新jpanel,使得jpanel上画的图形全部去掉
解决方案 »
- Java中的数组相等问题
- DispatchAction 中使用 synchronized 没有实现同步
- 今天面试,面试官出了这么一道题目
- socket通信程序中的问题
- 请问jEditorPane如何知道用户正在看第几行?
- 求助:自定义TreeModel中无法插入节点
- 请教string数组的排序问题!
- 帮忙查错,读取binary image data的ImageReader
- String如何转换成int?
- 有一个关于Win2000,Weblogic,Java,Struts,Oracle9i的项目,急切寻求开发高手,鼎力协助!!只要求能够快速操作的,有意合作者,请速与我
- java 连接oracle问题
- 跨线程操作哈希表的问题
jpanel.rePaint();
jpanel.invalidate();
jpanel.validate();
如果有透明效果的,就不用这样了。设置了透明属性的组件会自己清除的
private static mypanel mypanel = new mypanel(); public static void main(String[] args) {
JFrame myframe = new JFrame();
myframe.add(mypanel, BorderLayout.CENTER);
myframe.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
myframe.setVisible(true);
myframe.pack();
}
}class mypanel extends JPanel { private Path2D path = new Path2D.Double();
private mypanel self = null; mypanel() {
self = this; this.setPreferredSize(new Dimension(800, 700));
this.setVisible(true);
mymouseListener mymouse = new mymouseListener(this, path);
this.addMouseListener(mymouse);
this.addMouseMotionListener(mymouse); } public void paintComponent(Graphics g) {
Graphics2D ar = (Graphics2D) g;
ar.draw(path); }}class mymouseListener implements MouseListener, MouseMotionListener { private JPanel myComponent = null;
private Path2D mypath = null; boolean isinit = true;
boolean isok = true; mymouseListener(JPanel mcom, Path2D mpath) {
myComponent = mcom;
mypath = mpath;
// point = new Point(); } @Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
isinit = false;
isok = !isok;
if (!isok) {
mypath.reset();
mypath.moveTo(e.getX(), e.getY()); } else {
myComponent.repaint();
} } @Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub } @Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
if (!isinit && !isok) {
mypath.lineTo(e.getX(), e.getY());
myComponent.validate();
myComponent.revalidate();
myComponent.repaint(); }
}}