Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Why does my grayscale image appear black?

807580Jul 6 2010 — edited Jul 12 2010
I can get images to become grayscale in my other java codes, but when I include the code in this project, instead of the grayscale image appearing, it is all black.

This is the JApplet code:
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.event.*;

public class RGB_Slider extends JApplet
                       implements ChangeListener, ActionListener {

   private JSlider slider;
              // For setting the color.

   private JLabel Tlabel, imageLabel, copyLabel;
              // For displaying RGB values.

   private JButton loadbutton, runbutton;
   ImageFrame fr;
   Image image;
   BufferedImage bufferedImage;
   JFrame grayframe;

   public void init() {
       slider = new JSlider(JSlider.HORIZONTAL, 0, 150, 0);
       
            Tlabel = new JLabel(" Threshold = 0");

       loadbutton=new JButton("Load Image");
      runbutton=new JButton("Run Algorithm");
      loadbutton.addActionListener(this);
      runbutton.addActionListener(this);

           Tlabel.setBackground(Color.white);
       Tlabel.setForeground(Color.black);
       Tlabel.setOpaque(true);
      
          slider.addChangeListener(this);
      
         setBackground(Color.gray);
       getContentPane().setBackground(Color.gray);
       getContentPane().setLayout(new GridLayout(1,3,3,3)); //set the layout to grid
       JPanel buttons=new JPanel(); //creates new JPanel for the buttons
       JPanel scrolls = new JPanel(); //creates new JPanel for the sliders
       JPanel labels = new JPanel(); //creates new JPanel for the labels
       buttons.setBackground(Color.gray);//set the button's JPanel bg color
       scrolls.setBackground(Color.gray); //set the slider's JPanel bg color gray
       labels.setBackground(Color.gray); //set the label's JPanel  bg color gray
       getContentPane().add(buttons);//add the button's JPanel to the content pane
       getContentPane().add(scrolls); //add the slider's JPanel to the Content pane
       getContentPane().add(labels);//add the label's JPanel to the content pane

       scrolls.setLayout(new GridLayout(3,1,2,2));
       scrolls.add(slider);
     
       labels.setLayout(new GridLayout(3,1,2,2));
       labels.add(Tlabel);

       buttons.setLayout(new GridLayout(3,1,2,2));
       buttons.add(loadbutton);
       buttons.add(runbutton);

   } 


   public void stateChanged(ChangeEvent evt) {
    
       int threshold = slider.getValue();
       Tlabel.setText(" Threshold = " + threshold);
         
   } 

   public Insets getInsets()
   {
      return new Insets(5,5,5,5);
   }

    public void actionPerformed(ActionEvent e) 
    {
        if(e.getSource()==loadbutton)
        {
         image= new ImageIcon("bao.jpg").getImage();
         bufferedImage = new BufferedImage(image.getHeight(this),
         image.getWidth(this), BufferedImage.TYPE_BYTE_GRAY);

           fr=new ImageFrame(image);

           fr.addWindowListener( new WindowAdapter()
        {
            public void windowClosing( WindowEvent e )
            {
                fr.setVisible( false );
            }});

    fr.setSize( 500, 500 );
    fr.show();
        }

        if(e.getSource()==runbutton)
        {

            grayframe=new JFrame();
        }
    }


}  
This is the ImageFrame code:
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class ImageFrame extends Frame
{
    private ImageCanvas c;
    private Image image;
    Button exitbutton;
    BufferedImage bufferedImage;

    ImageFrame( Image image )
    {
        this.image = image;
        c = new ImageCanvas( image );
        add( c );
    }

    public static void main( String[] args )
    {
    Toolkit tk = Toolkit.getDefaultToolkit();
      }

    private class ImageCanvas extends Canvas
    {
       Image image;
        public ImageCanvas( Image image )
        {
          this.image = image;
          bufferedImage = new BufferedImage(image.getHeight(this),
          image.getWidth(this), BufferedImage.TYPE_USHORT_GRAY);

          Graphics g=bufferedImage.getGraphics();
            
        }

           public void paint( Graphics g )
        {
          //this draws the image in the ImageFrame
            g.drawImage( bufferedImage, 0, 0, null );
            g.dispose();

        
        }

       }
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Aug 9 2010
Added on Jul 6 2010
7 comments
360 views