Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Question: How do I use FileChooser to select a file in my ControlFrame.java file

ID: 3880070 • Letter: Q

Question

Question: How do I use FileChooser to select a file in my ControlFrame.java file? I want to select a file when I select Show Picture from the file menu or when I select Load Sound from the File menu. This question was answered, but I am struggling with finding where it needs to be in my code to work. Please explain where you would put it when selecting Show Picture from the File menu. Do I add it in the event handler when they select Show Picture from the File menu?

File: ControlFrame.java

// ControlFrame.java
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.filechooser.*;

public class ControlFrame extends JFrame
{
private JPanel mainPanel;
private final JPanel calcPanel;
private JSlider widthJSlider;
private JTextField xValTextField;
private JTextField yValTextField;
private JLabel calcJLabel;
private JButton calcJButton;
final FileChooser fc = new FileChooser();
  
private String xStr;
private String yStr;
  
public ControlFrame(String title)
{
super( title );
mainPanel = new JPanel( new BorderLayout() );
mainPanel.setSize(200, 250);
  
calcPanel = new JPanel( new FlowLayout() );
calcPanel.setSize(200, 200);

final DrawControlPanel drawPanel = new DrawControlPanel();
drawPanel.setSize(200, 200);

final FileChooser chooser = new FileChooser();
  
// set file name
String imageFileName = "C:/intro-prog-java/mediasources/mickey4.jpg";

final DrawImageControlPanel drawImagePanel = new DrawImageControlPanel(imageFileName);
drawImagePanel.setSize(800, 600);

// set sound name
String soundFileName = "C:/intro-prog-java/mediasources/thisisatest.wav";
  
final SoundControlPanel soundPanel = new SoundControlPanel(soundFileName);
soundPanel.setSize(200, 200);

this.setContentPane( mainPanel );
  
JMenu fileMenu = new JMenu( "File" );
fileMenu.setMnemonic( 'F' );
JMenuItem aboutItem = new JMenuItem( "About..." );
aboutItem.setMnemonic( 'A' );
  
fileMenu.add( aboutItem );
  
aboutItem.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
JOptionPane.showMessageDialog( ControlFrame.this,
"This application provides enhanced control over multimedia projects.",
"About", JOptionPane.PLAIN_MESSAGE );
}
} // End of anonymous inner class
);
  
final JMenuBar bar = new JMenuBar(); // Create a JMenuBar so we can attach menus to it.
setJMenuBar( bar ); // Attach the JMenuBar to the ControlFrame.
bar.add( fileMenu ); // Add the file menu to the JMenuBar.
  
final JMenu colorMenu = new JMenu( "Color" );
colorMenu.setMnemonic( 'C' );
  
JMenuItem redItem = new JMenuItem( "Red" );
colorMenu.add( redItem );
redItem.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawPanel.setFillColor( Color.RED );
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem blueItem = new JMenuItem( "Blue" );
colorMenu.add( blueItem );
blueItem.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawPanel.setFillColor( Color.BLUE );
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem magentaItem = new JMenuItem( "Magenta" );
colorMenu.add( magentaItem );
magentaItem.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawPanel.setFillColor( Color.MAGENTA );
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem cyanItem = new JMenuItem( "Cyan" );
colorMenu.add( cyanItem );
cyanItem.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawPanel.setFillColor( Color.CYAN );
repaint();
}
} // End of anonymous inner class
);

// create Image menu which allows image manipulation operations to be performed.
final JMenu imageMenu = new JMenu( "Image" );
imageMenu.setMnemonic( 'I' );
  
JMenuItem imageOp1Item = new JMenuItem( "Increase Red" );
imageMenu.add( imageOp1Item );
imageOp1Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawImagePanel.increaseRed();
repaint();
}
} // End of anonymous inner class
);   

JMenuItem imageOp2Item = new JMenuItem( "Increase Green" );
imageMenu.add( imageOp2Item );
imageOp2Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawImagePanel.increaseGreen();
repaint();
}
} // End of anonymous inner class
);   
  
JMenuItem imageOp3Item = new JMenuItem( "Increase Blue" );
imageMenu.add( imageOp3Item );
imageOp3Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawImagePanel.increaseBlue();
repaint();
}
} // End of anonymous inner class
);   
  
JMenuItem imageOp4Item = new JMenuItem( "Decrease Red" );
imageMenu.add( imageOp4Item );
imageOp4Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawImagePanel.decreaseRedForEach();
repaint();
}
} // End of anonymous inner class
);   

