Need help with this Java code, it uses code from a previous assignment which I h
ID: 3855318 • Letter: N
Question
Need help with this Java code, it uses code from a previous assignment which I have provided. Also, I provided a template at the bottom that I need help where it states. Thank You.
You will want to take your solution from JH3, or the published answer for JH3.
HERE IS THE JH3 Answer:
The Drawing.java part can stay the same. (not shown)
The old DrawingProg.java (shown above) )will be renamed to DrawingPanel.java. In this code you will change the class to extend JPanel instead of JFrame. In this process, you will discover a few things that need to be changed. In most cases it will be things to eliminate.
You will now create a new DrawingProg2.java class that uses your DrawingPanel class in the Center of a BorderLayout. The picture below shows the overall structure.
Instead of having a main which reads in commands from a Scanner class, you will add Components that will live in the North and West region of your overall BorderLayout.
I will show you a template of the new approach for DrawingProg2.java (end of these notes) which will outline the steps described in this set of notes.
Once you have added your components to the North and West regions of the BorderLayout, your screen can look like the following:
Note that when you pick "line" or "scribble", the "filled" checkbox is meaningless. It is nice to make this CheckBox invisible when you are in "line" or "scribble" mode.
So in scribble mode or line mode, your screen would look like the following:
North Panel
The North Region is filled with a JPanel which uses a FlowLayout.
We have one CheckBox class for the "Filled" part.
We have a Radio Button Group containing the RadioButtons for rectangle, oval, line, and scribble
Using the setVisible method, we make the "Filled" CheckBox visible when rectangle, or oval are selected. The CheckBox is invisible when line or scribble are selected.
Note that RadioButtons, and CheckBoxes respond to the ActionListener interface the same way that JButton does.
West Panel
The West Region is filled with a JPanel using a GridLayout.
GridLayout can be constructed with either:
new GridLayout(3,1) or
new GridLayout(0,1)
If you use GridLayout and set the number of rows to zero (note you need to specify the number of columns for this to work), then GridLayout adds rows on an as needed basis. So adding 3 components generates 3 rows in our case.
DrawingProg2.java
template for DrawingProg2:
Explanation / Answer
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Scanner;
import javax.swing.JFrame;
public class DrawingProgram extends JFrame implements DrawingToolbarListener
{
private final Dimension initDimen = new Dimension(800, 400);
private final int STATUSBAR_HEIGHT = 20;
Drawing drawing = new Drawing();
DrawingToolbar drawingToolbar = new DrawingToolbar();
Image offScreenImage = null;
Dimension screenDimension = null;
class MyMouseHandler extends MouseAdapter
{
public void mousePressed(MouseEvent e)
{
drawing.mousePressed(e.getPoint());
repaint();
}
public void mouseReleased(MouseEvent e)
{
drawing.mouseReleased(e.getPoint());
repaint();
}
public void mouseDragged(MouseEvent e)
{
drawing.mouseDragged(e.getPoint());
repaint();
}
public void mouseClicked(MouseEvent e)
{
drawing.mouseClicked(e.getPoint());
repaint();
}
}
DrawingProgram()
{
super("My Drawing Program");
setSize(initDimen);
setDefaultCloseOperation(EXIT_ON_CLOSE);
MyMouseHandler mmh = new MyMouseHandler();
addMouseListener(mmh);
addMouseMotionListener(mmh);
drawingToolbar.addDrawingToolbarEventListener(this);
setVisible(true);
}
public void paint(Graphics screen)
{
Dimension dimen = getSize();
Insets insets = getInsets();
// If the buffer image doesn't exist or the screen size changed
if (offScreenImage == null || !dimen.equals(screenDimension))
{
// Create a new image of the needed size
screenDimension = dimen;
offScreenImage = createImage(dimen.width, dimen.height);
}
// Get a graphics object representing the buffer image
Graphics g = offScreenImage.getGraphics();
// Get the status bar text
String str = drawing.toString();
// Calculate the status bar text y position so that it's vertically centered on the status bar
int textPos = (STATUSBAR_HEIGHT - g.getFontMetrics().getHeight()) / 2;
// Fill background
g.setColor(Color.white);
g.fillRect(0, 0, dimen.width, dimen.height);
// Draw shapes
drawing.draw(g);
// Draw rectangle for status bar
g.setColor(Color.YELLOW);
g.fillRect(0, dimen.height - insets.bottom - STATUSBAR_HEIGHT, dimen.width, STATUSBAR_HEIGHT);
// Draw text for status bar
g.setColor(Color.BLACK);
g.drawString(str, insets.left, dimen.height - STATUSBAR_HEIGHT + textPos);
// Draw the buffer image to the screen
screen.drawImage(offScreenImage, 0, 0, this);
}
// Capture events from the drawing toolbar and send the command to the handler
@Override
public void handleDrawingToolbarEvent(DrawingToolBarEvent e)
{
char c = e.command.charAt(0);
commandHandler(c);
System.out.println(c);
System.out.println("Commands: ?, r, o, l, s, p, a, q, f, d, b, m, g, t");
}
// Process commands
public boolean commandHandler(char cmd)
{
switch(cmd)
{
case 'r':
this.drawing.setDrawType(DrawType.rectangle);
break;
case 'o':
this.drawing.setDrawType(DrawType.oval);
break;
case 'l':
this.drawing.setDrawType(DrawType.line);
break;
case 's':
this.drawing.setDrawType(DrawType.scribble);
break;
case 'p':
case 'a':
this.drawing.setDrawType(DrawType.polygon);
break;
case 'q':
return false;
case 'f':
this.drawing.setFilled(true);
break;
case 'd':
this.drawing.setFilled(false);
break;
case 'b':
this.drawing.setColor(Color.blue);
break;
case 'm':
this.drawing.setColor(Color.magenta);
break;
case 'g':
this.drawing.setColor(Color.green);
break;
case 't':
this.drawingToolbar.toggleToolbarWindow();
break;
default: // '?' comes here
System.out.println("r - drawType= Rectangle");
System.out.println("o - drawType= Oval");
System.out.println("l - drawType= Line");
System.out.println("s - drawType= Scribble");
System.out.println("p - drawType= Polygon");
System.out.println("a - another Polygon");
System.out.println("q - quit");
System.out.println("f - filled objects");
System.out.println("d - draw objects (not filled)");
System.out.println("b - Use Blue Color");
System.out.println("m - Use magenta Color");
System.out.println("g - Use Green Color");
System.out.println("t - Toggle visibility of toolbar window");
break;
}
this.repaint();
return true;
}
// Process keyboard input while command handler returns true
public void inputProcessor()
{
Scanner keyboard = new Scanner(System.in);
boolean continueFlag = true;
while(continueFlag)
{
System.out.println("Commands: ?, r, o, l, s, p, a, q, f, d, b, m, g, t");
String str = keyboard.next().toLowerCase();
if (str.length() < 1)
continue;
continueFlag = commandHandler(str.charAt(0));
drawingToolbar.repaint();
}
keyboard.close();
}
public static void main(String[] args)
{
DrawingProgram dp = new DrawingProgram();
dp.inputProcessor();
System.out.println("Exiting the Drawing Program");
dp.dispose();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.