Java 如何使我的clearButton撤消所有操作
我画了一些形状,我想清理我的屏幕从他们。所以我做了一个按钮,叫它clearButton,在我的actionPreformed中我清洗了它们。一点也不干净。所以我想要的是我的清洁按钮来清洁我画的东西。只要看看“清洁”按钮,看看我做了什么使它干净。它很长,但我必须张贴整个事情,以便它更清楚。忽略它,只看cleanButton在做什么。谢谢 主要类别:Java 如何使我的clearButton撤消所有操作,java,swing,graphics,Java,Swing,Graphics,我画了一些形状,我想清理我的屏幕从他们。所以我做了一个按钮,叫它clearButton,在我的actionPreformed中我清洗了它们。一点也不干净。所以我想要的是我的清洁按钮来清洁我画的东西。只要看看“清洁”按钮,看看我做了什么使它干净。它很长,但我必须张贴整个事情,以便它更清楚。忽略它,只看cleanButton在做什么。谢谢 主要类别: package kal; import java.awt.*; import java.awt.event.*; import java.util.
package kal;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import kal.Box;
public class Kal extends JFrame implements MouseListener, MouseMotionListener,
ActionListener
{
int x=0, y=0; // last mouse location
int pressx, pressy; // coords where the mouse is pressed
JButton boxButton; // press this to put program in box-drawing mode
JButton ovalButton;
JButton triButton;
JButton clearButton;
int mode = 0; // 0=ovals, 1=boxes, 2=triangles .....
Oval theOval;
Box theBox;
Triangle theTriangle;
Shape theShape;
ColorPicker3 theColorPicker;
public static void main( String[] args )
{
System.out.println("hi there.");
new Kal();
}
// constructor, add MouseListener and MouseMotionListener
public Kal()
{
setDefaultCloseOperation( EXIT_ON_CLOSE );
setLayout( new FlowLayout() );
boxButton = new JButton("box");
add(boxButton);
boxButton.addActionListener(this);
ovalButton = new JButton("oval");
add(ovalButton);
ovalButton.addActionListener(this);
triButton = new JButton("Triangle");
add(triButton);
triButton.addActionListener(this);
clearButton = new JButton("Clear");
add(clearButton);
clearButton.addActionListener(this);
addMouseListener(this);
addMouseMotionListener(this);
setSize( 500,500);
setVisible( true);
theColorPicker = new ColorPicker3();
}
// returns a random color
public Color randomColor()
{
int red = (int)(Math.random()*255);
int green = (int)(Math.random()*255);
int blue = (int)(Math.random()*255);
return new Color(red,green,blue);
}
// note position of mouse xy globally, when clicked
public void mouseClicked( MouseEvent e )
{
x = e.getX(); y = e.getY();
System.out.println("click at x="+x+" y="+y);
}
public void mouseEntered( MouseEvent e ) {}
// record where the mouse gets pressed
public void mousePressed( MouseEvent e )
{
x = pressx = e.getX(); y = pressy = e.getY();
Point p0 = new Point( x, y );
Point p1 = new Point( pressx, pressy );
if ( mode==0 ) { theShape = theOval = new Oval( p0, p1, randomColor() ); }
else if ( mode==1 ) { theShape = theBox = (new Box(p0 ,p1, randomColor()));}
else if ( mode==2 ) { theShape = theTriangle = new Triangle( p0, p1,randomColor() ); }
theShape.color = theColorPicker.b.color;
}
public void mouseExited( MouseEvent e ) {}
public void mouseReleased( MouseEvent e ){}
public void mouseMoved( MouseEvent e ) {}
// note postion of mouse xy globally, when dragging
public void mouseDragged( MouseEvent e )
{
x = e.getX(); y = e.getY();
theShape.ends[0].x = x;
theShape.ends[0].y = y;
if ( mode==0 ) // Oval
{
theOval.ends[0].x = x;
theOval.ends[0].y = y;
}
else if ( mode==1 ) // Box
{
theBox.ends[0].x = x;
theBox.ends[0].y = y;
}
else if ( mode==2) // Triangle
{
theTriangle.ends[0].x = x;
theTriangle.ends[0].y = y;
}
System.out.println("dragged to x="+x+" y="+y);
repaint();
}
public void actionPerformed( ActionEvent e )
{
if ( e.getSource()==ovalButton) { mode = 0; }
else if ( e.getSource()==boxButton ) { mode = 1; }
else if ( e.getSource()==triButton ) { mode = 2; }
//clear all
else if (e.getSource() == clearButton)
{
//theOval.clear();
//theBox.clear();
//theTriangle.clear();
}
repaint();
}
public void paint(Graphics g )
{
super.paint(g); // is no super.paint(), then lines stay on screen
if ( theShape != null ) { theShape.drawMe(g); }
}
}
绘画的方法是什么?形状,就是这样。所有其他变量,包括theOval、theBox和Triangle,都是完全独立于一个重要变量形状变量的参考变量。因此,将actionPerformed方法放在要清除图形的位置:
theShape = null;
repaint();
就这样
更重要的是:不要直接在JFrame中绘制。相反,按照教程的要求,使用JPanel的paintComponent
方法