class error

  • (2 Pages)
  • +
  • 1
  • 2

27 Replies - 2238 Views - Last Post: 07 October 2010 - 08:41 PM Rate Topic: -----

#1 Guest_Josh*


Reputation:

class error

Posted 03 October 2010 - 07:24 PM

I am working on a class to draw a star, but when i try to compile i get the following errors:

What seems to be wrong with my code?

Star.java:70: cannot find symbol
symbol  : method drawPoloygon(int,int,int)
location: class Star
			drawPoloygon(x, y, width);
			^
Star.java:72: cannot find symbol
symbol  : method fillPolygon(int,int,int)
location: class Star
			fillPolygon(x, y, width);	
			^



Here is my code:

import javax.swing.*;
import java.awt.*;

public class Star
{
	// instance variables 
	private int x;
	private int y;
	private int width;
	private boolean filled; 
	
	// Default constructor 
	public Star()
	{
		width = 50;
		filled = false;
	
	} // end constructor
	
	// getter methods
	public double getX()
	{
		return x;
	}
	
	public double getY()
	{
		return y;
	}
	
	public double getWidth()
	{
		return width;
	}
	
	public boolean getFilled()
	{
		return filled;
	}

	// setter methods
	public void setX( int newX )
	{
	   x = newX;
	}
		
	public void setY( int newY )
	{	
		y = newY;
	}
		
	public void setWidth( int newWidth )
	{
		width = newWidth;
	}
		
	public void setFilled( boolean newFilled )
	{
		filled = newFilled;
	}
				
	public void draw( Graphics g )
	{
		if ( filled )
			g.drawPoloygon(x, y, width);
		else
			g.fillPolygon(x, y, width);	
	}	
	
} // endl class


Is This A Good Question/Topic? 0

Replies To: class error

#2 KYA   User is offline

  • Wubba lubba dub dub!
  • member icon

Reputation: 3213
  • View blog
  • Posts: 19,241
  • Joined: 14-September 07

Re: class error

Posted 03 October 2010 - 07:28 PM

Parameters for drawPolygon() do not match.

Choice 1
Choice 2
Was This Post Helpful? 1
  • +
  • -

#3 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: class error

Posted 03 October 2010 - 07:29 PM

drawPolygon
drawPolygon()

Look at the arguments the method takes. You dont meet the required arguments.
Was This Post Helpful? 1
  • +
  • -

#4 Guest_Josh*


Reputation:

Re: class error

Posted 05 October 2010 - 05:45 PM

Okay so if I understand what those links are basically saying, i need to make my instance variables arrays? Is there a way to draw my star without changing them to arrays if thats the case? I just want the user to be able to specify the x, y coordinates and the width and have the program draw the star based on their inputs
Was This Post Helpful? 0

#5 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: class error

Posted 05 October 2010 - 06:09 PM

Pass it an array of x coordinates, an array of y coordinates, and an int containing the number of total points.
Was This Post Helpful? 0
  • +
  • -

#6 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: class error

Posted 06 October 2010 - 04:58 PM

Hey i'm Josh (the one who started the topic), for some reason it wouldn't let me post as a guest, so i just created an account. Can someone show me some actual code, because i can't seem to figure this out

This post has been edited by <3DIC: 06 October 2010 - 05:35 PM

Was This Post Helpful? 0
  • +
  • -

#7 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: class error

Posted 06 October 2010 - 07:42 PM

Well, ok. Think about it like this...How can you draw a POLYgon, with 2 points? The fact is, you can't and when you originally called the method, you passed it a single x value and a y value. That is a point, not a polygon. You have to use an array like so:

int[] x = {25,35,45,55,65};
int[] y = {1, 25, 25, 25, 1};



Try passing those to the method to see. Experimentation is key to learning.
Was This Post Helpful? 1
  • +
  • -

#8 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: class error

Posted 07 October 2010 - 02:34 PM

Okay i came up with this:
when i compile i get no errors but is this actually doing what it needs to be?

import javax.swing.*;
import java.awt.*;

public class Star
{
	// instance variables
	private int x;
	private int y;
	private int width;
	private boolean filled;

