Set Color como valor int para uso no método setRGB (int x, int y, int rgb)? - Java
Qualquer outro erro de lado, eu preciso de uma maneira de converter minha Color grayScale em um int. Quando conecto o Color, recebo um erro. O método setRGB usa int, x, y e rgb como parâmetros. Como altero minha cor para int?
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;
import javax.imageio.ImageIO;
public class Picture{
Container content;
BufferedImage image, image2;
public Picture(String filename) {
File f = new File(filename);
//assume file is the image file
try {
image = ImageIO.read(f);
}
catch (IOException e) {
System.out.println("Invalid image file: " + filename);
System.exit(0);
}
}
public void show() {
final int width = image.getWidth();
final int height = image.getHeight();
JFrame frame = new JFrame("Edit Picture");
//set frame title, set it visible, etc
content = frame.getContentPane();
content.setPreferredSize(new Dimension(width, height));
frame.pack();
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//add a menubar on the frame with a single option: saving the image
JMenuBar menuBar = new JMenuBar();
frame.setJMenuBar(menuBar);
JMenu fileMenu = new JMenu("File");
menuBar.add(fileMenu);
JMenuItem saveAction = new JMenuItem("Save");
fileMenu.add(saveAction);
JMenuItem grayScale = new JMenuItem("Grayscale");
fileMenu.add(grayScale);
grayScale.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
grayscale(width, height);
}
});
//add the image to the frame
ImageIcon icon = new ImageIcon(image);
frame.setContentPane(new JLabel(icon));
//paint the frame
frame.setVisible(true);
frame.repaint();
}
public void grayscale(int width, int height) {
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
Color color = new Color(image.getRGB(i, j));
int red = color.getRed();
int green = color.getGreen();
int blue = color.getBlue();
int rGray = red*(1/3);
int gGray = green*(2/3);
int bGray = blue*(1/10);
Color grayScale = new Color(rGray, gGray, bGray);
image.setRGB(i,j, grayScale);
}
}
show();
}
public static void main(String[] args) {
Picture p = new Picture(args[0]);
p.show();
}
}