Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello, I need help on my brightness method. The program runs fine, but when I select the brightness option which displays a slider. I can
Hello, I need help on my brightness method. The program runs fine, but when I select the brightness option which displays a slider. I can only change the value by a little bit before the slider disappears. I believe it is how the slider is displayed, but I do not know how to fix it.
import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.File; import java.io.IOException; import javax.swing.JFileChooser; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JMenu; import javax.swing.JMenuBar; import javax.swing.JMenuItem; import javax.swing.JOptionPane; import javax.swing.JSlider; import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeListener; import javax.swing.filechooser.FileNameExtensionFilter; public class ImageProccessorFrame extends JFrame implements ActionListener { private static final long serialVersionUID = 1L; public boolean fileSelected = false; public Image img = new Image(); public boolean blackAndWhite = false; public ImageProccessorFrame() { setTitle("Image Proccessor"); setVisible(true); setSize(800,800); this.setDefaultCloseOperation(EXIT_ON_CLOSE); JMenuBar menu = new JMenuBar(); JMenu file = new JMenu("File"); menu.add(file); JMenuItem openFile = new JMenuItem("Open new image file..."); openFile.setToolTipText("Select a image file."); file.add(openFile); this.setJMenuBar(menu); openFile.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG, GIF, JPEG, PNG, BMP type of beat images", "jpg", "gif", "jpeg", "png", "bmp"); chooser.setFileFilter(filter); int returnVal = chooser.showOpenDialog(getParent()); if(returnVal == JFileChooser.APPROVE_OPTION) { Image imageChoice = new Image(chooser.getSelectedFile().getAbsolutePath()); img = imageChoice; ImagePanel imageChosen = new ImagePanel(img); setVisible(false); JOptionPane.showMessageDialog(imageChosen, imageChosen, "You have chosen this image" + chooser.getSelectedFile().getName(), JOptionPane.PLAIN_MESSAGE); setContentPane(imageChosen); setSize(imageChoice.getNumberOfColumns(), imageChoice.getNumberOfRows()); revalidate(); setVisible(true); fileSelected = true; } else { JOptionPane.showMessageDialog(null, "No image is chosen", "Image Chosen", JOptionPane.PLAIN_MESSAGE); } } }); JMenuItem save = new JMenuItem("Save the selected image"); file.add(save); save.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter( "JPG image saves only.", "jpg"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(getParent()); if(returnVal == JFileChooser.APPROVE_OPTION && fileSelected == true) { try { File file = chooser.getSelectedFile(); img.writeImage(file.getCanonicalPath()); } catch(IOException ioe) { System.out.println(ioe.getMessage()); } } } }); JMenu filter = new JMenu("Filters"); JMenuItem filter1 = new JMenuItem("Red Blue Swap Filter"); filter1.setToolTipText("This filter swaps the RGB value of the red and blue" + " (Cannot use if image black and white filter is enabled)"); filter.add(filter1); filter1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (fileSelected == true && blackAndWhite == false && fileSelected == true) { img.redBlueSwapFilter(); ImagePanel imageChosen = new ImagePanel(img); setContentPane(imageChosen); } } }); JMenuItem filter2 = new JMenuItem("Black and White Filter"); filter.add(filter2); filter2.setToolTipText("Changes colors to black and white (Disables color swap filters.)"); filter2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (fileSelected == true && fileSelected == true) { img.blackAndWhiteFilter(); ImagePanel imageChosen = new ImagePanel(img); setContentPane(imageChosen); blackAndWhite = true; } } }); JMenuItem filter3 = new JMenuItem("Rotate Clockwise Filter"); filter.add(filter3); filter3.setToolTipText("Rotates the image clockwise direction."); filter3.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (fileSelected == true && fileSelected == true) { img.rotateClockwiseFilter(); ImagePanel imageChosen = new ImagePanel(img); setContentPane(imageChosen); setSize(img.getNumberOfColumns(), img.getNumberOfRows()); } } }); JMenuItem filter4 = new JMenuItem("Swap blue/green & blue to red"); filter.add(filter4); filter4.setToolTipText("Swaps the blue and green values, changes blue value to red. (Cannot use if black and white filter is On)"); filter4.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (fileSelected == true && fileSelected == true && blackAndWhite == false) { img.customFilter(); ImagePanel imageChosen = new ImagePanel(img); setContentPane(imageChosen); } } }); JMenuItem brightness = new JMenuItem("Brightness"); filter.add(brightness); brightness.setToolTipText("Change how bright your image is based on the slider. Right is brighter while left is darker."); brightness.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e) { JSlider slider = new JSlider(JSlider.HORIZONTAL, -200, 200, 0); slider.setMajorTickSpacing(50); slider.setPaintTicks(true); slider.setPaintLabels(true); JLabel sliderLabel = new JLabel("Current value : 0"); slider.add(sliderLabel); add(slider); slider.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { int value = slider.getValue(); sliderLabel.setText("Current value: " + value); img.brightnessFilter(value); ImagePanel imageBrightFilter = new ImagePanel(img); setContentPane(imageBrightFilter); } }); } }); menu.add(filter); } @Override public void actionPerformed(ActionEvent e) { // TODO Auto-generated method stub } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started