JMenuItem imageOp5Item = new JMenuItem( "Set Original" );
imageMenu.add( imageOp5Item );
imageOp5Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
drawImagePanel.setOriginalImage();
repaint();
}
} // End of anonymous inner class
);
  
// create Sound menu which loads a Sound object.
final JMenu soundMenu = new JMenu( "Sound" );
imageMenu.setMnemonic( 'O' );
  
JMenuItem soundOp1Item = new JMenuItem( "Play" );
soundMenu.add( soundOp1Item );
soundOp1Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
soundPanel.blockingPlay();
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem soundOp2Item = new JMenuItem( "Increase Volume" );
soundMenu.add( soundOp2Item );
soundOp2Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
soundPanel.increaseVolume();
soundPanel.blockingPlay();
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem soundOp3Item = new JMenuItem( "Decrease Volume" );
soundMenu.add( soundOp3Item );
soundOp3Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
soundPanel.decreaseVolume();
soundPanel.blockingPlay();
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem soundOp4Item = new JMenuItem( "Mirror" );
soundMenu.add( soundOp4Item );
soundOp4Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
soundPanel.mirrorFrontToBack();
soundPanel.blockingPlay();
repaint();
}
} // End of anonymous inner class
);
  
JMenuItem soundOp5Item = new JMenuItem( "Set Original" );
soundMenu.add( soundOp5Item );
soundOp5Item.addActionListener(
new ActionListener() // Beginning of anonymous inner class
{
public void actionPerformed( ActionEvent event )
{
soundPanel.setOriginalSound();
soundPanel.blockingPlay();
repaint();
}
} // End of anonymous inner class
);
  
  
JMenuItem calcPanelItem = new JMenuItem( "Calculate" );
calcPanelItem.setMnemonic( 'C' );
fileMenu.add( calcPanelItem );
calcPanelItem.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
//remove menus
bar.remove( colorMenu );
bar.remove( soundMenu );
bar.remove( imageMenu );
  
//remove panels
mainPanel.remove( drawPanel );
mainPanel.remove( widthJSlider );
mainPanel.remove( drawImagePanel );
mainPanel.remove( soundPanel );
  
xValTextField.setText("");
yValTextField.setText("");
calcJLabel.setText( "" );
mainPanel.add( calcPanel, BorderLayout.CENTER );
validate();
repaint();
}
}
);
  
JMenuItem drawPanelItem = new JMenuItem( "DrawPanel" );
drawPanelItem.setMnemonic( 'D' );
fileMenu.add( drawPanelItem );
drawPanelItem.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
//remove menus
bar.remove( soundMenu );
bar.remove( imageMenu );
  
//remove panels
mainPanel.remove( drawImagePanel );
mainPanel.remove( soundPanel );
mainPanel.remove( calcPanel );   
  
bar.add( colorMenu );   
drawPanel.setBackground( Color.WHITE );
mainPanel.add( drawPanel, BorderLayout.CENTER );
mainPanel.add( widthJSlider, BorderLayout.SOUTH );
validate();
repaint();
}
}
);
  
  
// Add a menu item named Show Picture to the File menu that will show the image of a Picture object.
JMenuItem drawImagePanelItem = new JMenuItem( "Show Picture" );
drawImagePanelItem.setMnemonic( 'S' );
fileMenu.add( drawImagePanelItem );
drawImagePanelItem.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
// remove menus for color and sound
bar.remove( colorMenu );
bar.remove( soundMenu );
  
// if sound is played, stop the sound
//soundPanel.stop();
  
// remove panels for draw, sound and calculation
mainPanel.remove( drawPanel );
mainPanel.remove( soundPanel );
mainPanel.remove( calcPanel );
  
// remove slider feature
mainPanel.remove( widthJSlider );
  
// set original image to be shown
drawImagePanel.setOriginalImage();
  
// add panel for showing image
mainPanel.add( drawImagePanel, BorderLayout.CENTER );
  
// add image menu
bar.add( imageMenu );

validate();
repaint();
}
}
);
  
// Add a menu item named Load Sound to the File menu that will load sound
JMenuItem loadSoundPanelItem = new JMenuItem( "Load Sound" );
loadSoundPanelItem.setMnemonic( 'L' );
fileMenu.add( loadSoundPanelItem );
loadSoundPanelItem.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
// remove menus for color and image
bar.remove( colorMenu );
bar.remove( imageMenu );
  
