Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have been given the assignment to do the following: Create a class called DrawImageControlPanel. Modify the code presented in the Module 5 slides (also

I have been given the assignment to do the following:

Create a class called DrawImageControlPanel. Modify the code presented in the Module 5 slides (also posted separately on the Resources page) such that this new JPanel is integrated as part of the application. You are integrating the new functionality, not replacing!

Add a menu item named Show Picture to the File menu that will show the image of a Picture object. This image should be displayed within the new JPanel. To do this, look for a method that allows you to get the image from the Picture object. When that menu item is selected another menu named Image should be displayed that allows image manipulation operations to be performed. Support at least two image manipulation operations. These can be the same operations that you have used previously, or they can be new ones.

Add another menu item to the File menu that "loads" a Sound object. When that menu item is selected another menu named Sound should be displayed that allows sound operations to be performed. Support at least two sound operations. The first operation is to play the sound. The second operation is to play the mirror of the sound.

Add one more feature, for example a new image operation or a new sound operation. You will need to add at least one additional menu item to the Image or Sound menu to allow for the selection of this additional operation. If it makes sense, given the feature you are adding, you may want to add an additional menu item to the File menu as well.

If I could just edit the ControlFrame.java file I think I could at least get started, but my understanding is that I am to use the new DrawImageControlPanel.java file to put my code in using composition. Can you please help me get started by adding an item to the file menu? Below are the files:

File ControlFrame.java:

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(200, 250); calcPanel = new JPanel( new FlowLayout() ); calcPanel.setSize(200, 200);

final DrawControlPanel drawPanel = new DrawControlPanel(); drawPanel.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 ); JMenuItem calcPanelItem = new JMenuItem( "Calculate" ); calcPanelItem.setMnemonic( 'C' ); fileMenu.add( calcPanelItem ); calcPanelItem.addActionListener( new ActionListener() { public void actionPerformed( ActionEvent event ) { bar.remove( colorMenu ); mainPanel.remove( drawPanel ); 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 ) { bar.add( colorMenu ); mainPanel.remove( calcPanel ); drawPanel.setBackground( Color.WHITE ); mainPanel.add( drawPanel, BorderLayout.CENTER ); mainPanel.add( widthJSlider, BorderLayout.SOUTH ); 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 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 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..." ); } }

File: DrawControlPanel.java

// DrawControlPanel.java import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel;

public class DrawControlPanel extends JPanel { private Color fillColor = Color.CYAN; private int ovalWidth = 90; public DrawControlPanel() { setSize( 200, 200 ); } public void paintComponent( Graphics g ) { super.paintComponent( g ); // invoke the superclass paintComponent this.setBackground( Color.WHITE ); g.setColor( fillColor ); g.fillOval( 50, 50, ovalWidth, 60 ); } void setFillColor(Color fillColor) { this.fillColor = fillColor; } void setOvalWidth(int ovalWidth) { this.ovalWidth = ovalWidth; }

int getOvalWidth() { return ovalWidth; } }

File DrawImageControlPanel.java

import java.awt.Color; import java.awt.Graphics; import javax.swing.JPanel;

public class DrawImageControlPanel extends JPanel{

private ControlFrame controlFrame; public DrawImageControlPanel(ControlFrame thecontrolFrame){ } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Modern Datalog Engines In Databases

Authors: Bas Ketsman ,Paraschos Koutris

1st Edition

1638280428, 978-1638280422

More Books

Students also viewed these Databases questions