当前位置: 代码迷 >> Java相关 >> [求助]如何保留以前所画的内容,应该怎样实现?
  详细解决方案

[求助]如何保留以前所画的内容,应该怎样实现?

热度:262   发布时间:2006-06-18 02:36:05.0
[求助]如何保留以前所画的内容,应该怎样实现?

下面的这段代码只能画一个圆,后面画的圆将会清除前面的图形。我想保留以前所画的图形应该怎样实现?
望务位高手给一些建议,小弟不胜感激!

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

class SelfContainedPanel extends JPanel {
private int x1, y1, x2, y2;

// set up mouse event handling for SelfContainedPanel
public SelfContainedPanel()
{
// set up mouse listener
addMouseListener(

new MouseAdapter() { // anonymous inner class

// handle mouse press event
public void mousePressed( MouseEvent event )
{
x1 = event.getX();
y1 = event.getY();
}

// handle mouse release event
public void mouseReleased( MouseEvent event )
{
x2 = event.getX();
y2 = event.getY();
repaint();
}

} // end anonymous inner class

); // end call to addMouseListener

// set up mouse motion listener
addMouseMotionListener(

new MouseMotionAdapter() { // anonymous inner class

// handle mouse drag event
public void mouseDragged( MouseEvent event )
{
x2 = event.getX();
y2 = event.getY();
repaint();
}

} // end anonymous inner class

); // end call to addMouseMotionListener

} // end constructor SelfContainedPanel

// return preferred width and height of SelfContainedPanel
public Dimension getPreferredSize()
{
return new Dimension( 150, 100 );
}

// paint an oval at the specified coordinates
public void paintComponent( Graphics g )
{
super.paintComponent( g );

g.drawOval( Math.min( x1, x2 ), Math.min( y1, y2 ),
Math.abs( x1 - x2 ), Math.abs( y1 - y2 ) );

}

} // end class SelfContainedPanel

public class SelfContainedPanelTest extends JFrame {
private SelfContainedPanel myPanel;

// set up GUI and mouse motion event handlers for application window
public SelfContainedPanelTest()
{
// set up a SelfContainedPanel
myPanel = new SelfContainedPanel();
myPanel.setBackground( Color.YELLOW );

Container container = getContentPane();
container.setLayout( new FlowLayout() );
container.add( myPanel );

// set up mouse motion event handling
addMouseMotionListener(

new MouseMotionListener() { // anonymous inner class

// handle mouse drag event
public void mouseDragged( MouseEvent event )
{
setTitle( "Dragging: x=" + event.getX() +
"; y=" + event.getY() );
}

// handle mouse move event
public void mouseMoved( MouseEvent event )
{
setTitle( "Moving: x=" + event.getX() +
"; y=" + event.getY() );
}

} // end anonymous inner class

); // end call to addMouseMotionListener

setSize( 300, 200 );
setVisible( true );

} // end constructor SelfContainedPanelTest

public static void main( String args[] )
{
SelfContainedPanelTest application = new SelfContainedPanelTest();
application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

} // end class SelfContainedPanelTest

搜索更多相关的解决方案: import  awt  java  图形  event  

----------------解决方案--------------------------------------------------------
要不你就不要实时的画出来,在鼠标释放的时候再画出来,然后把super.paintComponent()去掉
就可以一直保存以前画的东西了
----------------解决方案--------------------------------------------------------

版主所说的那个方法我也试过了!继承canvas就可以解决,
不过那样又带来好多预想不到的效果,除了版主所说的那个方法外,就没有其他的法子了?


----------------解决方案--------------------------------------------------------

要不就把你画过的东西存在一个数组或者什么里面.每调用一次paint的时候,就先清空所有,然后从里面取曾经画的东西,


----------------解决方案--------------------------------------------------------
调用画布的setOpaque( true )方法 设置成组键透明。
这样在重画时将不会清处背景.
----------------解决方案--------------------------------------------------------
我把以前所画的内容放在两个数组上,可以实现了保留以前所画的内容,不过不能单独改变其大小,颜色!
就是说,你改变一条线的大小,以前的线也都变成当前的大小
----------------解决方案--------------------------------------------------------
  相关解决方案