Categories

JAVA DATEBASE
Technology Network Community
Oracle Database
Fusion Middleware
Development Tools
Java
Desktop
Server & Storage Systems
Enterprise Management
Berkeley DB Family
Cloud Computing
Big Data
Business Intelligence
Architecture
Migration and Modernization
E-Business Suite
Siebel
PeopleSoft Enterprise
JD Edwards World
Industries
JD Edwards EnterpriseOne
User Productivity Kit Pro (UPK) and Tutor
Governance, Risk & Compliance (GRC)
Master Data Management (MDM)
Oracle CRM On Demand
On Demand: SaaS and Managed Applications
AutoVue Enterprise Visualization
Primavera
ATG
Agile PLM
Endeca Experience Management
Fusion Applications
Archived Forums

 



Tags

New To Java


Private inner classes


I'm trying to complete a "turn the lightbulb on and off" program, but when I try to draw circle2 in the  ButtonListener class I get an error message cannot find symbol. This is in reference to the Graphics variable "page" created in the paintComponent method below. Shouldn't the inner class, private or  public inherit all data variables including objects from the parent class, in this case, the Bulb class? The code is below. By the way, this IS NOT a school assignment so any help would be appreciated. I'm just trying to learn this language.  import java.awt.*; import java.awt.event.*; import javax.swing.*;  public class Bulb extends JPanel { 	private JButton push; 	private Circle circle, circle2; 	private final int DIAMETER = 100; 	private final int X = 10; 	private final int Y = 10;  	public Bulb() 	{ 		circle = new Circle(DIAMETER, Color.white, X,Y); 		circle2 = new Circle(DIAMETER, Color.yellow, X, Y);  // to separate class  		push = new JButton("Turn on the Bulb"); 		push.addActionListener(new ButtonListener());  		add(push);  		setPreferredSize(new Dimension(500, 500)); 		setBackground(Color.black); 	}  	public void paintComponent(Graphics page) 	{ 		super.paintComponent(page);  		circle.draw(page); 	}      private class ButtonListener implements ActionListener    {    		public void actionPerformed(ActionEvent event)   //PROBLEM AREA. I GET ERROR MESSAGE STATING                                                                                        // "CANNOT FIND SYMBOL" IN REFERENCE TO VARIABLE "PAGE."                                                                                       // I THOUGHT THE INNER CLASS INHERITS ALL DATA FROM                                                                                       // PARENT CLASS SUCH AS "PAGE."    		{  			circle2.draw(page);  		}     }  }

The variable "page" is a parameter to your paintComponent method, thus making it a local variable. You cannot access it outside this method. Regardless of inner/outer classes.

Use code tags when you post code.  You do not have a "page" member variable.  You have a "page" method attribute.  The two are not the same.

I tried this, but I get another error message illegal start of expression regarding the public void paintComponent(Graphics page). And I guess I don't know the difference between and attribute and a variable. They both store values. Are attributes what objects store? Are variables their own, i.e, they're not stored in objects but stored in their own memory location regardless of any objects?   private class ButtonListener implements ActionListener    {    		public void actionPerformed(ActionEvent event)    		{ 			public void paintComponent(Graphics page)    // PROBLEM AREA; ILLEGAL START OF EXPRESSION. WHY? 				{ 					super.paintComponent(page);  					circle2.draw(page); 				}  		}  }

Is this variable "page" a formal parameter that is passed to some other parameter in the Java library?

You are trying to creating a method inside another method. This is a no-no.

Thanks, I didn't know you couldn't do that.

There are fields, which are associated with either a class or an object (and thus live in the heap in an object on the heap), and there are local variables, which are associated with methods and threads (i.e., a method invoked within a thread, and which thus live on the stack).  They're not the same thing.  You can't use a local variable in your paintComponent method in a different method.  Anyway you're designing your class wrong.  Think model-view-controller.  You have the model: a bunch of state and possibly behavior that represents the thing being seen, modified, and displayed.  You have the view, which is how you see the model.  And you have the controller, which modifies the model.  Your event handlers are part of the controller.  They should change the model. Your paintComponent method is part of the view.  So the event handlers should change some data, e.g., add a note that a circle should be displayed. Then your paintComponent method should look at the data and act accordingly -- e.g., see that there's a circle to be displayed, and display it.

I'm still having a hard time. I placed a setColor method in the ButtonListener class as follows:   private class ButtonListener implements ActionListener    { 	   public void actionPerformed(ActionEvent event) 	   { 			circle.setColor(Color.yellow); 	   }   }  that went to the Circle class as follows:  public void setColor(Color c) 	{ 		color = c; 		page.setColor(color); 		page.fillOval(X, Y, diameter, diameter); 	}  But the program has to draw this and since you cannot put a paintComponent method inside another method, I'm stuck. In fact, you can't even call that method.

So, what part of what I said before did you not understand?

It's the code I'm having trouble with. How can you draw another circle - after you click the mouse - if you cannot put a draw method within the actionPerformed method? Similarly, if I call another method, say in another class, I'm faced with the same problem. You can't call the paintComponent method directly (I already tried that), so I have to create another method that draws the new circle, that is, a circle with a different color representing the turned on light. But that method cannot include a paintComponent method either, and you need this method to draw the new circle. By the way, I should have mentioned earlier that I cannot use decision boolean statements like if, else, etc. nor can I use loops. These structures are in the next chapter. If you could point me to some examples of lightbulb on and off problems I'd appreciate it. I've already googled that and didn't find much.

Note: This thread was originally posted in the [Java Programming|http://forums.sun.com/forum.jspa?forumID=31] forum, but moved to this forum for closer topic alignment.

afried01 wrote: I cannot use decision boolean statements like if, else, etc. nor can I use loops. These structures are in the next chapter. You're being taught GUIs and custom painting before covering conditional program flow, loops and data structures?  Quit the course and ask for your money back.  Seriously.  db

I'm not taking a course. I'm studying a Java textbook by Lewis & Loftus that happens to cover these graphics topics before loops and decision structures. Maybe I'll just read ahead and come back to this problem later.  Thanks

Ouch. You weren't kidding about that. ( [Table of Contents|http://www.pearsonhighered.com/educator/academic/product/0,3110,0321409493,00.html] )  Applets in chapter 2 but the if-statement not until chapter 5? This is one of those books from the Dark Ages of Java when people thought that applets were the coolest thing ever, so they put them up front in their books. And then the web was littered with applets which looked like they were written by a chimpanzee with a chain saw. I wouldn't recommend that book.


Related Links

Help PLEASE with linked list. Inserting a string in the middle of
why applet code in the comment line
multipart/mixed
Array size!!
Why does if statement fail to compare string correctly?
synchonization(this) ..what is the porpose of it?
call constructor of the superclass
using an array of primitive
GetOpts class
The server encountered an internal error () that prevented it from fulfilli
adding support of other api in netbeans
Help with a program. GUI involved
Using Hashmap with ArrayList in java code
parsing in XML
null pointer exception...
Standard Deviation help!