// remove panels for draw, draw image and calculation
mainPanel.remove( drawPanel );
mainPanel.remove( calcPanel );
mainPanel.remove( drawImagePanel );
  
// remove slider feature
mainPanel.remove( widthJSlider );
  
// set original sound
final FileChooser chooser = new FileChooser();
// final soundFileName = chooser.pickAFile();
soundPanel.setOriginalSound();
  
// add panel for controlling sound
mainPanel.add( soundPanel, BorderLayout.CENTER );
  
// add sound menu
bar.add( soundMenu );
  
validate();
repaint();
}
}
);

JMenuItem exitItem = new JMenuItem( "Exit" );
exitItem.setMnemonic( 'x' );
fileMenu.add( exitItem );
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
System.exit( 0 );
}
}
);
  
widthJSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 100, drawPanel.getOvalWidth() );
widthJSlider.setMajorTickSpacing( 10 );
widthJSlider.setPaintTicks( true );
  
widthJSlider.addChangeListener(
new ChangeListener()
{
public void stateChanged( ChangeEvent e )
{
drawPanel.setOvalWidth( widthJSlider.getValue() );
repaint();
}
}
);
  
xValTextField = new JTextField( 3 );
xValTextField.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
xStr = event.getActionCommand();
}
}
);   

calcPanel.add( xValTextField );

yValTextField = new JTextField( 3 );
yValTextField.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
yStr = event.getActionCommand();
}
}
);   

calcPanel.add( yValTextField );
  
calcJButton = new JButton( "Calculate" );   
calcJButton.addActionListener(
new ActionListener()
{
public void actionPerformed( ActionEvent event )
{
try {   
int x = Integer.parseInt( xStr );
int y = Integer.parseInt( yStr );
int result = x + y;
calcJLabel.setText(xStr + " + " + yStr + " = " + result);
}
catch (NumberFormatException e) {
JOptionPane.showMessageDialog( ControlFrame.this, "You must enter a valid number and then <ENTER> for each textbox!", "Invalid Input", JOptionPane.ERROR_MESSAGE );
e.printStackTrace();
}
}
}
);
calcPanel.add( calcJButton );
  
calcJLabel = new JLabel();
calcPanel.add( calcJLabel, BorderLayout.CENTER );
  
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

setSize( 200, 250 );
setVisible( true );
validate();
}
}

File: DrawImageControlPanel.java

// DrawImageControlPanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class DrawImageControlPanel extends JPanel
{

private String imageFileName;
private Picture pictObject;
private int x;
private int y;

public DrawImageControlPanel(String imageFileName)
{
//setSize(800, 600);
this.imageFileName = imageFileName;
this.pictObject = null;
this.x = 0;
this.y = 0;
}

public void paintComponent( Graphics g )
{
super.paintComponent( g ); // invoke the superclass paintComponent
this.setBackground( Color.WHITE );

// get BufferedImage object
BufferedImage bufferedImage = this.pictObject.getBufferedImage();
  
// get draw starting point after picture is rendered
this.x = (this.getWidth() - this.pictObject.getWidth() ) / 2;
this.y = ( this.getHeight() - this.pictObject.getHeight() ) / 2;
  
// draw to panel
g.drawImage(bufferedImage, x, y, null);
}

public void setOriginalImage()
{
this.pictObject = new Picture( this.imageFileName );
}

public void increaseRed()
{
this.pictObject.increaseRed();
}

public void decreaseRedForEach()
{
this.pictObject.decreaseRedForEach();
}

public void increaseBlue()
{
this.pictObject.increaseBlue();
}

public void increaseGreen()
{
this.pictObject.increaseGreen();
}

}

File: DrawControlApp.java

// DrawControlApp.java
import java.awt.Color;
import javax.swing.JFrame;

public class DrawControlApp
{
public static void main( String args[] )
{
JFrame frame = new ControlFrame( "Controlling Multimedia Projects..." );
frame.setSize( 900, 700 );
}
}

Note: I know you need more files, but it won't accept the question if I include them.

Explanation / Answer

note: dure to limited character i posted only half code

ControlFrame.java
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Color;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JMenuBar;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.ItemListener;
import java.awt.event.ItemEvent;
import javax.swing.JOptionPane;
import javax.swing.JSlider;
import javax.swing.SwingConstants;
import javax.swing.event.ChangeListener;
import javax.swing.event.ChangeEvent;
import java.awt.BorderLayout;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JOptionPane;


