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

Background to question: The class Picture extends Simple Picture The class Simpl

ID: 2247744 • Letter: B

Question

Background to question:

The class Picture extends Simple Picture
The class Simple Picture implements Digital Picture

Picture contains the following methods (There are more methods in this class, but I know I don't need them for this assignment):
1. public void increaseRed()
2. public void increaseGreen()
3. public void increaseBlue()
4. public void blur(int startX, int endX, int startY, int endY)


Simple Picture contain the following methods:
1. public String getExtension()
2. public void copyPicture(SimplePicture sourcePicture)
3. public void setAllPixelsToAColor(Color color)
4. public BufferedImage getBufferedImage()
5. public Graphics getGraphics()
6. public Graphics2D createGraphics()
7. public String getFileName()
8. public String getTitle()
8. public void setTitle(String title)
9. public int getWidth()
10. public int getHeight()
11. public PictureFrame getPictureFrame()
12. public void setPictureFrame(PictureFrame pictureFrame)
13. public Image getImage()
14. public int getBasicPixel(int x, int y)
15. public void setBasicPixel(int x, int y, int rgb)
16. public Pixel getPixel(int x, int y)
17. public Pixel[] getPixels()
18. public void load(Image image)
19. public void show()
20. public void hide()
21. public void setVisible(boolean flag)
22. public void explore()
23. public void repaint()
24. public boolean load(String fileName)
25. public void addMessage(String message, int xPos, int yPos)
26. public Picture getPictureWithHeight(int height)
27. public boolean loadPictureAndShowIt(String fileName)
28. public void write(String fileName)
29. public static void setMediaPath(String directory)
30. public static String getMediaPath(String fileName)
30. public Rectangle2D getTransformEnclosingRect(AffineTransform trans)
31. public String toString()

The class ControlFrame includes the setup of the menu and event handlers for this assignment.
THe class DrawControlApp contains the main method
The class DrawControlPanel helps the DrawPanel on the File menu
The class DrawImageControlPanel helps the DrawImagePanel on the file menu (This is the one I am having a problem with). This class should help the Show Picture on the File menu.

A few have tried to answer this question...but they didn't use the methods already in the Picture Class.

Question: I need to understand how to get the event handler in the ControlFrame class to be able to Show Picture from the file menu (close to the bottom of the ControlFrame class) and then be able to select the Image menu and be

able to call the IncreaseRed method and have it display the picture with the IncreaseRed method implemented. I should be using the DrawImageControlPanel class to do most of this work. Like the DrawControlPanel class does the work

for the Draw Panel on the file menu.

Here is a copy of the actual files: (ask for them, because when I try to include them it won't let me post the question)

Explanation / Answer

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..." );
   }

   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;


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);  
  
    final DrawImageControlPanel drawImage = new DrawImageControlPanel();
    drawImage.setSize(300, 300);
  
    final PlaySoundControlPanel playSoundPanel = new PlaySoundControlPanel();
      
    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
    );
  
    final JMenu imageMenu = new JMenu( "Image" );
    imageMenu.setMnemonic( 'I' );
      
    JMenuItem showImageItem = new JMenuItem( "Show Image" );
    showImageItem.setMnemonic( 'S' );
    fileMenu.add(showImageItem);
    showImageItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event)
        {
          bar.remove( colorMenu );       
          mainPanel.remove( calcPanel );
          bar.add(imageMenu);  
         }
       }
    );
  
    JMenuItem increaseRedItem = new JMenuItem( "Increase Red" );
    increaseRedItem.setMnemonic('Q');
    imageMenu.add( increaseRedItem );
    increaseRedItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          mainPanel.add( drawImage, BorderLayout.CENTER );
          drawImage.increaseRed(10);
        }
     }
    );
  
    JMenuItem increaseGreenItem = new JMenuItem( "Increase Green" );
    increaseGreenItem.setMnemonic('L');
    imageMenu.add( increaseGreenItem );
    increaseGreenItem.addActionListener(
      new ActionListener() // Beginning of anonymous inner class
      {
        public void actionPerformed( ActionEvent event )
        {
          mainPanel.add( drawImage, BorderLayout.CENTER );
          drawImage.increaseGreen();
          }
      }
    );
  
    final JMenu soundMenu = new JMenu( "Sound" );
    soundMenu.setMnemonic( 'Y' );
      
    JMenuItem playSoundItem = new JMenuItem ("Play Sound");
    playSoundItem.setMnemonic( 'W' );
    fileMenu.add( playSoundItem );
    playSoundItem.addActionListener(
       new ActionListener()
    {
      public void actionPerformed(ActionEvent event)
      {
          bar.remove( colorMenu );
          bar.remove(imageMenu);
          mainPanel.remove( calcPanel );
          bar.add(imageMenu);
        
      }
    }
    );
  
    JMenuItem playTheSound = new JMenuItem ("Play the Sound");
    playTheSound.setMnemonic('X');
    soundMenu.add(playTheSound);
    playTheSound.addActionListener(
       new ActionListener()
    {
    public void actionPerformed(ActionEvent event)
    {
      playSoundPanel.playingSound();
    }
    }                     
    );
  
    JMenuItem playMirroredSound = new JMenuItem ("Play Mirrored Sound");
    playMirroredSound.setMnemonic('Z');
    soundMenu.add(playMirroredSound);
    playMirroredSound.addActionListener(
       new ActionListener()
    {
      public void actionPerformed(ActionEvent event)
    {
      playSoundPanel.playingMirroredSound();
    }
    }                     
    );

    JMenuItem increaseVolumeSound = new JMenuItem ("Turn up the volume");
    increaseVolumeSound.setMnemonic('K');
    soundMenu.add(increaseVolumeSound);
    increaseVolumeSound.addActionListener(
       new ActionListener(){
      public void actionPerformed(ActionEvent event){
      playSoundPanel.increaseVolume();
      }
    }
    );
  
    JMenuItem calcPanelItem = new JMenuItem( "Calculate" );
    calcPanelItem.setMnemonic( 'C' );
    fileMenu.add( calcPanelItem );
    calcPanelItem.addActionListener(
      new ActionListener()
      {
        public void actionPerformed( ActionEvent event )
        {
          bar.remove( colorMenu );
          bar.remove( imageMenu );
          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 );
          bar.remove( imageMenu );
          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 <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();
}
}

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;
}
}

