1

Any other errors aside, I need a way of converting my Color grayScale into an int. When I plug in the Color, I get an error. setRGB method takes an x, a y, and an rgb int as parameters. How do I change my Color into an 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();
    }
}
Ruben Martinez Jr.
  • 3,199
  • 5
  • 42
  • 76
  • Can you post the stack trace of the error? – Daniel Oct 10 '11 at 03:19
  • The error message? no suitable method found for setRGB(int,int,int,java.awt.Color) – Ruben Martinez Jr. Oct 10 '11 at 03:25
  • See also [How to use `TYPE_BYTE_GRAY` to efficiently create a grayscale bufferedimage using AWT](http://stackoverflow.com/questions/3106269/how-to-use-type-byte-gray-to-efficiently-create-a-grayscale-bufferedimage-using-a/3106550). – trashgod Oct 10 '11 at 03:32
  • 1
    @trashgod Don't suppose you could add a vote to [request to fix the hanging close bracket](http://meta.stackexchange.com/q/251795/155831)? – Andrew Thompson Mar 27 '15 at 06:17

4 Answers4

3

As per comment by trashgod.

Gray Scale image

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
import java.awt.image.*;

public class Picture{
    Container content;
    BufferedImage image;
    BufferedImage  image2;
    public Picture(BufferedImage image) {
        this.image = image;
        grayscale();
    }

    public void show() {
        JFrame frame = new JFrame("Edit Picture");

        //set frame title, set it visible, etc
        content = frame.getContentPane();
        frame.setResizable(false);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //add the image to the frame
        ImageIcon icon = new ImageIcon(image2);
        frame.setContentPane(new JLabel(icon));
        frame.pack();

        //paint the frame
        frame.setVisible(true);
    }

    public void grayscale() {
        // create a grayscale image the same size
        image2 = new BufferedImage(
            image.getWidth(),
            image.getHeight(),
            BufferedImage.TYPE_BYTE_GRAY);

        // convert the original colored image to grayscale
        ColorConvertOp op = new ColorConvertOp(
            image.getColorModel().getColorSpace(),
        image2.getColorModel().getColorSpace(),null);
        op.filter(image,image2);
        show();
    }

    public static void main(String[] args) {
        int size = 120;
        int pad = 10;
        BufferedImage bi = new BufferedImage(
            size,
            size,
            BufferedImage.TYPE_INT_RGB);
        Graphics g = bi.createGraphics();
        g.setColor(Color.WHITE);
        g.fillRect(0,0,size,size);
        g.setColor(Color.YELLOW);
        g.fillOval(pad,pad,size-(2*pad),size-(2*pad));
        g.dispose();

        Picture p = new Picture(bi);
        p.show();
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2
image.setRGB(i, j, grayScale.getRGB());
John B
  • 32,493
  • 6
  • 77
  • 98
  • This only returns a black screen with my menu bar. :/ – Ruben Martinez Jr. Oct 10 '11 at 03:23
  • @RMartin: What are you getting with these - `int rGray = red*(1/3); int gGray = green*(2/3); int bGray = blue*(1/10);`? – Bhesh Gurung Oct 10 '11 at 03:31
  • @BheshGurung I'm calculating the grayscale values. Technically it should be more like *.29, *.58, and *.11 I believe, but then I would need to use doubles, and I can't use doubles with this method :/ Not sure how I would use them otherwise. – Ruben Martinez Jr. Oct 10 '11 at 03:34
1

Take a look at what values (1/3), (2/3), and (1/10) actually have, then note that you are multiplying your red, green, and blue values by those numbers.

Jonathan Callen
  • 11,301
  • 2
  • 23
  • 44
0

The color is an Object. An rgb is an int. Use colorName.getRGB() to get the integer RGB value of that color.

Fractaly
  • 834
  • 2
  • 10
  • 25