如何在JavaSwing应用程序中保留和删除多个图形对象?
我有一个图像,我使用它的预定义位置来创建带有颜色的图形对象。我试着用鼠标点击来创建一些带有颜色的椭圆形。事实上,我无法实现这个目标。因为,当我点击一个预定义的位置时,我可以在上面创建一个椭圆形,但当我点击另一个预定义的位置时,第一个椭圆形已经消失了 单击两次可以删除椭圆形 看看这个,如何在JavaSwing应用程序中保留和删除多个图形对象?,java,image,swing,graphics,event-handling,Java,Image,Swing,Graphics,Event Handling,我有一个图像,我使用它的预定义位置来创建带有颜色的图形对象。我试着用鼠标点击来创建一些带有颜色的椭圆形。事实上,我无法实现这个目标。因为,当我点击一个预定义的位置时,我可以在上面创建一个椭圆形,但当我点击另一个预定义的位置时,第一个椭圆形已经消失了 单击两次可以删除椭圆形 看看这个, public class PrintDialog extends javax.swing.JDialog{ private int count = 0; private int count_1 = 0; /**
public class PrintDialog extends javax.swing.JDialog{
private int count = 0;
private int count_1 = 0;
/**
* Creates new form PrintFeetDialog
*/
public PrintDialog(java.awt.Frame parent, boolean modal)
{
super(parent, modal);
initComponents();
ImagePanel panel = new ImagePanel("Areas.jpg");
this.getContentPane().add(panel);
this.setResizable(false);
this.setLocationRelativeTo(panel);
this.pack();
}
private void formMouseClicked(java.awt.event.MouseEvent evt)
{
// TODO add your handling code here:
System.out.println("Print y - " + evt.getY());
System.out.println("Print x - " + evt.getX());
if ((evt.getX() >= 68 && evt.getX() <= 84) && (evt.getY() >= 44 && evt.getY() <= 72))
{
Graphics g = getGraphics();
count++;
if (count == 1)
{
g.setColor(Color.red);
g.fillOval(66, 52, 20, 20);
// repaint();
} else if (count > 1)
{
g.setColor(new Color(-3692899));
g.fillOval(66, 52, 20, 20);
repaint();
count = 0;
}
g.dispose();
}
if ((evt.getX() >= 137 && evt.getX() <= 157) && (evt.getY() >= 50 && evt.getY() <= 75))
{
Graphics g1 = getGraphics();
count_1++;
if (count_1 == 1)
{
g1.setColor(Color.RED);
g1.fillOval(137, 54, 20, 20);
} else if (count_1 > 1)
{
g1.setColor(new Color(-3692899));
g1.fillOval(66, 52, 20, 20);
repaint();
count_1 = 0;
}
g1.dispose();
}
}
}
你有什么想法吗
谢谢。不要使用
getGraphics
,这不是自定义绘制的工作方式,有关详细信息,请参阅和
基本思想是,您需要某种类型的列表
,将每个形状添加到其中。当出现mouseClicked
时,您迭代列表并检查鼠标单击是否出现其中一个形状,如果是,则从列表中删除该形状,如果不是,则在单击点创建一个新形状并将其添加到列表中
然后在paintComponent
方法中使用此List
来实际绘制形状
此示例扩展了您的ImagePanel
添加到自定义绘制中
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawPane("/Volumes/Disk02/Dropbox/MegaTokyo/thumnails/0.jpg"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img, String str) {
//this(new ImageIcon(img).getImage());
}
public ImagePanel(String path) {
Image img = new ImageIcon(path).getImage();
this.img = img;
try {
BufferedImage image = ImageIO.read(new File(path));
int rgb = image.getRGB(66, 52);
System.out.println("Colour is: " + rgb);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
public class DrawPane extends ImagePanel {
private List<Shape> shapes;
public DrawPane(String img, String str) {
super(img, str);
init();
}
public DrawPane(String path) {
super(path);
init();
}
protected void init() {
shapes = new ArrayList<>(25);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
boolean clicked = false;
Iterator<Shape> it = shapes.iterator();
while (it.hasNext()) {
Shape shape = it.next();
if (shape.contains(e.getPoint())) {
it.remove();
clicked = true;
break;
}
}
if (!clicked) {
shapes.add(new Ellipse2D.Double(e.getX() - 10, e.getY() - 10, 20, 20));
}
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Shape shape : shapes) {
g2d.draw(shape);
}
g2d.dispose();
}
}
}
导入java.awt.Color;
导入java.awt.Dimension;
导入java.awt.EventQueue;
导入java.awt.Graphics;
导入java.awt.Graphics2D;
导入java.awt.Image;
导入java.awt.Shape;
导入java.awt.event.MouseAdapter;
导入java.awt.event.MouseEvent;
导入java.awt.geom.Ellipse2D;
导入java.awt.image.buffereImage;
导入java.io.File;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.Iterator;
导入java.util.List;
导入javax.imageio.imageio;
导入javax.swing.ImageIcon;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.UIManager;
导入javax.swing.UnsupportedLookAndFeelException;
公开课考试{
公共静态void main(字符串[]args){
新测试();
}
公开考试(){
invokeLater(新的Runnable(){
@凌驾
公开募捐{
试一试{
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
}catch(ClassNotFoundException |实例化Exception | IllegalacessException |不支持ookandfeelException ex){
例如printStackTrace();
}
JFrame=新JFrame(“测试”);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(新绘图窗格(“/Volumes/Disk02/Dropbox/MegaTokyo/thumnails/0.jpg”);
frame.pack();
frame.setLocationRelativeTo(空);
frame.setVisible(true);
}
});
}
公共类ImagePanel扩展了JPanel{
私有图像img;
公共图像面板(字符串img、字符串str){
//这个(新的ImageIcon(img).getImage());
}
公共图像面板(字符串路径){
Image img=新图像图标(path).getImage();
this.img=img;
试一试{
buffereImage image=ImageIO.read(新文件(路径));
intrgb=image.getRGB(66,52);
System.out.println(“颜色为:“+rgb”);
}捕获(IOE异常){
e、 printStackTrace();
}
}
@凌驾
公共维度getPreferredSize(){
返回img==null?新维度(200200):新维度(img.getWidth(this)、img.getHeight(this));
}
@凌驾
公共组件(图形g){
超级组件(g);
g、 drawImage(img,0,0,null);
}
}
公共类绘图窗格扩展了ImagePanel{
私有列表形状;
公共绘图窗格(字符串img、字符串str){
super(img,str);
init();
}
公共绘图窗格(字符串路径){
超级(路径);
init();
}
受保护的void init(){
形状=新阵列列表(25);
addMouseListener(新的MouseAdapter(){
@凌驾
公共无效mouseClicked(MouseEvent e){
布尔值=假;
Iterator it=shapes.Iterator();
while(it.hasNext()){
Shape=it.next();
if(shape.contains(e.getPoint())){
it.remove();
单击=真;
打破
}
}
如果(!单击){
add(新的Ellipse2D.Double(e.getX()-10,e.getY()-10,20,20));
}
重新油漆();
}
});
}
@凌驾
公共组件(图形g){
超级组件(g);
Graphics2D g2d=(Graphics2D)g.create();
g2d.setColor(Color.RED);
用于(形状:形状){
g2d.绘制(形状);
}
g2d.dispose();
}
}
}
正如我所说的,不要使用getGraphics
,这不是自定义绘制的工作方式,请参阅,有关更多详细信息,我感到困惑。有编码支持吗?请从阅读链接教程开始,这就是它们存在的原因。他们有合理的例子。如果你尝试填充graphic2D对象,你想做什么改变?现在我有一个小问题。你能听我说吗?你可以看到我在帖子中标出的一些预定义的位置。我只想对这些标记的位置执行您的建议。你有什么想法吗?创建一个形状列表,在列表中为你想要的每个位置添加一个Ellipse2D实例,然后你可以使用确定鼠标点是否在其中一个位置的基本概念
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new DrawPane("/Volumes/Disk02/Dropbox/MegaTokyo/thumnails/0.jpg"));
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class ImagePanel extends JPanel {
private Image img;
public ImagePanel(String img, String str) {
//this(new ImageIcon(img).getImage());
}
public ImagePanel(String path) {
Image img = new ImageIcon(path).getImage();
this.img = img;
try {
BufferedImage image = ImageIO.read(new File(path));
int rgb = image.getRGB(66, 52);
System.out.println("Colour is: " + rgb);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public Dimension getPreferredSize() {
return img == null ? new Dimension(200, 200) : new Dimension(img.getWidth(this), img.getHeight(this));
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
public class DrawPane extends ImagePanel {
private List<Shape> shapes;
public DrawPane(String img, String str) {
super(img, str);
init();
}
public DrawPane(String path) {
super(path);
init();
}
protected void init() {
shapes = new ArrayList<>(25);
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
boolean clicked = false;
Iterator<Shape> it = shapes.iterator();
while (it.hasNext()) {
Shape shape = it.next();
if (shape.contains(e.getPoint())) {
it.remove();
clicked = true;
break;
}
}
if (!clicked) {
shapes.add(new Ellipse2D.Double(e.getX() - 10, e.getY() - 10, 20, 20));
}
repaint();
}
});
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.setColor(Color.RED);
for (Shape shape : shapes) {
g2d.draw(shape);
}
g2d.dispose();
}
}
}