Question: Need help co,pleting this java Code. Thanks, I will provide code just need some added. Its not as much work as it looks if you

Need help co,pleting this java Code. Thanks, I will provide code just need some added. Its not as much work as it looks if you could copy and paste the code I provide into eclipse or something like that. Jusy need some code changed in DrawingPanel.Java and one more class written at the end. Thanks

You will want to take your solution from JH3, or the published answer for JH3.

The Drawing.java part can stay the same. Here it is

import java.awt.Color; import java.awt.Graphics; import java.awt.Point; import java.awt.Polygon; import java.util.ArrayList;

enum DrawType {scribble, oval, rectangle, polygon, line};

class DrawingProperties { DrawType drawType; boolean filled; Color color; DrawingProperties(DrawType drawType, Color color, boolean filled) { this.drawType = drawType; this.color = color; this.filled = filled; } } public class Drawing { DrawingProperties drawingProperties = new DrawingProperties(DrawType.rectangle, Color.blue, false); ArrayList shapeArr = new ArrayList(); Shape inProgress = null;

public String toString() { return drawingProperties.drawType + " color=" + drawingProperties.color +" filled="+ drawingProperties.filled; }

public void draw(Graphics g) { for (int i=0; i

public void setColor(Color color) { drawingProperties.color = color; }

public void setFilled(boolean filled) { drawingProperties.filled = filled; } public void setDrawType(DrawType drawType) { if (drawingProperties.drawType == DrawType.polygon) exitPolygonMode(); drawingProperties.drawType = drawType; if (drawType == DrawType.polygon) enterPolygonMode(); }

private void enterPolygonMode() { drawingProperties.drawType = DrawType.polygon; inProgress = new PolygonShape(drawingProperties.color, drawingProperties.filled);

} private void exitPolygonMode() { if (inProgress != null) shapeArr.add(inProgress); inProgress = null; }

public void mousePressed(Point p) { if (drawingProperties.drawType == DrawType.polygon) { if (inProgress != null) { inProgress.subsequentPoint(p); } } else { switch(drawingProperties.drawType) { case rectangle: inProgress = new Rectangle(drawingProperties.color, drawingProperties.filled); break; case oval: inProgress = new Oval(drawingProperties.color, drawingProperties.filled); break; case line: inProgress = new Line(drawingProperties.color); break; case scribble: inProgress = new Scribble(drawingProperties.color); break; } inProgress.firstPoint(p); }

} public void mouseDragged(Point p) { switch(drawingProperties.drawType) { case rectangle: case oval: case scribble: case line: inProgress.subsequentPoint(p); break; } } public void mouseReleased(Point p) { if (drawingProperties.drawType != DrawType.polygon) { inProgress.subsequentPoint(p); shapeArr.add(inProgress); inProgress = null; } }

}

abstract class Shape { Color color; Shape ( Color c) { color =c; } abstract void firstPoint(Point p); abstract void draw(Graphics g); abstract void subsequentPoint(Point p); }

class Rectangle extends Shape { boolean filled=false; Point start; Point lastPoint; Rectangle(Color c, boolean filled) { super(c); lastPoint = start; this.filled = filled; } void firstPoint(Point p) { start = p; lastPoint =p; } void subsequentPoint(Point p) { lastPoint = p; } void draw(Graphics g) { g.setColor(color); int x = Math.min(start.x, lastPoint.x); int y = Math.min(start.y, lastPoint.y); int w = Math.abs(start.x - lastPoint.x); int h = Math.abs(start.y - lastPoint.y); if (filled) g.fillRect(x, y, w, h); else g.drawRect(x, y, w, h); } }

class Line extends Shape { Point start; Point lastPoint; Line(Color c) { super(c); } void firstPoint(Point p) { start = p; lastPoint =p; } void subsequentPoint(Point p) { lastPoint = p; } void draw(Graphics g) { g.setColor(color); g.drawLine(start.x, start.y, lastPoint.x, lastPoint.y); } }

class Oval extends Shape { boolean filled=false; Point start; Point lastPoint; Oval(Color c, boolean filled) { super(c); this.filled = filled; } void firstPoint(Point p) { start = p; lastPoint =p; } void subsequentPoint(Point p) { lastPoint = p; } void draw(Graphics g) { g.setColor(color); int x = Math.min(start.x, lastPoint.x); int y = Math.min(start.y, lastPoint.y); int w = Math.abs(start.x - lastPoint.x); int h = Math.abs(start.y - lastPoint.y); if (filled) g.fillOval(x, y, w, h); else g.drawOval(x, y, w, h); } }

class Scribble extends Shape { ArrayList points= new ArrayList(); Scribble(Color c) { super(c); } void firstPoint(Point p) { points.add(p); } void subsequentPoint(Point p) { points.add(p); } void draw(Graphics g) { g.setColor(color); for (int i=1; i

class PolygonShape extends Shape { ArrayList points= new ArrayList(); boolean filled; PolygonShape(Color c, boolean filled) { super(c); this.filled = filled; } void firstPoint(Point p) { // currently not called points.add(p); } void draw(Graphics g) { int[] y = new int[points.size()]; int[] x = new int[points.size()]; for (int i=0; i

The old DrawingProg.java 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. Here this part is, I have changed it to JPanel but still to eliminate stuff so it works successfully.

import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; 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; import javax.swing.JPanel;

public class DrawingPanel extends JPanel{ Drawing drawing = new Drawing();

Image offScreenImage = null; Dimension screenDimension = null;

// INNER Class 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(); } }

// End of INNER Class

DrawingPanel() { super("My Drawing Program"); setSize(800, 400); setDefaultCloseOperation(EXIT_ON_CLOSE);

MyMouseHandler mmh = new MyMouseHandler(); addMouseListener(mmh); addMouseMotionListener(mmh);

setVisible(true); } public void paint(Graphics screen) { Dimension dimen = getSize(); if (offScreenImage==null || !dimen.equals(screenDimension)) { screenDimension = dimen; offScreenImage = createImage(dimen.width, dimen.height); } Graphics g = offScreenImage.getGraphics(); Insets insets = getInsets(); int top = insets.top; int left = insets.left; g.setColor(Color.white); g.fillRect(0, 0, dimen.width, dimen.height);

drawing.draw(g);

// Draw a yellow banner on the top that prints out what we are currently doing Font font = g.getFont(); FontMetrics fm = getFontMetrics(font); int fontHeight = fm.getHeight(); g.setColor(Color.yellow); g.fillRect(0, top, dimen.width, fontHeight); String sdisplay = drawing.toString(); g.setColor(Color.black); g.drawString(sdisplay, left , top+fm.getMaxAscent());

screen.drawImage(offScreenImage, 0,0, this); }

public static void main(String[] args) { DrawingPanel ob = new DrawingPanel(); Scanner keyboard = new Scanner(System.in);

boolean continueFlag=true; while(continueFlag) { System.out.println("Cmds: r,o,l,s,p,a,q,?,f,d,b,m,g"); String str = keyboard.next().toLowerCase(); if (str.length() == 0) continue;

switch(str.charAt(0)) { case 'r': ob.drawing.setDrawType(DrawType.rectangle); break; case 'o': ob.drawing.setDrawType(DrawType.oval); break; case 'l': ob.drawing.setDrawType(DrawType.line); break; case 's': ob.drawing.setDrawType(DrawType.scribble); break; case 'p': case 'a': ob.drawing.setDrawType(DrawType.polygon); break; case 'q': continueFlag = false; break; case 'f': ob.drawing.setFilled(true); break; case 'd': ob.drawing.setFilled(false); break; case 'b': ob.drawing.setColor(Color.blue); break; case 'm': ob.drawing.setColor(Color.magenta); break; case 'g': ob.drawing.setColor(Color.green); 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"); break; } } System.out.println("Exitting the Drawing Program"); ob.dispose(); keyboard.close();

}

}

.

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.

Need the DrawingProg2.java class! Thanks!

JH4: Border Layout Drawing Program North FlowLayout with Components West Center GridLayout (0,1) Drawing Panel java Components

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!