【问题标题】:Change the color of an image to Gray将图像的颜色更改为灰色
【发布时间】:2021-08-08 23:38:47
【问题描述】:

我正在更改图像的颜色而不将图像的纵横比更改为灰色,当我从键盘上按字母“g”时,我需要更改图像的颜色,我已经使用 KeyListiner 但由于某种原因它不起作用,有人可以告诉我为什么当我按“g”时颜色没有改变

代码如下:

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.*;
public class ImageShuffle extends JComponent {
protected BufferedImage img;
protected BufferedImage rotated;
public ImageShuffle() throws MalformedURLException, IOException {
    img=ImageIO.read(new URL("https://wallpaper.dog/large/17167820.gif"));
   addKeyListener(new KeyListener() {
       public void keyReleased(KeyEvent e) {
           if(e.getKeyCode() == KeyEvent.VK_G)
               System.out.println("G pressed");
           for (int x = 0; x < img.getWidth(); x++) {
               for (int y = 0; y < img.getHeight(); y++) {
                   Color color = new Color(img.getRGB(x, y));
                   int red = color.getRed();
                   int green = color.getGreen();
                   int blue = color.getBlue();
                   red = green = blue = (int)(red * 0.299 + green * 0.587 + 
                  blue * 0.114);
                   color = new Color(red, green, blue);
                   int rgb = color.getRGB();
                   img.setRGB(x, y, rgb);
               }
           }
       }
   });
}
public Dimension getPreferredSize() {
    //Using image size as preferred size
    return new Dimension(img.getWidth(), img.getHeight());
}
public Dimension getMinimumSize() {
    //Using image size as minimum size
    return new Dimension(img.getWidth(), img.getHeight());
}
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    //Compute scaling for both axis according to current component size
    //then use the lower value
    double scaleX=getWidth()*1.0/img.getWidth();
    double scaleY=getHeight()*1.0/img.getHeight();
    double scale=Math.min(scaleX, scaleY);

    //Center the image on the axis with greater scale
    double offsetX=(getWidth()-scale*img.getWidth())/2.0;
    double offsetY=(getHeight()-scale*img.getHeight())/2.0;

    Graphics2D g2=(Graphics2D) g.create();

    //This gives a better quality in upscaling, but also slow down the repainting, remove it if the 
    resizing not responsive enough
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, 
    RenderingHints.VALUE_INTERPOLATION_BICUBIC);

    //We must consider the current possible transform before applying our scaling
    //Current transform
    AffineTransform at=g2.getTransform();
    //Translate to center
    at.concatenate(AffineTransform.getTranslateInstance(offsetX, offsetY));
    //Scale image
    at.concatenate(AffineTransform.getScaleInstance(scale, scale));

    g2.setTransform(at);
    //Paint the transformed image
    g2.drawImage(img, 0, 0, null);

    g2.dispose();
}
public static void main(String[] args) throws MalformedURLException, IOException {
    JFrame frame=new JFrame("Autoresize image");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setContentPane(new ImageShuffle());
    frame.pack();
    frame.setVisible(true);
}
}

【问题讨论】:

    标签: java swing


    【解决方案1】:
    public ImageShuffle() throws MalformedURLException, IOException {
            img=ImageIO.read(new URL("https://wallpaper.dog/large/17167820.gif"));
    
            InputMap im = getInputMap(WHEN_FOCUSED);
            ActionMap am = getActionMap();
    
            im.put(KeyStroke.getKeyStroke(KeyEvent.VK_G, 0), "grey_image");
    
            am.put("grey_image", new AbstractAction() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    System.out.println("G pressed");
    
                    for (int x = 0; x < img.getWidth(); x++) {
                        for (int y = 0; y < img.getHeight(); y++) {
                            Color color = new Color(img.getRGB(x, y));
                            int red = color.getRed();
                            int green = color.getGreen();
                            int blue = color.getBlue();
                            red = green = blue = (int)(red * 0.299 + green * 0.587 + blue * 0.114);
                            color = new Color(red, green, blue);
                            int rgb = color.getRGB();
                            img.setRGB(x, y, rgb);
                        }
                    }
                }
            });
    }
    

    这可行,请注意,这与此答案几乎 100% 相同: KeyPressed event in java 基本上你只需要使用输入映射来注册你的事件。

    【讨论】:

    • 非常感谢@Asis
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多