Sending a shape to a window

I'm having trouble getting the window to draw out shape1

Page 1 of 1

1 Replies - 1283 Views - Last Post: 23 February 2009 - 01:36 PM Rate Topic: -----

#1 grifter8875  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 15
  • Joined: 09-February 09

Sending a shape to a window

Post icon  Posted 23 February 2009 - 12:56 PM

This is my first project in my Java class. I'm having trouble getting my test class to draw shape1 in the window, this is what i have in my test class right now;
public class SimpleShapeTest{
  public static void main (String [] args) {
  //Initialize and constuct a SimpleShape object named shape1
  SimpleShape shape1 = new SimpleShape("circle",0,100,200);
  //Create a variable named size1 that contains a int value
  int size1 = shape1.getSize();
  //Print the value of size1.  Be sure to use a descriptive label
  System.out.println("Size "+size1);
  //Print the shape, horizontal position, and vertical position of shape1 using the appropriate accessor methods. 
  System.out.println("Shape, "+ shape1.getShape()+"\n"+"x-pos, "+shape1.getHorizontalPosition()+"\n"+"y-pos, "+shape1.getVerticalPosition());
  //Initialize and construct a SimpleShape object named shape2 
  SimpleShape shape2 = new SimpleShape("circle", 300, 300, 100);
  //Print shape2 using the toString method.
  System.out.println("Shape 2 "+shape2.toString());
  //Initialize and construct a SimpleWindow object named window with width 400 and height 300. 
   SimpleWindow window = new SimpleWindow(400, 300);
  //Draw shape1 in window in blue using the draw method.  Pause 1 second. 
   window.drawOval(shape1, "blue");

									   
  
  }//end main
}//end simpleshapetest

This is what i have in my shape class
public class SimpleShape{
 //member varible
  private String myShape;
  private int myHorizontalPosition;
  private int myVerticalPosition;
  private int mySize;
  
  //constuctors
  public SimpleShape(String shape, int horiz, int vert, int size){
   myShape = shape;
   myHorizontalPosition = horiz;
   myVerticalPosition = vert;
   mySize = size;
  }//end of SimpleShape
  
  //methods
  //getShape method returns the shape
  public String getShape(){
   return myShape; 
  }//end of getShape
 // getHorizontalPosition returns the x position 
  public int getHorizontalPosition(){
   return myHorizontalPosition; 
  }//end of getHorizontalPosition
  //getVerticalPosition returns the y position
  public int getVerticalPosition(){
   return myVerticalPosition; 
  }//end of getVerticalPosition
  //getSize returns the size
  public int getSize(){
   return mySize; 
  }//end of getSize
  //setSize sets the size 
  public void setSize(int size) {
	mySize = size;
  }
  // draw draws the shape on the window in the color
  public void draw(SimpleWindow window, String color){
	
	window.drawOval(myHorizontalPosition, myVerticalPosition, mySize, mySize);
	window.setColor(color);
  }
  // fill fills the shape on the window in the color
  public void fill(SimpleWindow window, String color){
	window.fillOval(myHorizontalPosition, myVerticalPosition, mySize, mySize);
	window.setColor(color);
  }
  // toString returns a string describing the shape.All the instance fields should be in the description. 
  public String toString(){
	return "Shape: "+getShape()+
		   " x pos: "+getHorizontalPosition()+
		   " y pos: "+getVerticalPosition()+
		   " Size: "+getSize(); 
  }//end toString()

   }//end SimpleShape
   

The Prof allready gave me the SimpleWindow class and said that shouldn't be modified,
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;

/**
 * SimpleWindow is a class that will create a window
 * in which you can draw and fill rectangles and ovals.
 * 
 * @author Tom Bylander
 * @version 20090110
 */
public class SimpleWindow extends JComponent {
  
  /** 
   * Here is a brief explanation of the implementation.
   * The JFrame object is the window and this JComponent
   * object is added to it. 
   * The Graphic2D object comes from the BufferedImage object,
   * which is painted on this JComponent object.
   * 
   * Trying to tie all these objects together is based on examples
   * from various books and web sites in addition to guesswork and
   * trial and error.  Oh, yes, the official documentation is
   * helpful sometimes.
   */ 
  
  private JFrame frame;		 // the whole window
  private BufferedImage image;  // needed for drawing stuff
  private Graphics2D graphics;  // also needed for drawing stuff
  
