gpt4 book ai didi

java - 当 ArrayList 中删除索引时,如何自动删除 JPanel 上的形状?

转载 作者:行者123 更新时间:2023-12-02 04:49:02 26 4
gpt4 key购买 nike

我想知道如何从 JPanel 中删除特定形状。该形状存储在 ArrayList 中,当删除该形状的索引时,我希望该形状也从我的 JPanel 中删除。

任何人都可以指导我找到解决方案吗?我应该使用什么来删除 JPanel 上的形状?

即使在ArrayList中删除了形状的索引,该形状仍然存在于JPanel上,有人有解决方案吗?

编辑代码:

import java.awt.AlphaComposite;
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Iterator;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;


public class Testing_1 extends JFrame {
private int len;

// public int getSizes(){
// return len;
//}

private static final long serialVersionUID = -140274271716086522L;

JMenuBar menubar;
JMenu File, Exit;
JMenuItem New, Open;
JComponent DrawingBoard;
JPanel panel;
int option;

JButton lineBut, ellipseBut, rectBut, strokeBut, trianBut, mulBut, diamondBut,
arrowBut, derivedBut, primaryBut;

JTextField check, defaultCon, length,attName;

DecimalFormat dec = new DecimalFormat("#.##");

// Contains all of the rules for drawing

Graphics2D graphSettings, dotted;


// Going to be used to monitor what shape to draw next

int currentAction = 1;
int x, y;

double a = 10.0;
double b =10.0;

String checkCon;

// Transparency of the shape

// Default stroke and fill colors

Color strokeColor = Color.BLACK;

public static void main(String[] args) {
new Testing_1();
}

public Testing_1() {
// Define the defaults for the JFrame

this.setSize(800, 600);
this.setTitle("ERD BUILDER");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setJMenuBar(menubar);

JMenuBar menuBar = new JMenuBar();

// Add the menubar to the frame



JPanel buttonPanel = new JPanel();

JPanel FieldPanel = new JPanel();

// Swing box that will hold all the buttons

Box theBox = Box.createVerticalBox();
Box theBoxs = Box.createVerticalBox();

// Make all the buttons in makeMeButtons by passing the
// button icon.


lineBut = makeMeButtons("./src/line.png", 2);
ellipseBut = makeMeButtons("./src/ellipse.png", 3);


// Make all the buttons in makeMeColorButton by passing the
// button icon and true for stroke color or false for fill

strokeBut = makeMeColorButton("./src/stroke.png", 5, true);

check = new JTextField();
defaultCon = new JTextField();
length = new JTextField();
attName = new JTextField();


// Add the fields to the boxs



//theBox.add(brushBut);
theBox.add(lineBut);
theBox.add(ellipseBut);

buttonPanel.add(theBox);
FieldPanel.add(theBoxs);


this.add(buttonPanel, BorderLayout.WEST);
this.add(FieldPanel, BorderLayout.EAST);
buttonPanel.setPreferredSize(new Dimension(200,480));
FieldPanel.setPreferredSize(new Dimension(200,480));
// Make the drawing area take up the rest of the frame

// this.add(new DrawingBoard(), BorderLayout.CENTER);
final DrawingBoard drawPanel = new DrawingBoard();
this.add(drawPanel, BorderLayout.CENTER);
this.getContentPane().setBackground(Color.WHITE);






// Show the frame
this.setVisible(true);
}

public JButton makeMeButtons(String iconFile, final int actionNum) {
JButton theBut = new JButton();
Icon butIcon = new ImageIcon(iconFile);
theBut.setIcon(butIcon);

theBut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {
currentAction = actionNum;
}
});
return theBut;
}

public JButton makeMeColorButton(String iconFile, final int actionNum,
final boolean stroke) {
JButton theBut = new JButton();
Icon butIcon = new ImageIcon(iconFile);
theBut.setIcon(butIcon);

theBut.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

if (stroke) {

// JColorChooser is a popup that lets you pick a color

strokeColor = JColorChooser.showDialog(null,
"Pick a Stroke", Color.BLACK);
} else {
}
}
});

