Question
Hi if you can fix my code; The orginal problem along with answer from your site CHEGG the question with the answer is under this
Hi if you can fix my code;
The orginal problem along with answer from your site CHEGG the question with the answer is under this URL
https://www.chegg.com/homework-help/java-how-to-program-9th-edition-chapter-25-problem-16E-solution-9780132575669
Here is my code after spending about five hours to type the code;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.io.*;
//class definition
public class Drawing extends JFrame {
private JInternalFrame[] internalframes;
private JMenuItem[] menuframe;
private int totalcount=0;
private JDesktopPane desktoppane;
private JMenuItem menuItem;
private JMenu panelMenu;
private static final int maximum =20;
// constructor
public Drawing ()
{
// display title
super( "Drawing program");
// create an object
desktoppane = new JDesktopPane();
getContentPane().add(desktoppane);
internalframes = new JInternalFrame[maximum];
menuframe = new JMenuItem [maximum];
JMenuBar menuBar = new JMenuBar();
JMenu fileMenu = new JMenu("file");
fileMenu.setMnemonic('F');
// create a new frame
menuItem = new JMenuItem ("New");
menuItem.setMnemonic('n');
fileMenu.add (menuItem);
menuItem.addActionListener(
new ActionListener()
{
public void actionPerformed ( ActionEvent event )
{
String title = "picture" + ( totalcount +1);
// creatw the menu item
internalframes [ totalcount] = new DrawFrame( title, Drawing.this );
desktoppane.add(internalframes [totalcount]);
internalframes[ totalcount].setVisible(true);
//create the menu item
menuframe[ totalcount] = new JMenuItem (title);
panelMenu.add(menuframe[totalcount]);
menuframe[totalcount].addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
String caller = event.getActionCommand();
int number =
Integer.parseInt(caller.substring(7));
internalframes [number-1].toFront();
}
}
);
totalcount++;
if (totalcount==maximum)
menuItem.setEnabled(false);
}
}
);
JMenuItem exitItem = new JMenuItem ("Exit");
exitItem.setMnemonic('e');
fileMenu.add(exitItem);
menuBar.add(fileMenu);
exitItem.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
);
panelMenu = new JMenu("window");
menuBar.add(panelMenu);
setJMenuBar (menuBar);
setSize (900, 900);
setVisible (true);
}
public void frameClosed (String window)
{
int number = Integer.parseInt(window.substring( 7));
panelMenu.remove(menuframe[number-1]);
menuframe[ number-1] = null;
}
// main function
public static void main ( String args[])
{
Drawing appliction = new Drawing ();
appliction.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
import java.awt.event.ItemEvent;
import java.awt.event.MouseEvent;
import javax.swing.JComboBox;
import javax.swing.JInternalFrame;
import javax.swing.JSlider;
import javax.swing.JTextField;
public class DrawFrame extends JInternalFrame
{
private String name;
private DrawPanel drawPanel;
private JComboBox cBox;
private Drawing mains;
// constructor
public DrawFrame ( String title, Drawing caller)
{
super (title, true, true, true, true);
mains = caller;
name = title;
// create an object
MyColorChooser colorChooser = new MyColorChooser();
final JSlider red = colorChooser.getRedSlider();
red.addChangeListener(new ChangeListener() {
public void stateChanged( ChangeEvent event)
{
drawpanel.setRed( red.getValue());
}
}
);
final JTextField redField = colorChooser.getRedDisplay();
redField.addActionListener(new ActionListener() {
public void ActionPerformed (ActionEvent event)
{
drawPanel.setRed(Integer.parseInt(redField.getText()));
}
}
);
final JSlider green = colorChooser.getGreenSlider();
green.addChangeListener(new ChangeListener() {
public void stateChanged( ChangeEvent event)
{
drawpanel.setRed( green.getValue());
}
}
);
final JTextField greenField = colorChooser.getGreenDisplay();
greenField.addActionListener(new ActionListener() {
public void ActionPerformed (ActionEvent event)
{
drawPanel.setGreen(Integer.parseInt(greenField.getText()));
}
}
);
final JSlider blue= colorChooser.getBlueSlider();
blue.addChangeListener(new ChangeListener() {
public void stateChanged( ChangeEvent event)
{
drawpanel.setRed( blue.getValue());
}
}
);
final JTextField blueField = colorChooser.getBlueDisplay();
blueField.addActionListener(new ActionListener() {
public void ActionPerformed (AcionEvent event)
{
drawPanel.setBlue(Integer.parseInt(blueField.getText()));
}
}
);
drawPanel= new DrawPanel();
drawPanel.addMouseListener( new MouseAdapter()
{
public void mousePressed (MouseEvent event) {
drawPanel.createShape( event.getX(), event.getY();
public void mouseReleased (MouseEvent event) {
drawPanel.finishShape();
}
}
);
drawPanel.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged( MouseEvent event) {
drawPanel.resizeShape( event.getX(),event.getY());
}
}
);
ShapePanel ShapePanel = new ShapePanel();
JCheckBox fill = ShapePanel. getFil();
fill.addItemListtener(new ItemListener() {
public void itemStateChanged (ItemEvent event) {
if ( event.getStateChange()== ItemEvent.SELECTED)
drawPanel.setFill ( true);
else
drawPanel.setFill( false);
}
}
);
cBox =ShapePanel.getChooser();
cBox,addItemListener( new ItemListener() {
public void itemStateChanged( ItemEvent event) {
drawPanel.setShape((String)cBox.getSelectedItem());
}
}
);
Container container = getContaintPane();
container.setLayout(new BorderLayout());
container.add( shapePanel, BorderLayout.North);
container.add( drawPanel, BorderLayout.Center);
container.add( colorChooser, BorderLayout.South);
addInternalFrameListener(
new InternalFrameAdapter() {
public void internalFraameClosed ( InternalFrameEvent e)
{
mains.frameClosed(name);
}
}
);
setSize (300, 300);
}
}
import java.awt.Graphics;
public class DrawPanel extends JPanel {
private MyShape shape;
private boolean fill;
private int red, green, blue;
public DrawPanel ( MyShape s);
{
shape = s;
}
public DrawPanel()
{
shape = null;
}
// drawcolored rectangle
public void paint Component ( Graphics g)
{
super. paintComponent( g );
if ( shape ! = null)
shape, draw( g );
}
public void createShape ( int x, int y, String type)
{
Color color = new Color( red, green, blue);
if (type.equals("Rectangle"))
shape = new MyRectangle(x, y, x, y, color, fill);
else
if (type.equals("Line"))
shape = new MyLine(x, y, x, y, color);
else
shape = new MyOval( x, y, x, y, color, fill);
repaint ();
}
public void resizeShape (int x, int y)
{
shape.setX2(X);
shape.setY2(y);
repaint();
}
public void setFill(boolean f) {
fill = f;
if (shape instanceof MyBounded)
((MyBounded)shape).setFill( f);
repaint();
}
public void setShape (String type)
{
int x1 = shape.getX1();
int y1 = shape.getY1();
int x2 = shape.getX2();
int y2 = shape.getY2();
Color color = new Color ( red, green, blue);
if (type.equals("Rectangle"))
shape = new MyRectangle (x1, y1, x2, y2, color, fill);
else
if (type.equals(" Line"))
shape = new MyLine (x1, y1, x2, y2, color);
else
shape = new MyOval(x1, y1, x2, y2, color, fill);
repaint();
}
public void setRed( int r)
{
red = r;
shape.setColor( new Color ( red, green, blue));
repaint();
}
public void setGreen( int g)
{
green = g;
shape.setColor( new Color ( red, green, blue));
repaint();
}
public void setBlue( int b)
{
blue = b;
shape.setColor( new Color ( red, green, blue));
repaint();
}
}
public abstract class MyBounded extends MyShape{
private boolean filled;
public MyBounded()
{
super ();
setFilled ( false);
}
public MyBounded (int x1, int y1, int x2, int y2, boolean f )
{
super (x1, y1, x2, y2 );
}
public MyBounded (int x1, int y1, int x2, int y2, Color c, boolean f)
{
super (x1, y1, x2, y2, c );
setFilled (f);
}
public final void setAll (int x1, int y1, int x2, int y2, Color c, boolean f )
{
super.setAll(x1, y1, x2, y2, c);
setFilled ( f);}
public void setFilled ( boolean f)
{
filled = f;
}
public boolean getFilled ()
{
return filled;
}
public int getUpperLeftX()
{
return Math.min ( getX1(), getX2() );
}
public int getUpperleftY()
{
return Math.min ( getY2(), GETy2() );
}
public int getWidth ()
{
return Math.abs( getX1()- getx2() );
}
public int getHeight()
{
return Math.abs( getY1()- getY2() );
}
}
import java.awt.Color;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
public class MyColorChooser extends JPanel {
private JSlinder redSlinder, blueSlinder, greenSlinder;
private JTextField redDisplay, blueDisplay, greenDisplay;
private JLael redLabel, blueLabel, greenLabel;
private Color color;
public MyColorChooser4()
{
redLabel = new JLabel(" red:");
redSlinder=
new JSlinder(SwingConstants.HORIZONTAL, 0, 255, 1);
redDisplay= new JTextField( " 0", 4);
greenLabel = new JLabel(" green:");
greenSlinder=
new JSlinder(SwingConstants.HORIZONTAL, 0, 255, 1);
greenDisplay= new JTextField( " 0", 4);
blueLabel = new JLabel("blue:");
blueSlinder=
new JSlinder(SwingConstants.HORIZONTAL, 0, 255, 1);
blueDisplay= new JTextField( " 0", 4);
LsetLayout( new GridLayout( 3, 3));
add ( redLabel );
add ( redSlinder );
add ( redDisplay );
dd ( greenLabel );
add ( greenSlinder );
add (greendDisplay );
add ( blueLabel );
add ( blueSlinder );
add ( blueDisplay );
redSlinder.addChangeListener( new ChangeHandler());
greenSlinder.addChangeListener( new ChangeHandler());
blueSlinder.addChangeListener( new ChangeHandler());
redDisplay.addActionListener( new ActionHandler());
greenDisplay.addActionListener( new ActionHandler());
blueDisplay.addActionListener( new ActionHandler());
color = Color.black;
}
public void SetColor ( Color c)
{
color = c;
redSlideer.setValue (c.getRed());
redDisplay.setText(String.valueof( c.getRed()));
greenSlideer.setValue (c.getGreen());
greenDisplay.setText(String.valueof( c.getGreen()));
blueSlideer.setValue (c.getBlue());
bluedDisplay.setText(String.valueof( c.getBlue()));
}
public Color getColor()
{
return color;
}
public JSlinder getRedSlinder()
{
return redSlinder;
}
public JSlinder getgreenSlinder()
{
return greenSlinder;
}
public JSlinder getblueSlinder()
{
return blueSlinder;
}
// get green text field
public JTextField getredDisplay()
{
return redDisplay;
}
public JTextField getgreenDisplay()
{
return greenDisplay;
}
public JTextField getblueDisplay()
{
return blueDisplay;
}
private class ChangeHandler implements ChangeListener{
public void stateChanged ( ChangedEvent event)
{
int red = redSlinder.getValue();
int green = greenSlinder.getValue();
int blue = blueSlinder.getValue();
color = new Color ( red, green, blue);
redDisplay.setText( String.valueOf(red));
greenDisplay.setText( String.valueOf(green));
blueDisplay.setText( String.valueOf( blue));
}
}
private class ActioHandler implements ActionListener{
public void ActionPerformed ( ActionEvent event )
{
int red = Integer.parseInt(redDisplay.getText());
int green = Integer.parseInt(greenDisplay.getText());
int blue = Integer.parseInt(blueDisplay.getText());
color = new Color ( red, gree, blue);
redSlinder.setValue(red);
greenSlinder.setValue( green);
blueSlinder.setValue( blue);
}
}
}
import java.awt.Graphics;
public class MyLine extends MyShape {
public MyLine ()
{
super ();
}
public MyLine ( int x1, int y1, int x2, int y2 )
{
super (x1, y1, x2, y2);
}
public MyLine ( int x1, int y1, int x2, int y2, Color c)
{
super ( x1, y1, x2, y2 );
}
public void draw ( Graphics g)
{
Color c = g.getColor();
g.setColor(this.getColor());
g.drawLine(getX1 (), getY1(), getX2(), getY2());
g.setColor(c);
}
public String toString()
{
return " Line";
}
}
import java.awt.Graphics;
public class MyOval extends MyBounded {
public MyOval ()
{
super ();
}
public MyOval ( int x1, int y1, int x2, int y2, boolean f)
{
super ( x1, y1, x2, y2, f);
}
public MyOval (int x1, int y1, int x2, int y2, Color c, boolean f)
{
super ( x1, y1, x2, y2, c, f);
}
public void draw ( Graphics g )
{
Color c = g.getColor() );
g.setColor( this.getColor());
if ( getFilled ())
g.fillOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
else
g.drawOval(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight());
g.setColor(c);
}
public String toString ()
{
return "Oval" ;
}
}
public class MyRectangle extends MyBounded {
public MyRectangle ()
{
super ();
}
public MyRectangle ( int x1, int y1, int x2, int y2, boolean f )
{
super ( x1, y1, x2, y2, f);
}
public MyRectangle ( int x1, int y1, int x2, int y2, Color c, boolean f )
{
super ( x1, y1, x2, y2, c, f);
}
Color c = g.getColor();
g.setColor( this.getColor());
if ( getFilled())
g.fillRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() );
else
g.drawRect(getUpperLeftX(), getUpperLeftY(), getWidth(), getHeight() );
g.setColor( c);
}
public String toString()
{
return " Rectangle" ;
}
}
import java.awt.color.*;
abstract class class MyShape implements Serializable {
private int x1, y1, x2, y2;
private Color color;
public MyShape ()
{
this (0, 0, 0, 0);
}
public MyShape ( int x1, int y1, int x2, int y2)
{s
this (x1, y1, x2, y2, Color.black);
}
public MyShape ( int x1, int y1, int x2, int y2, Color c)
{
setAll(x1, y1, x2, y2, c );
}
public final void setAll( int x1, int y1, int x2, int y2, Color c )
{
setX1 (x1);
setY1 (y1);
setX2 (x2);
setY2 (y2);
}
public final void setX1 (int x1 )
{
x1 = x1;
}
public final int getX1()
{
return x1;
}
public final void setY1 (int y1 )
{
y1 = y1;
}
public final int getY1()
{
return y1;
}
}
public final void setX2 (int x2 )
{
x2 = x2;
}
public final int getX2()
{
return x2;
}
public final void setY2 (int y2 )
{
y2 = y2;
}
public final int getY2()
{
return y2;
}
public void setColor (Color c)
{
color= c;
}
public Color getColor()
{
return color;
}
public abstract void draw (Graphics g);
}
public class ShapePanel extends JPanel {
private static final String [] shapeList =
{ " Rectangel", " Line", " Oval"};
private JComboBox choose;
private JCheckBox fill;
public ShapePanel()
{
choose = new JCombobox ( shapeList);
fill = new JCheckBox(" Filled", flase);
add ( choose);
add( fill);
}
public JCheckBox getFill()
{
return fill;
public JComboBox getChooser()
{
return choose;
}
}
Please do Not tell it is compling in your Eclipse because i have really bad experince with Chegg this code is really long and it is for m y final project and by the way do i have to put all the classes in one claas or sperate as i did for example; drawing, drawframe, bounded, shape and so on they are 10 classes.
theanks a lot for your understanding.
As i mentioned early this is not my code so i just type it in my Eclipse but is Not running and it Does Not have what you looking for JSLINDER code.Java.
it has only 10 classes as i mentioned {{ mycolor, myshape,mybounded, myrectangle, myline, myoval, drawing, drawframe,and other two classes}}
if you can fix it and if you think you need the JSlinder.java mybe you can write it. please
thanks and much apperciate it.
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