  /**
   * Constructs and displays a SimpleWindow object.
   * 
   * @param width   the width of the drawing space in pixels
   * @param height  the height of the drawing space in pixels
   */
  public SimpleWindow(int width, int height) {
	super();
	setPreferredSize(new Dimension(width,height));
	image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
	graphics = image.createGraphics();
	graphics.setColor(Color.WHITE);
	graphics.fillRect(0,0,width,height);
	graphics.setColor(Color.BLACK);
	
	// create JFrame and add this to it
	frame = new JFrame();
	frame.setTitle("Simple Window");
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	frame.add(this);
   
	// finish   
	frame.pack();
	frame.setVisible(true);
  }
  
  /**
   * Closes the window. 
   */
  public void close() {
	frame.dispose();
  }
  
  /**
   * Pause for a given number of milliseconds.
   * Use the pause method if you want to schedule
   * a sequence of images.
   * 
   * @param t  the number of milliseconds to pause
   */ 
  public void pause(int t) {
	try {
	  Thread.currentThread().sleep(t);
	} catch (Exception e) {
	  // ignore exceptions
	}
  }
  
  /**
   * Sets the current color to the specified color.
   * All following graphics operations use this color.
   * The color must be one of: black, blue, cyan,
   * darkgray, gray, green, lightgray, magenta, orange,
   * pink, red, white, or yellow.  If the color is not
   * recognized, no change is made, and no errors or
   * warnings are given.
   * 
   * @param c  the new color.
   */
  public void setColor(String c) {
	if (c.equalsIgnoreCase("black"))
	  graphics.setColor(Color.BLACK);
	else if (c.equalsIgnoreCase("blue"))
	  graphics.setColor(Color.BLUE);
	else if (c.equalsIgnoreCase("cyan"))
	  graphics.setColor(Color.CYAN);
	else if (c.equalsIgnoreCase("darkgray"))
	  graphics.setColor(Color.DARK_GRAY);
	else if (c.equalsIgnoreCase("gray"))
	  graphics.setColor(Color.GRAY);
	else if (c.equalsIgnoreCase("green"))
	  graphics.setColor(Color.GREEN);
	else if (c.equalsIgnoreCase("lightgray"))
	  graphics.setColor(Color.LIGHT_GRAY);
	else if (c.equalsIgnoreCase("magenta"))
	  graphics.setColor(Color.MAGENTA);
	else if (c.equalsIgnoreCase("orange"))
	  graphics.setColor(Color.ORANGE);
	else if (c.equalsIgnoreCase("pink"))
	  graphics.setColor(Color.PINK);
	else if (c.equalsIgnoreCase("red"))
	  graphics.setColor(Color.RED);
	else if (c.equalsIgnoreCase("white"))
	  graphics.setColor(Color.WHITE);
	else if (c.equalsIgnoreCase("yellow"))
	  graphics.setColor(Color.YELLOW);
  }
  
  /* Here are some methods which are forwarded
   * to the Graphics2D object followed by a
   * call to the display method to insure that
   * the change becomes visible.
   */ 
  
  /** Draws a line between the points
   * (x1, y1) and (x2, y2).
   * 
   * @param x1  the first point's x coordinate.
   * @param y1  the first point's y coordinate.
   * @param x2  the second point's x coordinate.
   * @param y2  the second point's y coordinate.
   */
  public void drawLine(int x1, int y1, int x2, int y2) {
	graphics.drawLine(x1, y1, x2, y2);
	this.display();
  }
  
  /**
   * Draws the outline of an oval. The result is a circle or
   * ellipse that fits within the rectangle specified by the
   * x, y, width, and height arguments.
   * <p>
   * The oval covers an area that is width + 1 pixels wide
   * and height + 1 pixels tall.
   * 
   * @param x	   the x coordinate of the upper left corner.
   * @param y	   the y coordinate of the upper left corner.
   * @param width   the width of the oval.
   * @param height  the height of the oval.
   */ 
  public void drawOval(int x, int y, int width, int height) {
	graphics.drawOval(x, y, width, height);
	this.display();
  }
  
  /**
   * Fills an oval.
   * 
   * @param x	   the x coordinate of the upper left corner.
   * @param y	   the y coordinate of the upper left corner.
   * @param width   the width of the oval.
   * @param height  the height of the oval.
   */ 
  public void fillOval(int x, int y, int width, int height) {
	graphics.fillOval(x, y, width, height);
	this.display();
  }
  