return theBut;
}
public class DrawingBoard extends JComponent {

private static final long serialVersionUID = -4431176095451940075L;

// ArrayList<Shape> shapes = new ArrayList<Shape>();
ArrayList<Color> shapeStroke = new ArrayList<Color>();
ArrayList<Shape> dottedShape = new ArrayList<Shape>();
ArrayList<Integer> count = new ArrayList<Integer>();
ArrayList<NamedShape> shapes = new ArrayList<NamedShape>();


public class NamedShape {
private String name;
private Shape shape;

public NamedShape( String name, Shape shape ){
this.name = name;
this.shape = shape;
}
public String getName(){
return name;
}
public Shape getShape(){
return shape;
}
}

Point drawStart, drawEnd;
// Monitors events on the drawing area of the frame

public DrawingBoard() {


this.addMouseListener(new MouseAdapter() {

public void mousePressed(MouseEvent e) {

if (currentAction != 1) {

// When the mouse is pressed get x & y position

drawStart = new Point(e.getX(), e.getY());
drawEnd = drawStart;
repaint();
}
}

public void mouseReleased(MouseEvent e) {
int counts =0;
if (currentAction != 1) {
Shape aShape = null;
String nam = null;

if (currentAction == 2) {
boolean collision = false;

aShape = drawLine(drawStart.x, drawStart.y,
e.getX(), e.getY());

shapes.add( new NamedShape( "Humpty-Dumpty",
aShape ) );

int var = shapes.size();
System.out.println(shapes);
System.out.println("Array index"+var);
shapeStroke.add(strokeColor);
drawStart = null;
drawEnd = null;
repaint();
} else


if (currentAction == 3) {
boolean collision = false;
int collisions =0;
aShape = drawEllipse(drawStart.x, drawStart.y,
e.getX(), e.getY());


String text = (String) JOptionPane.showInputDialog(DrawingBoard, "Enter name of Attribute:");



shapes.add( new NamedShape( text,
aShape ) );

int var = shapes.size();
System.out.println("Array index"+var);
shapeStroke.add(strokeColor);
drawStart = null;
drawEnd = null;
repaint();
}
// new AttributeForm().setVisible(true);




}

}
});

this.addMouseMotionListener(new MouseMotionAdapter() {

public void mouseDragged(MouseEvent e) {

// If this is a brush have shapes go on the screen quickly

if (currentAction == 1) {

int x = e.getX();
int y = e.getY();

Shape aShape = null;
shapes.add( new NamedShape( "Line",
aShape ) );

shapeStroke.add(strokeColor);

}

drawEnd = new Point(e.getX(), e.getY());
repaint();
}
});
}

public void paint(Graphics g) {
// Class used to define the shapes to be drawn

graphSettings = (Graphics2D) g;
dotted = (Graphics2D)g;


// Antialiasing cleans up the jagged lines and defines rendering
// rules

graphSettings.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Defines the line width of the stroke

graphSettings.setStroke(new BasicStroke(4));

dotted.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);

// Iterators created to cycle through strokes and fills
Iterator<Color> strokeCounter = shapeStroke.iterator();
for (NamedShape s : shapes) {

// Grabs the next stroke from the color arraylist
graphSettings.setPaint(strokeCounter.next());

graphSettings.draw(s.getShape());

}

for(Shape b : dottedShape)
{

Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);

dotted.setStroke(dashed);

// Grabs the next stroke from the color arraylist
dotted.setPaint(strokeCounter.next());

dotted.draw(b);
}




// Guide shape used for drawing
if (drawStart != null && drawEnd != null) {
// Makes the guide shape transparent

graphSettings.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.40f));

// Make guide shape gray for professional look

graphSettings.setPaint(Color.LIGHT_GRAY);


dotted.setComposite(AlphaComposite.getInstance(
AlphaComposite.SRC_OVER, 0.40f));

dotted.setPaint(Color.LIGHT_GRAY);

Shape aShape = null;
Shape dShape = null;

if (currentAction == 2) {
aShape = drawLine(drawStart.x, drawStart.y, drawEnd.x,
drawEnd.y);
} else

if (currentAction == 3) {
aShape = drawEllipse(drawStart.x, drawStart.y, drawEnd.x,
drawEnd.y);
}

graphSettings.draw(aShape);


}
}


private Ellipse2D.Float drawEllipse(int x1, int y1, int x2, int y2) {
int x = Math.min(x1, x2);
int y = Math.min(y1, y2);
int width = Math.abs(x1 - x2);
int height = Math.abs(y1 - y2);

return new Ellipse2D.Float(x, y, width, height);
}

private Line2D.Float drawLine(int x, int y, int width , int height) {

return new Line2D.Float(x, y,width, height);
}

}

}

上面的代码允许用户绘制形状。我的 JComponent 称为 DrawingBoard ,用于绘制形状的 Jpanel 称为 drawPanel ,我的 ArrayList 用于存储形状的是NamedShape

我不太确定在哪里放置方法remove,以便Java也可以识别我的ArrayList和Jpanel。可以请您赐教吗?

这是编辑后的方法:

void removeShape(int index) {
drawPanel.remove(shapes.remove(index));
}

最佳答案

您可以使用辅助方法来执行此操作,如下所示:

JPanel panelWithShapes ...
List<ShapeComponent> myShapes ...

void removeShape(int index) {
panelWithShapes.remove(myShapes.remove(index));
}

另一种可能性是从 List 覆盖 remove 并从面板中删除该组件。

关于java - 当 ArrayList 中删除索引时,如何自动删除 JPanel 上的形状?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29421481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com