所以我想在我的盒子和三角形以及其他一些
上画画后清理我的窗户形状。我需要点击一个按钮并撤消所有应该从
消失的所有内容 窗口,一切都被清理干净了。这里我做了一个方框,当我点击按钮框并在窗口中点击一个框显示
我点击。
我为三角形做了同样的事情。
这是主程序(只有框):
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Kaleidescope extends JFrame implements MouseListener, ActionListener,
MouseMotionListener {
Triangle L;
Triangle[] triangles;
int triangleCount;
Box b;
Box[] boxes; // 2-d array of Box objects, form a color pallet
int boxCount;
JButton boxButton;
Oval O;
Oval[] ovals;
int ovalCount;
Oval v; // Oval x, y
int downX=0, downY=0, upX=0, upY=0;
int x1, y1; // mousePressed
int w1, z1; // mouseEntered
int mode = 1; // 1 = line, 2= boxes, 3 = oval, 4= text, 5 = SG, twoLines = 7.
JButton TriangleButton;
JButton boxButton;
boolean Boxe = false; // if true draw box
JButton ovalButton;
int x1, y1; // mousePressed
int w1, z1; // mouseEntered
public static void main(String[] args)
{
System.out.println("hi there.");
new Kaleidescope();
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
triangles = new Triangle[20];
triangleCount = 0;
boxes = new Box[20];
boxCount = 0;
ovals = new Oval[20];
ovalCount = 0;
setLayout(new FlowLayout());
boxButton = new JButton("Boxes");
add(boxButton);
boxButton.addActionListener(this);
TriangleButton = new JButton("Triangles");
add(TriangleButton);
TriangleButton.addActionListener( this);
ovalButton = new JButton("Oval");
add(ovalButton);
ovalButton.addActionListener( this);
setSize(new Dimension(500, 500));
setVisible(true);
}
// 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);
}
public void mouseClicked(MouseEvent e)
{
System.out.println("click at x="+e.getX()+" y="+e.getY());
// convert window coords to box array indexes.
// These were adjusted slightly after the video was made
// (no more flakiness, these are right on target).
int boxi = (e.getX()-10)/20; // convert mouse x to box index
int boxj = (e.getY()-40)/20;
System.out.println("click at boxi="+boxi+" boxj="+boxj);
// set extra box to the color that we clicked on
if (mode == 2)
{
boxes[boxCount++] = new Box(e.getX(), e.getY(), randomColor());
}
if ( mode == 1)
{
triangles[triangleCount++] = new Triangle(e.getX(), e.getY(),
randomColor());
}
repaint();
}
public void actionPerformed(ActionEvent e)
{
if ( e.getSource()==TriangleButton ) { mode = 1;}
else if ( e.getSource()==boxButton ) { mode = 2;}
else if ( e.getSource()==ovalButton) { mode = 3;}
else if (e.getSource() == clearButton)
{
// release all existing boxes
while (boxCount-- > 0) boxes[boxCount] = null;
while (triangleCount-- > 0) triangles[triangleCount] = null;
while (ovalCount-- > 0) ovals[ovalCount] = null;
}
repaint();
}
public void mouseEntered(MouseEvent e)
{
}
public void mousePressed(MouseEvent e)
{
x1 = e.getX();
y1 = e.getY();
downX = e.getX();
downY = e.getY();
if ( mode == 3)
{
v = new Oval(e.getX(),e.getY(), randomColor());
ovals [ovalCount]= v;
ovalCount++;
}
repaint();
}
public void mouseExited(MouseEvent e)
{
}
public void mouseReleased(MouseEvent e)
{
}
public void mouseMoved( MouseEvent e ) {}
public void mouseDragged(MouseEvent e)
{
upX = e.getX();
upY = e.getY();
if ( mode ==3)
{
int width;
int height;
width = upX - downX;
height = upY - downY;
v.w = width;
v.h = height;
}
repaint();
}
public void paint(Graphics g)
{
super.paint(g);
for (int i = 0; i < boxCount; i++)
{
boxes[i].drawMe(g);
}
for ( int a=0; a<triangleCount; a++)
{
triangles[a].drawMe(g);
}
for ( int b=0; b<ovalCount; b++)
{
ovals[b].drawMe(g);
}
}
}
这是盒子的另一个类(连接到主程序来绘制我的盒子)
import java.awt.*;
public class Box
{
protected Color color;
protected int x, y; // pixels from upper left to upper left corner
// make a box
public Box(int x1, int y1, Color c1)
{
x = x1;
y = y1;
color = c1;
}
public void drawMe(Graphics g)
{
g.setColor(color);
g.fillRect(x, y, 20, 20);
}
public void setColor(Color c)
{
color = c;
}
public Color getColor()
{
return color;
}
}
Here is another class
package kaleidescope;
public class Point
{
int x;
int y;
public Point( int x1, int y1 )
{
x = x1; y = y1;
}
}
此类
package kaleidescope;
import java.awt.*;
public abstract class Shape
{
protected Color color;
abstract public void drawMe( Graphics g );
}
答案 0 :(得分:0)
只需添加“清除”按钮,然后在处理程序中将boxCount
设置为零并调用repaint()
。您必须调整actionPerformed
以检测清除按钮何时为源。
编辑:这是你的代码修改后添加一个清晰的功能(仅适用于盒子,但见下文):
JButton boxButton;
JButton clearButton; // NEW
public static void main(String[] args)
{
System.out.println("hi there.");
new Kaleidescope();
setDefaultCloseOperation(EXIT_ON_CLOSE);
addMouseListener(this);
addMouseMotionListener(this);
boxes = new Box[20];
boxCount = 0;
setLayout(new FlowLayout());
boxButton = new JButton("Boxes");
add(boxButton);
boxButton.addActionListener(this);
// NEXT 3 LINES ARE NEW
clearButton = new JButton("Clear");
add(clearButton);
clearButton.addActionListener(this);
setSize(new Dimension(500, 500));
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{
if (e.getSource() == boxButton)
{
mode = 2;
repaint();
}
else if (e.getSource() == clearButton)
{
// release all existing boxes
while (boxCount-- > 0) boxes[boxCount] = null;
// TODO: zero out all other display list items as well
// redraw everything (which is now nothing)
repaint();
}
}
由于您没有显示如何维护其他形状的列表,因此我无法为TODO部分提供特定代码。但是,它看起来与我发布的内容非常相似。
编辑2:
以下是我建议如何切换使用ArrayList
而不是数组的方法。我将描述如何改变盒子的东西;其他形状也类似。
java.util.ArrayList
。boxCount
。更改
Box[] boxes;
到
ArrayList<Box> boxes;
替换
行boxes = new Box[20];
boxCount = 0;
使用:
boxes = new ArrayList<Box>(); // or new ArrayList<>() for Java 7+ compiler
替换行:
boxes[boxCount++] = new Box(e.getX(), e.getY(), randomColor());
使用:
boxes.add(new Box(e.getX(), e.getY(), randomColor()));
替换行:
while (boxCount-- > 0) boxes[boxCount] = null;
使用:
boxes.clear();
替换绘图循环:
for (int i = 0; i < boxCount; i++)
{
boxes[i].drawMe(g);
}
使用:
for (Box box : boxes) {
box.drawMe(g);
}
就是这样。从您到目前为止发布的代码中,我没有看到为盒子,三角形和椭圆形成单独列表的原因。我建议您只有一个Shape
个对象的显示列表,其中包含要绘制的所有形状(盒子,三角形和椭圆形都混合在一起)。但是,在代码的其他部分中,您可能会分别处理每个对象列表;这可能会改变我的建议。