public class ControlFrame extends JFrame
{
private JPanel mainPanel;
private final JPanel calcPanel;
private JSlider widthJSlider;
private JTextField xValTextField;
private JTextField yValTextField;
private JLabel calcJLabel;
private JButton calcJButton;

private String xStr;
private String yStr;

public ControlFrame(String title)
{
    super( title );
    mainPanel = new JPanel( new BorderLayout() );
    //mainPanel.setSize(900, 700);  
  
    calcPanel = new JPanel( new FlowLayout() );  
    calcPanel.setSize(200, 200);  

    final DrawControlPanel drawPanel = new DrawControlPanel();
    drawPanel.setSize(200, 200);
  
    // set image name
    String currentDir = System.getProperty("user.dir");
    String imageFileName = currentDir + "\" + "butterfly.png";
  
    // create DrawImageControlPanel
    final DrawImageControlPanel drawImagePanel = new DrawImageControlPanel( imageFileName );
  
    // set sound name
    String soundFileName = currentDir + "\" + "thisisatest.wav";
  
    // create SoundControlPanel
    final SoundControlPanel soundPanel = new SoundControlPanel( soundFileName );

    this.setContentPane( mainPanel );
  
    JMenu fileMenu = new JMenu( "File" );
    fileMenu.setMnemonic( 'F' );
    JMenuItem aboutItem = new JMenuItem( "About..." );
    aboutItem.setMnemonic( 'A' );
    fileMenu.add( aboutItem );
    aboutItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          JOptionPane.showMessageDialog( ControlFrame.this,
                                      "This application provides enhanced control over multimedia projects.",
                                      "About", JOptionPane.PLAIN_MESSAGE );
        }
     } // End of anonymous inner class
    );
    
    final JMenuBar bar = new JMenuBar(); // Create a JMenuBar so we can attach menus to it.
    setJMenuBar( bar ); // Attach the JMenuBar to the ControlFrame.
    bar.add( fileMenu ); // Add the file menu to the JMenuBar.

    final JMenu colorMenu = new JMenu( "Color" );
    colorMenu.setMnemonic( 'C' );
  
    JMenuItem redItem = new JMenuItem( "Red" );
    colorMenu.add( redItem );
    redItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawPanel.setFillColor( Color.RED );
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem blueItem = new JMenuItem( "Blue" );
    colorMenu.add( blueItem );
    blueItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawPanel.setFillColor( Color.BLUE );
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem magentaItem = new JMenuItem( "Magenta" );
    colorMenu.add( magentaItem );
    magentaItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawPanel.setFillColor( Color.MAGENTA );
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem cyanItem = new JMenuItem( "Cyan" );
    colorMenu.add( cyanItem );
    cyanItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawPanel.setFillColor( Color.CYAN );
          repaint();
        }
     } // End of anonymous inner class
    );
  
    // create Image menu which allows image manipulation operations to be performed.
    final JMenu imageMenu = new JMenu( "Image" );
    imageMenu.setMnemonic( 'I' );
  
    JMenuItem imageOp1Item = new JMenuItem( "Increase Red" );
    imageMenu.add( imageOp1Item );
    imageOp1Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.increaseRed();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp2Item = new JMenuItem( "Increase Blue" );
    imageMenu.add( imageOp2Item );
    imageOp2Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.increaseBlue();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp3Item = new JMenuItem( "Increase Green" );
    imageMenu.add( imageOp3Item );
    imageOp3Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.increaseGreen();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp4Item = new JMenuItem( "Sepia Tint" );
    imageMenu.add( imageOp4Item );
    imageOp4Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.sepiaTint();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp5Item = new JMenuItem( "Blur" );
    imageMenu.add( imageOp5Item );
    imageOp5Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.blurr();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp6Item = new JMenuItem( "Grayscale" );
    imageMenu.add( imageOp6Item );
    imageOp6Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.grayscale();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp7Item = new JMenuItem( "Black And White" );
    imageMenu.add( imageOp7Item );
    imageOp7Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.edgeDetection();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem imageOp8Item = new JMenuItem( "Set Original" );
    imageMenu.add( imageOp8Item );
    imageOp8Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          drawImagePanel.setOriginalImage();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    // create Sound menu which loads a Sound object.
    final JMenu soundMenu = new JMenu( "Sound" );
    imageMenu.setMnemonic( 'O' );
  
    JMenuItem soundOp1Item = new JMenuItem( "Play" );
    soundMenu.add( soundOp1Item );
    soundOp1Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.play();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp2Item = new JMenuItem( "Stop" );
    soundMenu.add( soundOp2Item );
    soundOp2Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.stop();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp3Item = new JMenuItem( "Increase Volume" );
    soundMenu.add( soundOp3Item );
    soundOp3Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.increaseVolume();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp4Item = new JMenuItem( "Decrease Volume" );
    soundMenu.add( soundOp4Item );
    soundOp4Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.decreaseVolume();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp5Item = new JMenuItem( "Mirror" );
    soundMenu.add( soundOp5Item );
    soundOp5Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.mirror();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp6Item = new JMenuItem( "Echo" );
    soundMenu.add( soundOp6Item );
    soundOp6Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.echo();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp7Item = new JMenuItem( "Reverse" );
    soundMenu.add( soundOp7Item );
    soundOp7Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.reverse();
          repaint();
        }
     } // End of anonymous inner class
    );
  
    JMenuItem soundOp8Item = new JMenuItem( "Set Original" );
    soundMenu.add( soundOp8Item );
    soundOp8Item.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          soundPanel.setOriginalSound();
          repaint();
        }
     } // End of anonymous inner class
    );
   
    JMenuItem calcPanelItem = new JMenuItem( "Calculate" );
    calcPanelItem.setMnemonic( 'C' );
    fileMenu.add( calcPanelItem );
    calcPanelItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          // remove menus for color, image and sound
          bar.remove( colorMenu );
          bar.remove( imageMenu );
          bar.remove( soundMenu );
        
           // if sound is played, stop the sound
          soundPanel.stop();
        
          // remove panels for draw, draw image and sound
          mainPanel.remove( drawPanel );
          mainPanel.remove( drawImagePanel );
          mainPanel.remove( soundPanel );
        
          // remove slider feature
          mainPanel.remove( widthJSlider );
        
          xValTextField.setText("");
          yValTextField.setText("");
          calcJLabel.setText( "" );
          mainPanel.add( calcPanel, BorderLayout.CENTER );
          validate();
          repaint();
        }
      }
    );
  
    JMenuItem drawPanelItem = new JMenuItem( "DrawPanel" );
    drawPanelItem.setMnemonic( 'D' );
    fileMenu.add( drawPanelItem );
    drawPanelItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          // remove menus for image and sound
          bar.remove( imageMenu );
          bar.remove( soundMenu );
        
          // if sound is played, stop the sound
          soundPanel.stop();
        
          // remove panels for calculate, image and sound
          mainPanel.remove( calcPanel );
          mainPanel.remove( drawImagePanel );
          mainPanel.remove( soundPanel );
        
          drawPanel.setBackground( Color.WHITE );
          mainPanel.add( drawPanel, BorderLayout.CENTER );
          mainPanel.add( widthJSlider, BorderLayout.SOUTH );   
        
          bar.add( colorMenu );
          validate();
          repaint();
        }
      }
    );
  
    // Add a menu item named Show Picture to the File menu that will show the image of a Picture object.
    JMenuItem drawImagePanelItem = new JMenuItem( "Show Picture" );
    drawImagePanelItem.setMnemonic( 'S' );
    fileMenu.add( drawImagePanelItem );
    drawImagePanelItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          // remove menus for color and sound
          bar.remove( colorMenu );
          bar.remove( soundMenu );
        
          // if sound is played, stop the sound
          soundPanel.stop();
        
          // remove panels for draw, sound and calculation
          mainPanel.remove( drawPanel );
          mainPanel.remove( soundPanel );
          mainPanel.remove( calcPanel );
        
          // remove slider feature
          mainPanel.remove( widthJSlider );
        
          // set original image to be shown
          drawImagePanel.setOriginalImage();

          // add panel for showing image
          mainPanel.add( drawImagePanel, BorderLayout.CENTER );
        
          // add image menu
          bar.add( imageMenu );
        
          validate();
          repaint();
        }
      }
    );
  
    // Add a menu item named Load Sound to the File menu that will load sound
    JMenuItem loadSoundPanelItem = new JMenuItem( "Load Sound" );
    loadSoundPanelItem.setMnemonic( 'L' );
    fileMenu.add( loadSoundPanelItem );
    loadSoundPanelItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          // remove menus for color and image
          bar.remove( colorMenu );
          bar.remove( imageMenu );
        
          // remove panels for draw, draw image and calculation
          mainPanel.remove( drawPanel );
          mainPanel.remove( calcPanel );
          mainPanel.remove( drawImagePanel );
        
          // remove slider feature
          mainPanel.remove( widthJSlider );
        
          // set original sound
          soundPanel.setOriginalSound();
        
          // add panel for controlling sound
          mainPanel.add( soundPanel, BorderLayout.CENTER );
        
          // add sound menu
          bar.add( soundMenu );
        
          validate();
          repaint();
        }
      }
    );
   
    JMenuItem exitItem = new JMenuItem( "Exit" );
    exitItem.setMnemonic( 'x' );
    fileMenu.add( exitItem );
    exitItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          System.exit( 0 );
        }
      }
    );
  
    widthJSlider = new JSlider( SwingConstants.HORIZONTAL, 0, 100, drawPanel.getOvalWidth() );
    widthJSlider.setMajorTickSpacing( 10 );
    widthJSlider.setPaintTicks( true );
  
    widthJSlider.addChangeListener(
      new ChangeListener()
      {
        public void stateChanged( ChangeEvent e )
        {
          drawPanel.setOvalWidth( widthJSlider.getValue() );
          repaint();
        }
      }
    );
      
    xValTextField = new JTextField( 3 );
    xValTextField.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          xStr = event.getActionCommand();
        }
      }
    );                                                                     

    calcPanel.add( xValTextField );

    yValTextField = new JTextField( 3 );
    yValTextField.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          yStr = event.getActionCommand();
        }
      }
    );   

    calcPanel.add( yValTextField );
  
    calcJButton = new JButton( "Calculate" );
    calcJButton.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          try {     
            int x = Integer.parseInt( xStr );
            int y = Integer.parseInt( yStr );
            int result = x + y;
            calcJLabel.setText(xStr + " + " + yStr + " = " + result);
          }
          catch (NumberFormatException e) {
            JOptionPane.showMessageDialog( ControlFrame.this, "You must enter a valid number and then <ENTER> for each textbox!", "Invalid Input", JOptionPane.ERROR_MESSAGE );
            e.printStackTrace();
          }
        }
      }
    );
    calcPanel.add( calcJButton );
  
    calcJLabel = new JLabel();
    calcPanel.add( calcJLabel, BorderLayout.CENTER );
  
    setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );

    setSize( 200, 250 );
    setVisible( true );
    validate();
}
}

