我需要单独或全部删除形状!
请帮助我尝试删除arrayList
“Figuras”中的所有内容
和小组removeAll
public class PaintRodo extends JFrame {
JButton linea, circulo, rectangulo, trazo, llenar,borrar;
static int seleccion = 1;
Color trazoC = Color.BLACK;
Color llenarC = Color.lightGray;
JMenuBar barra;
public static void main(String[] args) {
new PaintRodo();
}
public PaintRodo() {
this.setSize(1000,700);
this.setTitle("Paint");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
Box miBox = Box.createHorizontalBox();
linea= AgregarIcono("./src/linea.png",2);
circulo= AgregarIcono("./src/circ.png",3);
rectangulo= AgregarIcono("./src/rect.png",4);
trazo = AgregarIconoColor("./src/trazo.png",5,true);
llenar = AgregarIconoColor("./src/llenar.png",6,false);
borrar = AgregarIcono("./src/borrador.png",7);
miBox.add(linea);
miBox.add(circulo);
miBox.add(rectangulo);
miBox.add(llenar);
miBox.add(trazo);
miBox.add(borrar);
panel.add(miBox);
this.add(panel,BorderLayout.NORTH);
this.add(new Lienzo(),BorderLayout.CENTER);
this.setVisible(true);
}
public JButton AgregarIcono (String icono, final int accion){
JButton boton = new JButton();
Icon iconos = new ImageIcon (icono);
boton.setIcon(iconos);
boton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
seleccion =accion;
}
});
return boton;
}
public JButton AgregarIconoColor (String icono, final int accion,final boolean grosor ){
JButton boton = new JButton();
Icon iconos = new ImageIcon (icono);
boton.setIcon(iconos);
boton.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
if(grosor){
trazoC = JColorChooser.showDialog(null,"Elige el Contorno",Color.BLACK);
}
else{
llenarC = JColorChooser.showDialog(null,"Elige el Relleno",Color.BLACK);
}
}
});
return boton;
}
public class Lienzo extends JComponent{
ArrayList<Shape> figuras = new ArrayList<Shape>();
ArrayList<Color> arrLleno = new ArrayList<Color>();
ArrayList<Color> arrTrazo = new ArrayList<Color>();
Point inicio,fin;
public Lienzo(){
this.addMouseListener(new MouseAdapter()
{
public void mousePressed(MouseEvent e){
if (seleccion!=1){
inicio = new Point (e.getX(),e.getY());
fin=inicio;
repaint();
}
}
public void mouseReleased(MouseEvent e){
if (seleccion!=1){
Shape SAVEfig=null;
if (seleccion==2){
SAVEfig = crearLinea(inicio.x,inicio.y,e.getX(),e.getY());
}
else if (seleccion==3){
SAVEfig = crearCirculo(inicio.x,inicio.y,e.getX(),e.getY());
}
figuras.add(SAVEfig);
arrLleno.add(llenarC);
arrTrazo.add(trazoC);
repaint();
}
}
});//mouselistener
this.addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
fin= new Point (e.getX(),e.getY());
repaint();
}
});//MotionListener
}//constructor lienzo
public void paint (Graphics g){
Graphics2D graFig = (Graphics2D)g;
graFig.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
graFig.setStroke (new BasicStroke(2));
Iterator<Color>contadorTrazo= arrTrazo.iterator();
Iterator<Color>contadorLleno= arrLleno.iterator();
graFig.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,1.0f));
this.setBackground(Color.LIGHT_GRAY);
for (Shape i: figuras){
graFig.setPaint(contadorTrazo.next());
graFig.draw(i);
graFig.setPaint(contadorLleno.next());
graFig.fill(i);
}
if (inicio!=null&&fin!=null){
graFig.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,0.40f));
graFig.setPaint(Color.LIGHT_GRAY);
Shape SAVEfig=null;
if (seleccion==2){
SAVEfig=crearLinea(inicio.x,inicio.y,fin.x,fin.y);
}
else if (seleccion==3){
SAVEfig=crearCirculo(inicio.x,inicio.y,fin.x,fin.y);
}
graFig.draw(SAVEfig);
}
}
private Rectangle2D.Float crearRectangulo(int x1, int y1,int x2 , int y2){
int x =Math.min(x1, x2);
int y = Math.min(y1, y2);
int ancho = Math.abs(x1-x2);
int alto = Math.abs(y1-y2);
return new Rectangle2D.Float(x,y,ancho,alto);
}
private Line2D.Float crearLinea(int x1, int y1, int x2, int y2){
return new Line2D.Float(x1, y1, x2, y2);
}
private Ellipse2D.Float crearCirculo( int x1, int y1, int x2, int y2){
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int ancho = Math.abs(x1 - x2);
int alto = Math.abs(y1 - y2);
return new Ellipse2D.Float(x, y, ancho, alto);
}
}//classLienzo
}//MainClass Paint rodo
我需要单独或全部删除形状! 请帮忙;我已经尝试删除我的arrayList“Figuras”中的所有内容 和(removeAll)从面板