我绘制了一些形状,我想从屏幕上清理屏幕。所以我做了一个按钮并将其命名为clearButton并在我的actionPreformed中清理它们。它根本不是清洁。所以我想要的是我的cleanButton来清理我画的东西。只要看看cleanButton以及我做了什么来使它干净。它很长但我不得不发布整个事情,所以它更清楚。只需忽略它,只查看cleanButton正在做什么。谢谢。
主类:
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); }
}
}
答案 0 :(得分:1)
绘画方法有什么用? theShape,就是这样。所有其他变量,包括Oval,theBox和Triangle,都只是完全独立于一个重要变量theShape Variable的引用变量。因此,请在actionPerformed方法中放置要清除绘图的内容:
theShape = null;
repaint();
就是这样。
更重要的是:永远不要直接在JFrame中绘制。而是在JPanel的paintComponent
方法中绘制,因为教程都会告诉你这样做。