	int[] coordinateX = {x,           // 1
								x + width/4, // 2
								x + width/3, // 3
								x + width/2, // 4
								x + width,   // 5
								x + width/2, // 6
								x + width,   // 7
								x + width/3, // 8
								x,           // 9
								x + width/4  // 10
								};

	int[] coordinateY = {y,              // 1
							y,              // 2
							y - width/3,    // 3
							y,              // 4
							y,              // 5
							y + width/3,    // 6
							y + width/2,    // 7
							y + width/3,    // 8
							y + width/2,    // 9
							y + width/3     // 10
							}; 

	
	// Default constructor
	public Star()
	{
		width = 50;
		filled = false;

	} // end constructor

	public void draw( Graphics g )
	{
		if ( filled == false )
			g.drawPolygon(coordinateX, coordinateY, 10);
		else
			g.fillPolygon(coordinateX, coordinateY, 10);
	}

	// getter methods
	public double getX()
	{
		return x;
	}

	public double getY()
	{
		return y;
	}

	public double getWidth()
	{
		return width;
	}

	public boolean getFilled()
	{
		return filled;
	}

	// setter methods
	public void setX( int newX )
	{
		x = newX;
	}

	public void setY( int newY )
	{
		y = newY;
	}

	public void setWidth( int newWidth )
	{
		width = newWidth;
	}
	
	public void setFilled( boolean newFilled )
	{
		filled = newFilled;
	}

} // endl class 

Was This Post Helpful? 0
  • +
  • -

#9 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: class error

Posted 07 October 2010 - 03:03 PM

In order to use the paint() method the way you are, you will want to extend either JComponent or something like JPanel and then add it to a JFrame. Unless you do this, you cannot see it at all.
Was This Post Helpful? 0
  • +
  • -

#10 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: class error

Posted 07 October 2010 - 03:06 PM

In my "tester program"?

