如何在Java swing应用程序中保留和删除多个图形对象?
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了如何在Java swing应用程序中保留和删除多个图形对象?,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含5445字,纯文字阅读大概需要8分钟。
内容图文
![如何在Java swing应用程序中保留和删除多个图形对象?](/upload/InfoBanner/zyjiaocheng/696/1501d655b2194eabbf9ceb47863bf59b.jpg)
我有一个图像,并在其上使用预定义的位置来创建带有color的图形对象.用鼠标单击,我尝试在其上创建一些带有颜色的椭圆形.实际上,我无法实现这一目标.因为,当我单击一个预定义位置时,可以在其上创建一个椭圆形,但是当我单击另一个预定义位置时,第一个椭圆形消失了.
可以通过单击椭圆两次将其删除.
看看这个,
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();
}
}
}
图像面板类
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;
Dimension size = new Dimension(img.getWidth(null), img.getHeight(null));
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
setSize(size);
setLayout(null);
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();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(img, 0, 0, null);
}
}
有什么想法吗?
谢谢 .
解决方法:
不要使用getGraphics,这不是自定义绘画的工作方式,有关更多详细信息,请参见Painting in AWT and Swing和Performing Custom Painting
基本思想是,您需要某种列表,您可以在其中添加每个形状.当发生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 swing应用程序中保留和删除多个图形对象?全部内容,希望文章能够帮你解决如何在Java swing应用程序中保留和删除多个图形对象?所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。