DrawImageControlPanel.java

// DrawImageControlPanel.java
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;

public class DrawImageControlPanel extends JPanel
{

private String imageFileName;
private Picture pictObject;
private int x;
private int y;

public DrawImageControlPanel(String imageFileName)
{
    //setSize(800, 600);
    this.imageFileName = imageFileName;
    this.pictObject = null;
    this.x = 0;
    this.y = 0;
}

public void paintComponent( Graphics g )
{
    super.paintComponent( g ); // invoke the superclass paintComponent
    this.setBackground( Color.WHITE );

    // get BufferedImage object
    BufferedImage bufferedImage = this.pictObject.getBufferedImage();
  
    // get draw starting point after picture is rendered
    this.x = (this.getWidth() - this.pictObject.getWidth() ) / 2;
    this.y = ( this.getHeight() - this.pictObject.getHeight() ) / 2;
  
    // draw to panel
    g.drawImage(bufferedImage, x, y, null);
}

public void setOriginalImage()
{
    this.pictObject = new Picture( this.imageFileName );
}

public void increaseRed()
{
    this.pictObject.increaseRed();
}

public void increaseBlue()
{
    this.pictObject.increaseBlue();
}

public void increaseGreen()
{
    this.pictObject.increaseGreen();
}
  
public void sepiaTint()
{
    this.pictObject.sepiaTint();
}
    
public void blurr()
{
    // blur the the whole picture
    this.pictObject.blurr(2, // number of pixels to average in all directions
                          0, // starting x point
                          0, // starting y point
                          this.pictObject.getWidth(), // blur all the x pixels
                          this.pictObject.getHeight()); // blur all the y pixels
}

public void grayscale()
{
    this.pictObject.grayscale();
}

public void edgeDetection()
{
    this.pictObject.edgeDetection();
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote