Trying to draw 2 objects on screen and store the selected item names in an array
Posted
by
thefonso
on Stack Overflow
See other posts from Stack Overflow
or by thefonso
Published on 2011-02-26T23:22:35Z
Indexed on
2011/02/26
23:24 UTC
Read the original article
Hit count: 326
Ok...this is a homework question, here is what i'm asked to do....
"Allow the user to draw two Shapes, which when instantiated, get put into the array myShapes...(store the shapes in the createShape() method."
I want to know if I'm going in the right direction. Do I need to modify only Model.java or GUIDemo.java as well? Am I sufficient in thinking of only storing the values for the array via a loop inside my createShape() method? How do I go a bout checking to see if things work so far. There are many steps for this homework project after this one but i'm stuck here. Please point me in the right direction.
The array myShapes lives inside my model class inside Model.java:
package model;
import java.awt.Color; import java.awt.Container;
import shapes.Line; import shapes.Oval; import shapes.Rectangle; import shapes.Shape; import shapes.Triangle;
import interfaces.Resettable;
public class Model implements Resettable { private Container container; private String message;
public final static String DRAW = "Draw";
public final static String MOVE = "Move";
public final static String REMOVE = "Remove";
public final static String RESIZE = "Resize";
public final static String FILL = "Fill";
public final static String CHANGE = "Change";
public final static String RECTANGLE = "Rectangle";
public final static String OVAL = "Oval";
public final static String LINE = "Line";
public final static String TRIANGLE = "Triangle";
private String action = DRAW;
private boolean fill = false;
public static String[] selections = {"Rectangle", "Oval", "Line", "Triangle"};
//project 9 begin
public Shape[] myShapes = new Shape[2];
//project 9 stop
private String currentShapeType;
private Shape currentShape;
public Color lineColor;
private Color fillColor = Color.gray;
public Shape createShape() {
if(currentShapeType == RECTANGLE){
currentShape = new Rectangle(0, 0, 0, 0, lineColor, fillColor, fill);
}
if(currentShapeType == OVAL) {
currentShape = new Oval(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == LINE) {
currentShape = new Line(0,0,0,0, lineColor, fillColor, fill);
}
if(currentShapeType == TRIANGLE) {
currentShape = new Triangle(0,0,0,0, lineColor, fillColor, fill);
}
//project 9 start
if(myShapes[0] == null) {
myShapes[0]=currentShape;
}
else {
myShapes[1]=currentShape;
}
//project 9 stop
return currentShape;
}
public Shape getCurrentShape() {
return currentShape;
}
public String getCurrentShapeType(){
return currentShapeType;
}
public void setCurrentShapeType(String shapeType){
currentShapeType = shapeType;
}
public Model(Container container) {
this.container = container;
}
public void repaint() {
container.repaint();
}
public void resetComponents() {
action = DRAW;
currentShape = null;
if (container instanceof Resettable) {
((Resettable) container).resetComponents();
}
}
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public boolean isFill() {
return fill;
}
public void setFill(boolean fill) {
this.fill = fill;
}
public void setMessage(String msg) {
this.message = msg;
}
public String getMessage() {
return this.message;
}
public Color getLineColor() {
return this.lineColor;
}
public void setLineColor(Color c) {
this.lineColor = c;
}
public String toString() {
return "Model:\n\tAction: " + action + "\n\tFill: " + fill;
}
}
The application is run from GUIDemo.java:
package ui.applet;
import interfaces.Resettable; import java.applet.Applet; import java.awt.Graphics; import event.ShapeMouseHandler; import shapes.Shape; //import ui.panels.ButtonPanel; import ui.panels.ChoicePanel; import ui.panels.MainPanel; import model.Model;
@SuppressWarnings("serial") public class GUIDemo extends Applet implements Resettable { MainPanel mainPanel; Model model; ChoicePanel choicePanel;
public void init() {
resize(600,400);
model = new Model(this);
choicePanel = new ChoicePanel(model);
mainPanel = new MainPanel(model);
this.add(choicePanel);//this is the drop down list
this.add(mainPanel);//these are the radio buttons and reset button
ShapeMouseHandler mouseHandler = new ShapeMouseHandler(model);
addMouseListener(mouseHandler);
addMouseMotionListener(mouseHandler);
}
public void paint(Graphics g) {
Shape shape;
shape = model.getCurrentShape();
if(shape != null) {
shape.draw(g);
}
System.out.println(model);
System.out.println(shape);
}
public void resetComponents() {
mainPanel.resetComponents();
choicePanel.resetComponents();
}
}
© Stack Overflow or respective owner