DrawImageControlPanel.java

import java.awt.*;
import java.awt.font.*;
import java.awt.geom.*;
import java.text.*;
import javax.swing.JPanel;


public class DrawImageControlPanel extends JPanel{

public class Picture {

public static void main(String args[]){
   String fileName = "c:/intro-prog-java/mediasources/jenny-red.jpg";
   Picture jennyPicture = new Picture(fileName);
}

public void increaseRed(int inputFactor){
  
   Pixel[] pixelArray = this.getPixels();
   Pixel pixel = null;
   int value = 0;
   int index = 0;

   while(index < pixelArray.length)
   {
     pixel = pixelArray[index];
     value = pixel.getRed();
     value = (int)(value * inputFactor);
     pixel.setRed(value);
     index = index+1;
   }
}

public void increaseGreen(){
  
   Pixel[] pixelArray = this.getPixels();
   Pixel pixel = null;
   int value = 0;
   int index = 0;

   while(index < pixelArray.length)
   {
     pixel = pixelArray[index];
     value = pixel.getGreen();
     value = (int)(value * 1.5);
     pixel.setGreen(value);
     index = index+1;
   }
}

}
}

PlaySoundControlPanel.java

public class PlaySoundControlPanel extends JPanel{

public void playingSound(){
String f = "c:/intro-prog-java/mediasources/gettysburg10.wav";
Sound s = new Sound(f);
s.play();

}

public void playingMirrorSound(){
String e = "c:/intro-prog-java/mediasources/gettysburg10.wav";
Sound g = new Sound(f);
int lengthMirr = mirrored.getLength();
int mirrorPoint = lengthMirr;
int valueMirr = 0;
for(int i = 0; i < mirrorPoint; i++){
   valueMirr = this.getSampleValueAt(i);
   this.setSampleValueAt(lengthMirr - 1 - i, valueMirr);
}
}

public void increaseVolume()
{
  
    String t = "c:/intro-prog-java/mediasources/gettysburg10.wav";
    Sound h = new Sound(f);
    SoundSample[] sampleArray = this.getSamples();
    int value = 0;
  
    //Used ForEach Loop
    for(SoundSample sample: sampleArray){
      value = sample.getValue();
      sample.setValue(value*2);
    }
}


}

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