0

Jslider is working fine but filtering of image is not occuring.Please help me to find out what is wrong in this code.Is there any other method to change brightness of image? If so please change the code but using jslider brightness should be increase or decrease.

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.imageio.ImageIO;
import java.io.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RescaleOp;

public class Bright extends JFrame 
{
    Dimension screenwidth=getToolkit().getScreenSize();
    int mx=(int)screenwidth.getWidth(); 
    int my=(int)screenwidth.getHeight();
    BufferedImage picture1;
    JLabel label3;
    int neww;
    int newh;
    float[] scales = { 0.5f,1f, 1f, 1f };
    float[] offsets = new float[4];
    RescaleOp rescale;

    public Bright()
    {
        JFrame f = new JFrame("Image Editor v1.0");
        f.setLayout(null);
        try{
            File file=new File("e:\\8.jpg");
            picture1=ImageIO.read(file);
        }catch(Exception e)
        {
        }

        JPanel panel2 = new JPanel(); // create GUI to load image i.e picture1
        panel2.setLayout(null);
        panel2.setBounds(101,20,mx-100,my-20);
        f.add(panel2);
        label3 = new JLabel("");

        panel2.add(label3);
        f.setExtendedState(Frame.MAXIMIZED_BOTH);
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        createimage();
        brightness();
    }

    public void brightness()
    {
        JSlider slider=new JSlider(0,100);
        JPanel sliderpanel = new JPanel();     
        JFrame frame1 = new JFrame();

        frame1.setTitle("Vertical JSlider");
        frame1.setContentPane(sliderpanel);
        frame1.setVisible(true);

        slider.addChangeListener(new ChangeListener() 
            {
                @Override
                public void stateChanged(ChangeEvent e) {
                    // TODO Auto-generated method stub
                    JSlider slider = (JSlider)e.getSource();
                    System.out.println("Slider: "+slider.getValue());
                    float value = (float) slider.getValue()/100f;
                    scales[3]=value;
                    rescale = new RescaleOp(scales,offsets, null);
                    rescale.filter(picture1,picture1); // what problem is here ??
                }   
            });                     

        frame1.add(slider);

        frame1.setSize(550,100);
        createimage();
    }

    public void createimage()
    {
        BufferedImage bi = null;

        if(picture1.getWidth()>mx-110 ||picture1.getHeight()>my-30 )
        {
            neww= (int) Math.round(picture1.getWidth() * 0.25); // reduce size
            newh = (int) Math.round(picture1.getHeight() *0.25);
        }
        else
        {
            neww=picture1.getWidth();
            newh=picture1.getHeight();
        }

        bi = new BufferedImage(neww,newh,BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = (Graphics2D) bi.createGraphics();
        g2d.addRenderingHints(new RenderingHints(RenderingHints.KEY_RENDERING,RenderingHints.VALUE_RENDER_QUALITY));
        g2d.drawImage(picture1,0,0,neww,newh,0,0,picture1.getWidth(),picture1.getHeight(), null);
        label3.setIcon(new ImageIcon(bi));
        label3.setBounds(150,150,neww,newh); //draw image
    }

    public static void main(String[] args) 
    {
        new Bright();
    }
}
Stone
  • 11
  • 2

1 Answers1

0

The createimage method creates a new BufferedImage, and adds this image to the UI via an ImageIcon. As a result, changes to the picture1 variable will not be reflected in the UI. Further, if you do make changes a call to repaint on the UI object may be necessary (you may be better off and have more flexibility creating your own JPanel and drawing the image using custom painting). There's a lot of other things in that code snippet worth mentioning but not necessarily directly related to your problem:

  1. Avoid null layout's like the plague
  2. Do you plan on changing brightness, or opacity of the image (eg alpha). If you plan on changing opacity an Image, the Image should have an alpha component (the createimage method creates RGB type with no alpha).
  3. No reason for your class to extend JFrame if it is not necessary
  4. You are calling createimage twice.
copeg
  • 8,290
  • 19
  • 28
  • could you do some changes so that it will work in addChangeListener because i am trying since 2 days .I am creating image first to display on which image processing would be done second time it will load after changing brightness. i.e why 2 times createimage() method is written. – Stone Apr 16 '15 at 17:11