Here is what i have for that: (though it doesn't work)

I get this error:

StarApplet.java:26: draw(java.awt.Graphics) in Star cannot be applied to ()
		 Star.draw();
		     ^
1 error


import javax.swing.*;
import java.awt.*;

public class StarApplet extends JApplet
{

   public void draw( Graphics g )
	{
	 
	    // create two Star objects
		 
		 Star s1 = new Star();
		 //Star s2 = new Star();
		 
		 // test the "setter" methods
		 
		 s1.setX(50);
		 s1.setY(100);
		 s1.setWidth(50);
		 
		 Star.draw();
		 
		 // test the "getter" methods
		 
		 System.out.println("Star 1 has x= "
		 						  + s1.getX()
								  + " y= "
								  + s1.getY()
								  + " width = "
								  + s1.getWidth());
		 	 		 
		 
	} 
} 

Was This Post Helpful? 0
  • +
  • -

#11 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: class error

Posted 07 October 2010 - 03:12 PM

This is your call

Star.draw();

this the signature of the method

public void draw( Graphics g )

you have to pass a Graphics object to the draw() method
Was This Post Helpful? 0
  • +
  • -

#12 Dogstopper   User is offline

  • The Ninjaducky
  • member icon

Reputation: 2975
  • View blog
  • Posts: 11,224
  • Joined: 15-July 08

Re: class error

Posted 07 October 2010 - 03:17 PM

Right...first off, you should use your instance variables, not the class variables here:
Star.draw();



Should be
s1.draw();



However, that's STILL wrong because draw a graphics object. So, your approach is wrong. Take a step back to you other class. You will want to make it extend something paintable, like JPanel and then use the paintComponent() method:
public class Star extends JPanel
{
	...

	public void paintComponent( Graphics g )
	{
		if ( filled == false )
			g.drawPolygon(coordinateX, coordinateY, 10);
		else
			g.fillPolygon(coordinateX, coordinateY, 10);
	}

	...

} // endl class 



Now, all you have to do is call repaint() on the Star's objects and it will in turn call paintComponent().

Let's look at your JApplet. Since you are using a JApplet, you shouldn't have a main() method. Only JFrame has a main() method. Use the init() method of JApplet to start things...like a constructor:

import javax.swing.*;
import java.awt.*;

public class StarApplet extends JApplet
{
    Star s1;

   public void init()
	{
	 
	    // create two Star objects
		 
		 s1 = new Star();
		 
		 // add the star to the screen
		 add(s1);

		 
		 // test the "setter" methods
		 
		 s1.setX(50);
		 s1.setY(100);
		 s1.setWidth(50);
		 	 	 		 
		 
	} 

        public void start() {
                 s1.repaint();
            
		 // test the "getter" methods
		 
		 System.out.println("Star 1 has x= "
		 						  + s1.getX()
								  + " y= "
								  + s1.getY()
								  + " width = "
								  + s1.getWidth());
        }
} 

This post has been edited by Dogstopper: 07 October 2010 - 03:19 PM
Reason for edit:: forgot a ;

Was This Post Helpful? 1
  • +
  • -

#13 pbl   User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8381
  • View blog
  • Posts: 31,956
  • Joined: 06-March 08

Re: class error

Posted 07 October 2010 - 03:21 PM

View PostDogstopper, on 07 October 2010 - 04:17 PM, said:

Right...first off, you should use your instance variables, not the class variables here:
Star.draw();



Should be
s1.draw();



Didn't even bother checking that as the class name is StartApplet (and not Star) I assumed that Star was an instance of StarApplet
Was This Post Helpful? 0
  • +
  • -

#14 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: class error

Posted 07 October 2010 - 03:31 PM

Okay everything compiles with no errors, now i can get the applet to run but nothing comes up in window (I'm running JGrasp)
I do get my system.out message in the Run I/O portion. What am i doing wrong now?

Here are both programs:

Star.java

import javax.swing.*;
import java.awt.*;

public class Star extends JPanel
{
	// instance variables
	private int x;
	private int y;
	private int width;
	private boolean filled;

	int[] coordinateX = {x,           // 1
								x + width/4, // 2
								x + width/3, // 3
								x + width/2, // 4
								x + width,   // 5
								x + width/2, // 6
								x + width,   // 7
								x + width/3, // 8
								x,           // 9
								x + width/4  // 10
								};

	int[] coordinateY = {y,              // 1
							y,              // 2
							y - width/3,    // 3
							y,              // 4
							y,              // 5
							y + width/3,    // 6
							y + width/2,    // 7
							y + width/3,    // 8
							y + width/2,    // 9
							y + width/3     // 10
							}; 

	
	// Default constructor
	public Star()
	{
		width = 50;
		filled = false;

	} // end constructor

	public void paintComponent( Graphics g )
	{
		if ( filled == false )
			g.drawPolygon(coordinateX, coordinateY, 10);
		else
			g.fillPolygon(coordinateX, coordinateY, 10);
	}

	// getter methods
	public int getX()
	{
		return x;
	}

	public int getY()
	{
		return y;
	}

	public int getWidth()
	{
		return width;
	}

	public boolean getFilled()
	{
		return filled;
	}

	// setter methods
	public void setX( int newX )
	{
		x = newX;
	}

	public void setY( int newY )
	{
		y = newY;
	}

	public void setWidth( int newWidth )
	{
		width = newWidth;
	}
	
	public void setFilled( boolean newFilled )
	{
		filled = newFilled;
	}

} // endl class



StarApplet.java

import javax.swing.*;
import java.awt.*;

public class StarApplet extends JApplet
{
    Star s1;
	 Star s2;

   public void init()
	{
	 
	    // create two Star objects
		 s1 = new Star();
		 s2 = new Star();
		 
		 // add the star to the screen
		 add(s1);
 
		 // test the "setter" methods
		 s1.setX(50);
		 s1.setY(100);
		 s1.setWidth(50);
		 
	} 

        public void start() {
                 s1.repaint();
            
		 // test the "getter" methods
		 
		 System.out.println("Star 1 has x= "
		 						  + s1.getX()
								  + " y= "
								  + s1.getY()
								  + " width = "
								  + s1.getWidth());
        }
} 

Was This Post Helpful? 0
  • +
  • -

#15 <3DIC   User is offline

  • D.I.C Regular


Reputation: 6
  • View blog
  • Posts: 327
  • Joined: 06-October 10

Re: class error

Posted 07 October 2010 - 04:32 PM

Why doesn't it draw anything to the screen?
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2