I have been given the assignment to do the following: Create a class called Draw
ID: 2246522 • Letter: I
Question
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 <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 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){
}
}
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..." );
frame.setSize( 900, 700 );
}
}
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(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();
}
}
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
// 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();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.