More java help

A place to discuss the implementation and style of computer programs.

Moderators: phlip, Moderators General, Prelates

asshammer
Posts: 23
Joined: Thu Sep 20, 2007 12:25 pm UTC
Location: Dallas
Contact:

More java help

Postby asshammer » Wed Nov 14, 2007 3:48 am UTC

I've got a quick question with a project I'm working on. In it a created a jframe object and need to access a Timer variable in it. I've tried making it public and I've tried adding an accessor method. Either way it says that "frame" cannot be found. I've got no clue what I'm doing wrong. Thanks a ton in advance.

Spoiler:

Code: Select all

/**
 * @(#)projectFive.java
 *
 *
 * @author
 * @version 1.00 2007/11/13
 */
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class projectFive {

    public static void main(String[] args)
   {
      JFrame frame = new JFrame("fan");
      //fan fanObj = new fan();
      JPanel buttonpanel = new JPanel();
      JButton highbutton = new JButton("High");
      JButton medbutton = new JButton("Medium");
      JButton lowbutton = new JButton("Low");
      JButton offbutton = new JButton("Off");
      buttonpanel.setLayout(new GridLayout(4, 1));
      buttonpanel.add(highbutton);
      buttonpanel.add(medbutton);
      buttonpanel.add(lowbutton);
      buttonpanel.add(offbutton);
      
      highbutton.addActionListener(new actionHigh());
      medbutton.addActionListener(new actionMed());
      lowbutton.addActionListener(new actionLow());
      offbutton.addActionListener(new actionOff());
      
      
      fan drawing = new fan();
      frame.setLayout(new BorderLayout());
      frame.add(drawing);
      frame.add(buttonpanel, BorderLayout.SOUTH);
      
      frame.setLocationRelativeTo(null);
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
       frame.setSize(200, 250);
          
       
       frame.setVisible(true);
       //frame.timer.setDelay(20);      //cannot find symbol
       //frame.setHigh();            //cannont find symbol
       
       
          
          
     }
     
}

     
  class actionHigh implements ActionListener
     {
     
        public void actionPerformed(ActionEvent e)
        {
           
           //projectFive.frame.timer.setDelay(10);      //cannot find symbol
           //projectFive.frame.setHigh();            //cannot find symbol
           System.out.println("pressed high");
        }
     }
  class actionMed implements ActionListener
     {
     
        public void actionPerformed(ActionEvent e)
        {
           
           System.out.println("pressed med");
        }
     }
  class actionLow implements ActionListener
     {
     
        public void actionPerformed(ActionEvent e)
        {
           //projectFive.callLow();
           System.out.println("pressed low");
        }
     }
  class actionOff implements ActionListener
     {
     
        public void actionPerformed(ActionEvent e)
        {
           //projectFive.callOff();
           System.out.println("pressed off");
        }
     }
   
   

Code: Select all

/**
 * @(#)fan.java
 *
 *
 * @author
 * @version 1.00 2007/11/13
 */
import java.awt.*;
import javax.swing.*;
//import java.util.*;
import java.awt.event.*;

public class fan extends JPanel
{
   private int _theta = 0;
   public Timer timer = new Timer(10, new TimerListener());
   
    public fan()
    {
       //Timer timer = new Timer(10, new TimerListener());
      //timer.start();
    }
   
    public void testIn()
    {
       System.out.println("hello");
    }
   
    public void setHigh()
    {
       timer.setDelay(10);
       if (timer.isRunning())
            {
            }
            else
            {
               timer.start();
            }
    }      
   
    public void setMedium()
    {
       timer.setDelay(20);
       if (timer.isRunning())
            {
            }
            else
            {
               timer.start();
            }
    }
   
    public void setLow()
    {
       timer.setDelay(30);
       if (timer.isRunning())
            {
            }
            else
            {
               timer.start();
            }
    }
   
    public void setOff()
    {
       if (timer.isRunning())
            {
               timer.stop();
            }
            
    }
   
   
   
    public void paintComponent (Graphics g)
    {
       super.paintComponent(g);
       int xCenter = (getWidth()) / 2;
      int yCenter = (getHeight() - 50) / 2;
      int radius = (int)(Math.min(getWidth(), (getHeight() - 50 )) * 0.4);
      int x = xCenter - radius;
      int y = yCenter - radius;
      g.setColor(Color.gray);
      g.fillArc(x, y, 2 * radius, 2 * radius, (0 + _theta), 30);
      //g.setColor(Color.green);
      g.fillArc(x, y, 2 * radius, 2 * radius, (90 + _theta), 30);
      //g.setColor(Color.blue);
      g.fillArc(x, y, 2 * radius, 2 * radius, (180 + _theta), 30);
      //g.setColor(Color.yellow);
      g.fillArc(x, y, 2 * radius, 2 * radius, (270 + _theta), 30);
    }
   
    public void upTheta ()
   {
      _theta += 10;
   }
   
   public Dimension getPreferredSize()
   {
      return new Dimension(200, 200);
   }
   
   private class TimerListener implements ActionListener
   {
      public void actionPerformed(ActionEvent e)
      {
         upTheta();
         repaint();
      }
   }
   
   
}

User avatar
pieaholicx
The cake is a lie!
Posts: 531
Joined: Mon Oct 22, 2007 12:51 pm UTC
Contact:

Re: More java help

Postby pieaholicx » Wed Nov 14, 2007 12:50 pm UTC

Code: Select all

  class actionHigh implements ActionListener
     {
     
        public void actionPerformed(ActionEvent e)
        {
           
           //projectFive.frame.timer.setDelay(10);      //cannot find symbol
           //projectFive.frame.setHigh();            //cannot find symbol
           System.out.println("pressed high");
        }
     }


Where are you supposed to be getting the variable projectFive from? At the moment your class actionHigh has no variables of its own, has not been passed any variables, and barely even knows that there is a class called projectFive. The way you're going about it now it's looking at it as "I need the static variable frame from the class called projectFive", which is not what you want here.

It's been a while since I've done Swing, but I believe the way I did it was like this example, which connects the actions performed on the JButton to a function within the same class.
It's okay, I'm Chaotic Neutral. I can kill him over the loot.
Overexposure to pieaholicx may, in semi-rare cases, emancipate dental fillings, crowns, tooth enamel, and teeth.

Waterhouse
Posts: 54
Joined: Wed Oct 03, 2007 12:37 pm UTC

Re: More java help

Postby Waterhouse » Wed Nov 14, 2007 3:36 pm UTC

Please let us know whether you were able to figure or if you still need help.

Can you highlight just the portion of the code that you are trying to change and what you'd like for it to do?

asshammer
Posts: 23
Joined: Thu Sep 20, 2007 12:25 pm UTC
Location: Dallas
Contact:

Re: More java help

Postby asshammer » Wed Nov 14, 2007 5:39 pm UTC

Its figured out now. It was a scope issue. The part I was having problems out were the commented out lines that had a comment of "//cannot find symbol". Later I'll post the corrected code.


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 6 guests