  /**
   * Draws the outline of a rectangle. 
   * The left and right edges are at x and x + width. 
   * The top and bottom edges are at y and y + height.
   *
   * @param x	   the x coordinate of the rectangle to be drawn.
   * @param y	   the y coordinate of the rectangle to be drawn.
   * @param width   the width of the rectangle to be drawn.
   * @param height  the height of the rectangle to be drawn.
   */ 
  
  public void drawRect(int x, int y, int width, int height) {
	graphics.drawRect(x, y, width, height);
	this.display();
  }
  
  /**
   * Fills a rectangle. 
   * The left and right edges are at x and x + width - 1. 
   * The top and bottom edges are at y and y + height - 1.
   *
   * @param x	   the x coordinate of the rectangle to be drawn.
   * @param y	   the y coordinate of the rectangle to be drawn.
   * @param width   the width of the rectangle to be drawn.
   * @param height  the height of the rectangle to be drawn.
   */ 
  
  public void fillRect(int x, int y, int width, int height) {
	graphics.fillRect(x, y, width, height);
	this.display();
  }
  
  /**
   * Returns a Graphics2D object so that you can
   * draw on it using any Graphics2D method, rather
   * than just the few delegated by Simplewindow.
   * You might need to use the display method to 
   * ensure that what you draw is displayed.
   * 
   * @return a Graphics2D object to draw on
   */ 
  public Graphics2D getGraphics() {
	return graphics;
  }
  
  /**
   * Displays the Graphics2D object that was returned
   * from the getGraphics method.
   */ 
  public void display() {
	frame.repaint();
  }
  
  /**
   * <b>Do not use this method</b>; this method
   * is needed to so that the display method will work.
   * <p>
   * Do not call this method.  Use the display method.
   * Bad things will happen if you call this method,
   * possibly including the end of the world as we
   * know it.
   * 
   * @param g  a Graphics object that is part of this JComponent
   * object
   */ 
  public void paintComponent(Graphics g) {
	// Paint the image instance field, 
	// which was used to generate the 
	// graphics instance field, on the window.
	g.drawImage(image, 0, 0, null);
  }
  
  /**
   * main method to test the class
   */
  public static void main(String[] args) {
	SimpleWindow window = new SimpleWindow(400,300);
	window.drawOval(10,10,100,100);
	window.pause(500);
	window.setColor("orange");
	window.drawLine(110,60,290,60);
	window.pause(500);
	window.setColor("red");
	window.fillOval(290,10,100,100);
	window.pause(500);
	window.setColor("cyan");
	window.drawLine(340,110,340,190);
	window.pause(500);
	window.setColor("green");
	window.fillRect(290,190,100,100);
	window.pause(500);
	window.setColor("gray");
	window.drawLine(290,240,110,240);
	window.pause(500);
	window.setColor("blue");
	window.drawRect(10,190,100,100);
	window.pause(500);
	window.setColor("pink");
	window.drawLine(60,190,60,110);
	window.pause(500);
	window.setColor("white");
	window.drawOval(10,10,100,100);
	window.pause(500);
	window.drawLine(110,60,290,60);
	window.pause(500);
	window.fillOval(290,10,100,100);
	window.pause(500);
	window.drawLine(340,110,340,190);
	window.pause(500);
	window.fillRect(290,190,100,100);
	window.pause(500);
	window.drawLine(290,240,110,240);
	window.pause(500);
	window.drawRect(10,190,100,100);
	window.pause(500);
	window.drawLine(60,190,60,110);
	window.pause(500);
	window.close();
  }
}

any time i try to compile it gives the message Error: F:\College\CS\Project 1\SimpleShapeTest.java:23: drawOval(int,int,int,int) in SimpleWindow cannot be applied to (SimpleShape,java.lang.String)

Any help pointing me in the right direction would be really appreciated

Is This A Good Question/Topic? 0
  • +

Replies To: Sending a shape to a window

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Sending a shape to a window

Posted 23 February 2009 - 01:36 PM

The error message is showing you that drawOval() takes 4 arguments. You are calling it with two. The error message tells you the 4 arguments are integers. You are calling it with a string. You should look at the definition of the drawOval() method in the SimpleWindow class. :)

This post has been edited by n8wxs: 23 February 2009 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1