Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6662287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:22:25+00:00 2026-05-26T02:22:25+00:00

Any other errors aside, I need a way of converting my Color grayScale into

  • 0

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();
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T02:22:26+00:00Added an answer on May 26, 2026 at 2:22 am

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can PHP cause memory leaks, buffer overflows, stack overflows and any other errors
Is there any other better way to fill up the array like : var
Or any other normal scripting language for that matter. I know there is VBScript
Are there any other similar packages that help identify and provide capabilities for browser
Without using any other jquery plugin I'm looking to continuously flash/change a div's border
How can Mercurial (or any other DVCS) recognize partially overlapped histories? E.g. fine grain
I can't press any other buttons in the same window while UIScrollView is zooming
Is there any other method that is faster than doing like this? private void
Is there any other command for redirecting a controller to a particular view page
How many bogomips (or any other relevant unit) can an iPhone or iPod touch

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.