Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

JAVA PUBLIC METHOD TO DETERMINE/MAINTAIN STATE & PRINT JPEG + STATEMENT TO CONSOLE

Posted on 2006-05-08
5
Medium Priority
?
288 Views
Last Modified: 2010-05-18
I'm adding some code to my football fantasy program. I am using a class file and a driver program. In the class file I'm trying to use a boolean method that will determine AND maintain an on or off state. I would appreciate if someone could review my code ASAP and indicate any syntax errors and how to fix them and show replacement code. Also, not sure how to call an image for display on screen; is that code in need of correction too?

-------------------------------------CLASS FILE CODE FOR BOOLEAN METHOD------------------

  //method lightsOn (print out "Stadium lights are on")
   
   public void lightsOn()
        {
        System.out.println("Stadium lights are on");
        }
       
   //method lightsOff (print out "Stadium lights are off")
   public void lightsOff()
               {
         System.out.println("Stadium lights are off");
                }
   //method to keep track of light status & display on screen(use a boolean flag status)
   public boolean areLightsOnOrOff()
         {
            
                               System.out.println("Stadium lights are on");
                               panel.add(new ImageIcon(Toolkit.getDefaultToolkit().createImage  ("LIGHTS ON.jpg")));

                               return (on);
                }
 

      else
            System.out.println("Stadium lights are off");
            panel.add(new ImageIcon(Toolkit.getDefaultToolkit().createImage  ("LIGHTS OFF.jpg")));
                                 return (off);            
   }
}

            
0
Comment
Question by:computerese
  • 4
5 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 16632174
You don't need to add an Icon each time - just change the Image.

>>public boolean areLightsOnOrOff()

can be

public boolean areLightsOn()
0
 

Author Comment

by:computerese
ID: 16632344
@CEHJ

I'm getting an "illegal start of type" error for the else. I am wondering if you could take a second and maybe closer look at the entire code. I'm not sure if this will even work.

Appreciate your continued professionalism, thoroughness and patience.

Computerese
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 16632560
I would do it something like this:


//In the constructor

lightsOnIcon = new ImageIcon("LIGHTS ON.jpg");
lightsOffIcon = new ImageIcon("LIGHTS OFF.jpg");


//
public boolean areLightsOn {
      return lightsOn;
}

public void toggleLights() {
      lightsOn = !lightsOn;
      if (lightsOn) {
            lightsOnLabel.setIcon(lightsOnIcon);
      }
      else {
            lightsOnLabel.setIcon(lightsOffIcon);      
      }
}
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16632566
( An ImageIcon is normally in a JLabel )
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 16636947
:-)
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month13 days, 1 hour left to enroll

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question