solving
Hi every1,
This is my first post here. Ive been programming in java for some weeks now (im a student) and half way through writing this code i got this error:
"x is not public in Java.awt.Component; cannot be accessed from outside the package"
Im not sure what it means and what i need to do to fix it. The bit that its highlighted is part of a switch statement im trying to use in a program that supposed to different draw shapes when you click two different buttons.
"public void paintComponent(Graphics g)
{super.paintComponent(g);
switch(shape)//beginning of switch
{
case '1': g.drawOval(x-30,y-30,40,40);break;
case '2': g.drawRect(x-30,y-30,40,40);break;
case '3': g.drawPolygon(x-30,y-30,40,40);break;
default: System.out.print("Select a Shape");
} //end of switch"
The error is refering to line:5 "case '1': g.drawOval(x-30,y-30,40,40);break;" Im really stuck and its possibly something really simple that ive overlooked. I would really appreciate any help you guys can give.
thnx
cybersurfur
> "x is not public in Java.awt.Component; cannot be
> accessed from outside the package"
>
> Im not sure what it means and what i need to do to
> fix it.
You can only access class members your're allowed to access. For example only public class members can be accessed outside the package where the class resides.
To me it seems the compiler is under the impression you're trying to ge at something that's in the Java.awt.Component package you're not privvy to. The way out of it is stopping to do that.
uj_a at 2007-7-8 1:33:30 >

Im not sure what you mean by "The way out of it is stopping to do that"could you explain that a little more pls.thnx
The problem is that you use the variable x where it is not defined. For the compiler the situation is essenitally this:class Sample {
void method() {
int y = x + 1; // what's x ?
}
}
Probably you've declared x in a different method. If you declare a variable in a method, according to Java's scope rules it is visible only in that method:
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/scope.html
The solution is to move the declaration of x outside all methods, so that x can be accessed by every method of the class.
Unfortunately the problem is not immediately clear from the error message because the compiler can find a different variable also called x from the superclass java.awt.Component. This other variable is not accessible, so the error message says "cannot be accessed" rather than the clearer "cannot find symbol".
I'll give you the whole class code, so you can see what im trying to do and then you'll see:
import java.awt.*;
import java.awt.Graphics.*;
import javax.swing.*;
class DrawingSurface extends JPanel
{
int shape;
public void init()
{
String input;
input = JOptionPane.showInputDialog(
"Enter 1 to draw a Circle\n" +
"Enter 2 to draw a Square\n" +
"Enter 3 to draw a Rectange\n" );
shape = Integer.parseInt(input);
}
public void paint(Graphics g)
{
switch(shape)//beginning of switch
{
case '1': g.drawOval(x-30,y-30,40,40); break;
case '2': g.drawRect(x-30,y-30,40,40); break;
case '3': g.drawRect(x-30,y-50,40,60); break;
default: System.out.print("Select a Shape");
} //end of switch
}
public void draw(int x, int y){
this.x = x;
this.y = y;
repaint();
}
}
Basicly its supposed to bring up a promt at the beginning and ask if you want to draw a circle, square or rectangle (i'll prob add more shapes later once i get this working) and after choosing a shape its supposed to let you choose where to draw in (by simply clicking somewhere on the JPanel). But ive been having no luck so far.
Ive also got a seperate class that handles the mouse click events, defining the x and y . If you need a look at that let me know.
and im aware this would be a lot easier as an Applet but i need to do it as a swing. So id be really greatful if you could help us out.
> class DrawingSurface extends JPanel
> {
> int shape; // ok, here's the shape member, but where are the x and y members?
> public void paint(Graphics g) {
> case '1': g.drawOval(x-30,y-30,40,40); break;
> }
> public void draw(int x, int y){
> this.x = x;
> }
I don't see from your code where you've declared any x nor y members of this class. So what are you expecting it to do other than give you that error message?
well they're below
public void draw(int x, int y){
this.x = x;
this.y = y;
repaint();
}
the x and y basicly refer to a the positioning of the mouse and its click event which is coded as a seperate class and defined there. huh mayb thats the problem, maybe i need it to look at the other class for defining what they are. If you'd like i could provide you with the code for that aswell if it helps.
You need to look at where you are declaring the x and y variable and consider what scope access restrictions (public, private etc...) they have.