Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在java中使用图形删除当前颜色并获取图像的默认颜色_Java_Image_Swing_Graphics_Event Handling - Fatal编程技术网

如何在java中使用图形删除当前颜色并获取图像的默认颜色

如何在java中使用图形删除当前颜色并获取图像的默认颜色,java,image,swing,graphics,event-handling,Java,Image,Swing,Graphics,Event Handling,我有一个图像,然后我使用它的预定义位置创建一个带有颜色的椭圆形。然后我再次点击它,去除椭圆形和颜色 这是我为目标所做的 ImagePanel.java public class ImagePanel extends JPanel{ private Image img; public ImagePanel(String img, String str){ //this(new ImageIcon(img).getImage()); } p

我有一个图像,然后我使用它的预定义位置创建一个带有颜色的椭圆形。然后我再次点击它,去除椭圆形和颜色

这是我为目标所做的

ImagePanel.java

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) {
        g.drawImage(img, 0, 0, null);
    }
}
然后我使用它如下

public class PrintDialog extends javax.swing.JDialog{

private int count = 0;

/**
 * Creates new form PrintDialog
 */
public PrintDialog(java.awt.Frame parent, boolean modal){
    super(parent, modal);
    initComponents();
    ImagePanel panel = new ImagePanel("example.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:
    Graphics g = getGraphics();
    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)){

        count++;
        if (count == 1){
            System.out.println("Count - 1");
            g.setColor(Color.red);
            g.fillOval(66, 52, 20, 20);

        } else if (count > 1){
            g.setColor(new Color(-3692899));
            System.out.println("Min - 2");
            g.fillOval(66, 52, 20, 20);
            count = 0;
        }
    }
    g.dispose();
}
}
公共类PrintDialog扩展了javax.swing.JDialog{ 私有整数计数=0; /** *创建新表单打印对话框 */ 公共打印对话框(java.awt.Frame父对象,布尔模式){ 超级(父级、模态); 初始化组件(); ImagePanel=newImagePanel(“example.jpg”); this.getContentPane().add(面板); 此参数为.setresizeable(false); 此。设置相对位置(面板); 这个包(); } 私有void formMouseClicked(java.awt.event.MouseEvent evt){ //TODO在此处添加您的处理代码: Graphics g=getGraphics(); System.out.println(“Print y-”+evt.getY()); System.out.println(“Print x-”+evt.getX()); 如果((evt.getX()>=68&&evt.getX()=44&&evt.getY()1){ g、 setColor(新颜色(-3692899)); System.out.println(“Min-2”); g、 圆形(66,52,20,20); 计数=0; } } g、 处置(); } } 但最后,当我第二次点击它时,我无法获得该位置的默认颜色。这意味着,我无法删除我创建的椭圆形

你有什么想法吗


谢谢。

永远不要使用getGraphics,这不是自定义绘制工作从
super.paintComponent(g);
开始的方式,因为它是
public void paintComponent(Graphics g){
中的1.st代码行,然后鼠标事件必须调用'repaint();您的问题的答案在我的第一条评论中。您应该使用paintComponent来执行自定义painting@MadProgrammer:我想删除椭圆形或更改其颜色。如果您仍在使用
getGraphics
,请在绘制圆后尝试调整窗口大小…永远不要使用getGraphics,这不是自定义绘制的方式工作从
super.paintComponent(g);
开始,作为
public void paintComponent(Graphics g){
中的1.st代码行,然后鼠标事件必须调用'repaint();您的问题的答案在我的第一条评论中。您应该使用paintComponent来执行自定义painting@MadProgrammer:我要删除椭圆形或更改其颜色。如果您仍在使用
getGraphics
,请在绘制圆形后尝试调整窗口大小。。。