Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Will this keep staying at 0 like I fear it might?

  1. #1
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Cool Will this keep staying at 0 like I fear it might?

          private String minutes3;
          private Long millis;
          private String seconds3;
          private Long seconds4;
          private String hours3;
          private Long hours4;
          private Long minutes4;
          private Timer gameTimer;
          private Long millis2;
          private JLabel gameTimerLabel;
          private Date dateObject;
          private Long millis3;
          private Long timerSeconds;
    		private String minuteString3;
    		private String secondString3;
    		private String hourString3;
     
     
     
     
     
          private class GameTimer implements ActionListener
          {
             private Clock clock;
     
             private class Clock
             {
                private Long hours, minutes5, seconds, milliseconds;
     
                public Clock(Long hours, Long minutes5, Long seconds, Long milliseconds)
                {
                   setHours(hours);
                   setMinutes(minutes5);
                   setSeconds(seconds);
     
                }
     
                public void setHours(Long hours)
                {
                   this.hours = hours;
                }
     
                public Long getHours()
                {
                   return hours;
                }
     
                public void setMinutes(Long minutes5)
                {
     
                   this.minutes5 = minutes5;
                   if (minutes5 > 59)
                   {
                      setMinutes(new Long(0));
                      setHours(getHours() + 1);
                   }
     
                }
     
                public Long getMinutes()
                {
                   return minutes5;
     
                }
     
                public void setSeconds(Long seconds)
                {
                   this.seconds = seconds;
                   if (seconds > 59)
                   {
                      setSeconds(new Long(0));
                      setMinutes(getMinutes() + 1);
     
                   }
     
                }
     
                public Long getSeconds()
                {
                   return seconds;
     
                }
     
                public void setMilliseconds(Long milliseconds)
                {
                   this.milliseconds = milliseconds;
                   if (milliseconds > 999)
                   {
                      setMilliseconds(new Long(0));
                      setSeconds(getSeconds() + 1);
                   }
     
                }
     
                public Long getMilliseconds()
                {
                   return milliseconds;
                }
             }
     
             public void actionPerformed(ActionEvent e)
             {
                dateObject = new Date();
                millis = dateObject.getTime();
     
                millis2 = System.currentTimeMillis();
     
                millis3 = millis2 - millis;
     
                clock = new Clock(new Long(0), new Long(0), new Long(0), millis3);
     
     
             	Long l = clock.getSeconds();
     
     
     
    				if (l < 10)
     
    				secondString3 = "0" + l.toString();
     
     
    			else
    			secondString3 = l.toString();
     
     
     
     
     
             }
     
          }

    These are actually an inner class and an inner class within an inner class, hence the private modifiers.

    I was also going to put the Date object in the constructor of my actual class, to avoid it getting changed every time by the ActionListener that goes with a Timer.

    However, my fear is that, though I spent time on the Clock class, when the ActionListener is called by the Timer object (not shown here for simplicity's sake) will it only be able to reach 1 second and then keep resetting to 0 because of the way I set up the Clock object clock? Also, why does it require me to use new Long(0), instead of just 0? I tried 0 and it said

    found: int
    required: Long


    If the Clock object is being reset every time the ActionListener is called to 0 hours, 0 minutes, and 0 seconds, how do I fix it or put it somewhere else where it won't run into that problem?

    The full code is below, in case you have to see the entire program:
       import java.awt.*;
       import java.util.*;
       import java.io.*;
       import javax.swing.*;
       import javax.swing.JOptionPane;
       import javax.swing.Timer;
       import java.awt.event.*;
     
       public class PegGame extends JFrame
       {
     
       /*  peg1, peg2, peg3, peg4, peg5, peg6, peg7, peg8, peg9, peg10, peg11, peg12, peg13, peg14, and peg15 - The 15 Peg objects that correspond to the 15 pegs in the game.
       panel- the JPanel object
       timer- a Timer object that I might need to redraw the pegs
       fileReader-  reads the high score file (Is handled if it doesn't exist.)
       file - the File object that contains the high scores , but where to put it, I don't know.
       highScoreRank - the ranking  like 1, 2, 3, ,4 and so on.  Has no maximum limit.
       textRank-  a String, could be either "Peg Master" (1 peg left)  "Not Bad"(2 pegs left),  "Needs Improvement"(3 pegs left) or "Hopeless" (4 or more pegs left).
       name- the String to corresponders to the name of the high score holder
       date- the date the person in question got the high score (note, this is a String, not a Date or Calendar object.)
       newText- this is to correspond to the stuff read in.  It is saved and then altered when more is added if someone gets a high score.
       It was created so that I don't have to mess with the getText() method of the JLabel and risk getting the header text read in/out and causing errors later on. 
       oldText- the original label headings. 
       scoreLabel - a JLabel that contains the text for the high score page. */
     
          private Peg peg1,peg2, peg3, peg4, peg5, peg6, peg7,
          peg8, peg9, peg10, peg11, peg12, peg13, peg14, peg15;
          private JPanel panel;
          private Timer timer;
          private Scanner fileReader;
          private File file;
          private Integer highScoreRank;
          private Integer pegsLeft;
          private String textRank;
          private String name;
          private String date;
          private String newText;
          private String oldText;
          private  JLabel  scoreLabel;
          private JFileChooser load2, save2;
       //private FileChooserFilter pegFilter;
          private Scanner fileReader2;
          private FileWriter writer;
          private JMenuBar jMenuBar;
          private JMenu file2, about;
          private HelpMenu hm;
          private JMenuItem load, save, newWindow, exit, help, about2, highScores2;
          private ImageIcon logo;
          private File logoFile;
          private String month;
          private Integer month2;
          private String dayOfWeek;
          private Integer dayOfWeek2;
          private String year;
          private Integer year2;
          private String dayOfMonth;
          private Integer dayOfMonth2;
          private String hour;
          private Integer hour2;
          private String minute;
          private Integer minute2;
          private String second;
          private Integer second2;
          private Integer hourOfDay;
          private String time;
          private String dateString;
          private Timer tadTimer;
          private TimeAndDateListener tadl;
          private String tadString;
          private GamePanel gamp;
          private HighScoreFile hsf;
          private String minutes3;
          private Long millis;
          private String seconds3;
          private Long seconds4;
          private String hours3;
          private Long hours4;
          private Long minutes4;
          private Timer gameTimer;
          private Long millis2;
          private JLabel gameTimerLabel;
          private Date dateObject;
          private Long millis3;
          private Long timerSeconds;
          private String minuteString3;
          private String secondString3;
          private String hourString3;
     
     
     
     
     
          private class GameTimer implements ActionListener
          {
             private Clock clock;
     
             private class Clock
             {
                private Long hours, minutes5, seconds, milliseconds;
     
                public Clock(Long hours, Long minutes5, Long seconds, Long milliseconds)
                {
                   setHours(hours);
                   setMinutes(minutes5);
                   setSeconds(seconds);
     
                }
     
                public void setHours(Long hours)
                {
                   this.hours = hours;
                }
     
                public Long getHours()
                {
                   return hours;
                }
     
                public void setMinutes(Long minutes5)
                {
     
                   this.minutes5 = minutes5;
                   if (minutes5 > 59)
                   {
                      setMinutes(new Long(0));
                      setHours(getHours() + 1);
                   }
     
                }
     
                public Long getMinutes()
                {
                   return minutes5;
     
                }
     
                public void setSeconds(Long seconds)
                {
                   this.seconds = seconds;
                   if (seconds > 59)
                   {
                      setSeconds(new Long(0));
                      setMinutes(getMinutes() + 1);
     
                   }
     
                }
     
                public Long getSeconds()
                {
                   return seconds;
     
                }
     
                public void setMilliseconds(Long milliseconds)
                {
                   this.milliseconds = milliseconds;
                   if (milliseconds > 999)
                   {
                      setMilliseconds(new Long(0));
                      setSeconds(getSeconds() + 1);
                   }
     
                }
     
                public Long getMilliseconds()
                {
                   return milliseconds;
                }
             }
     
             public void actionPerformed(ActionEvent e)
             {
                dateObject = new Date();
                millis = dateObject.getTime();
     
                millis2 = System.currentTimeMillis();
     
                millis3 = millis2 - millis;
     
                clock = new Clock(new Long(0), new Long(0), new Long(0), millis3);
     
     
                Long l = clock.getSeconds();
     
     
     
                if (l < 10)
     
                   secondString3 = "0" + l.toString();
     
     
                else
                   secondString3 = l.toString();
     
     
     
     
     
             }
     
          }
     
     
     
     
     
          private class TimeAndDateListener implements ActionListener
          {
     
             public void actionPerformed(ActionEvent e)
             {
     
     
                Calendar cal = Calendar.getInstance();
     
                month2 = cal.get(Calendar.MONTH);
     
                if (month2 == 0)  
                   month = "January";  
                else if (month2 ==1)
                   month = "February";
                else if (month2 ==2)
                   month = "March";
                else if (month2 ==3)
                   month = "April";
                else if (month2 == 4)
                   month = "May";
                else if (month2 == 5)
                   month = "June";
                else if (month2 == 6)
                   month = "July";
                else if (month2 == 7)
                   month = "August";
                else if (month2 == 8)
                   month = "September";
                else if (month2 == 9)
                   month = "October";
                else if (month2 == 10)
                   month = "November";
                else
                   month = "December";
     
                dayOfWeek2 = cal.get(Calendar.DAY_OF_WEEK);
     
                if (dayOfWeek2 == 1)
                   dayOfWeek = "Sunday";
                else if (dayOfWeek2 == 2)
                   dayOfWeek = "Monday";
                else if (dayOfWeek2 == 3)
                   dayOfWeek = "Tuesday";
                else if (dayOfWeek2 == 4)
                   dayOfWeek = "Wednesday";
                else if (dayOfWeek2 == 5)
                   dayOfWeek = "Thursday";
                else if (dayOfWeek2 == 6)
                   dayOfWeek = "Friday";
                else
                   dayOfWeek = "Saturday";
     
     
                dayOfMonth2 = cal.get(Calendar.DAY_OF_MONTH);
                dayOfMonth = dayOfMonth2.toString();
     
                year2 = cal.get(Calendar.YEAR);
                year = year2.toString();
     
                dateString = dayOfWeek + "," + month +" " +  dayOfMonth + "," + year;
                System.out.println("Date String:" + dateString);
     
     
     
                hourOfDay = cal.get(Calendar.HOUR_OF_DAY);
     
                minute2 = cal.get(Calendar.MINUTE);
                second2 = cal.get(Calendar.SECOND);
     
                if (hourOfDay > 0 && hourOfDay < 12 )
                { // beginning of if
                   if (minute2 < 10)
                   { // beginning of if
     
                      if (second2 < 10)
                      { // beginning of if
                         hour2 = hourOfDay;
                         time = hour2 + ":0" + minute2 + ":0" + second2 + " a.m.";
     
     
                      } // end if
                      else
                      { // begin else
                         hour2 = hourOfDay;
                         time = hour2 + ":0" + minute2 + ":" + second2 + " a.m.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second2 < 10)
                      { // begin if 
                         hour2 = hourOfDay;
                         time = hour2 + ":" + minute2 + ":0" + second2 + " a.m.";
                      } // end if
     
                      else
                      { // begin else
                         hour2 = hourOfDay;
                         time = hour2 + ":" + minute2 + ":" + second2 + " a.m.";
                      } // end else
     
                   } // end else
     
                } // end if
     
                else if (hourOfDay == 12)
                {  // begin else if 
                   if (minute2 < 10)
                   { // beginning of if
     
                      if (second2 < 10)
                      { // beginning of if
                         hour2 = hourOfDay;
                         time = hour2 + ":0" + minute2 + ":0" + second2 + " p.m.";
     
     
                      } // end if
                      else 
                      { // begin else
                         hour2 = hourOfDay;
                         time = hour2 + ":0" + minute2 + ":" + second2 + " p.m.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second2 < 10)
                      { // begin if 
                         hour2 = hourOfDay;
                         time = hour2 + ":" + minute2 + ":0" + second2 + " p.m.";
                      } // end if
     
                      else
                      { // begin else
                         hour2 = hourOfDay;
                         time = hour2 + ":" + minute2 + ":" + second2 + " p.m.";
                      } // end else
     
                   } // end else
     
     
                } // end else if
     
                else if (hourOfDay ==0)
                { // begin else if
                   if (minute2 < 10)
                   { // beginning of if
     
                      if (second2 < 10)
                      { // beginning of if
                         hour2 = hourOfDay;
                         time = "12" + ":0" + minute2 + ":0" + second2 + " a.m.";
     
     
                      } // end if
                      else 
                      { // begin else
                         hour2 = hourOfDay;
                         time = "12" + ":0" + minute2 + ":" + second2 + " a.m.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second2 < 10)
                      { // begin if 
                         hour2 = hourOfDay;
                         time = "12" + ":" + minute2 + ":0" + second2 + " a.m.";
                      } // end if
     
                      else
                      { // begin else
                         hour2 = hourOfDay;
                         time = "12" + ":" + minute2 + ":" + second2 + " a.m.";
                      } // end else
     
                   } // end else
     
     
                } // end of else if
     
     
                else
                { // begin else
                   if (minute2 < 10)
                   { // beginning of if
     
                      if (second2 < 10)
                      { // beginning of if
                         hour2 = hourOfDay - 12;
                         time = hour2 + ":0" + minute2 + ":0" + second2 + " p.m.";
     
     
                      } // end if
                      else
                      { // begin else
                         hour2 = hourOfDay - 12;
                         time = hour2 + ":0" + minute2 + ":" + second2 + " p.m.";
                      } // end else
     
                   } // end if
     
                   else
                   { // begin else
                      if (second2 < 10)
                      { // begin if 
                         hour2 = hourOfDay - 12;
                         time = hour2 + ":" + minute2 + ":0" + second2 + " p.m.";
                      } // end if
     
                      else
                      { // begin else
                         hour2 = hourOfDay - 12;
                         time = hour2 + ":" + minute2 + ":" + second2 + " p.m.";
                      } // end else
     
                   } // end else
                } // end else
     
                System.out.println("Time:" + time);
                System.out.println("Date String now is:" + dateString);
     
     
                tadString =   dateString + "  " + time;
                System.out.println("Tad String is:" + tadString);
     
                gamp.repaint();
     
     
     
     
             }
     
     
          }
     
     
     
          private class HelpMenu extends JFrame
          {
             private JPanel panel;
             private JLabel label;
     
             public HelpMenu()
             {
                super("Help Menu");
                panel = new JPanel();
                String str = "<html><br> Welcome to the Take All but One Peg Game.  </br>" +" <br> The object of the game is to take all but one peg. </br>" +" <br>You can jump a peg but can't move if it's not a jump. </br>" +"<br> The files are saved as .peg extensions. </br></html>";
                label = new JLabel(str);
                label.setForeground(new Color(23, 255, 23));
                label.setFont(new Font("Kristen ITC", Font.BOLD + Font.ITALIC, 20));
                panel.setBackground(new Color(234, 124, 54));
                panel.add(label);
     
                setVisible(true);
                setContentPane(panel);
     
             }
     
          }
     
          private class HighScoreFile  extends JFrame 
          {
             private JButton  reset;
             private  JPanel panel;
             private JScrollPane pane;
     
     
             public HighScoreFile()
             {
                super("High Scores");
     
                panel = new JPanel();
                panel.setBackground(Color.BLUE);
                pane = new JScrollPane(panel, JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED, JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
               // setVisible(true);
                reset = new JButton("Reset");
     
                file = new File("C:/Users/HighScores.txt");
                newText = "";
                String str = "<html><br>High Scores</br>"  +  "<br><b>Rank           			</b> " + "<b>Pegs Left           </b> " + "<b> Title               </b>"+     "<b> Name                                 </b>" +     "<b> Date                                            </b></br></html>";
                scoreLabel = new JLabel(str);
                oldText = scoreLabel.getText();
     
     
                try
                {
                   fileReader = new Scanner(file);
                   while (fileReader.hasNext())
                   {
                      highScoreRank = fileReader.nextInt();
                      pegsLeft = fileReader.nextInt();
                      textRank = fileReader.nextLine();
                      name = fileReader.nextLine();
                      date = fileReader.nextLine();
     
                      newText = newText + "<html><br>" + highScoreRank.toString() + "               " + pegsLeft.toString() + "              " + textRank + "                " +   name + "                                            " +  date + "</html></br>";
     
                      scoreLabel.setText(scoreLabel.getText() + newText);
     
                   }
                }
     
                   catch(FileNotFoundException fnfeRef)
                   {
                      scoreLabel.setText(scoreLabel.getText() + "<html><br> NO HIGH SCORES YET </br></html>");
     
     
                   }
     
                   catch(Exception e)
                   {
                      e.printStackTrace();
     
                   }
     
                panel.add(scoreLabel);
     
                panel.add(reset);
     
                reset.addActionListener(
                      new ActionListener() {
     
                         public void actionPerformed(ActionEvent e)
                         {
                            scoreLabel.setText(oldText + "<html><br> NO HIGH SCORES YET </html></br>");
                         }
                      });
               // panel.setLayout(new GridLayout(2,1));
                pack();
     
                setContentPane(pane);
             }
     
          }
     
          private class Peg 
          {
             private Point location;
             private Color color;
             private int x, y;
     
     
             public Peg(Point location, Color color)
             {
                this.location = location;
                this.color = color;
             }
     
             public void setPoint(Point location)
             {
                this.location = location;
             }
     
             public Point getPoint()
             {
                return location;
             }
     
             public void setColor(Color color)
             {
                this.color = color;
             }
     
             public Color getColor()
             {
                return color;
             }
     
             public void setX(int x)
             {
                setPoint(new Point(x,(int) location.getY()));
             }
     
     
     
             public void setY(int y)
             {
                setPoint(new Point((int) location.getX(), y));
             }
     
             public void setPoint2(int x, int y)
             {
                setPoint(new Point(x,y));
             }
     
             public boolean canJump(Peg p)
             {
     
                return false;
     
             }
     
          }
     
          private class GamePanel extends JPanel
          {
     
             protected void paintComponent(Graphics g)
             {
                super.paintComponent(g);
     
                drawBoard(g);
                drawWindowFeatures(g);
     
             }
     
             public void drawBoard(Graphics g)
             {
                Polygon poly = new Polygon();
     
                poly.addPoint(600, 95);
                poly.addPoint(280, 512);
                poly.addPoint(925, 512);
     
                g.setColor(new Color(251, 148, 79));
                g.fillPolygon(poly);
                g.setColor(Color.BLACK);
                g.drawPolygon(poly);
     
     
             }
     
             public void drawWindowFeatures(Graphics g)
             {
     
     
                g.drawLine(327, 50, 327, 83);
                g.drawLine(500, 83, 327, 83);
     
     
                g.setColor(new Color(0, 206,206));
                g.setFont(new Font("Papyrus", Font.BOLD + Font.ITALIC, 23));
                g.drawString("THE JUMP ALL BUT ONE PEG GAME", 327, 80);
     
                Font f = g.getFont().deriveFont(20);
                g.setFont(f);
     
     
                g.drawString(tadString, 920, 80);
     
     
             }
     
     
          }
     
     
     
          public PegGame()
          {
             super("Jump All But One Peg Game");
             System.out.println(dateString);
     
     
     
     
             setVisible(true);
             gamp = new GamePanel();
             hm = new HelpMenu();
             tadl = new TimeAndDateListener();
             tadTimer = new Timer(0,tadl);
             tadTimer.setDelay(1000);			
             tadTimer.start();
             hm.setVisible(false);
          // hmdll = new DoublyLinkedList<HelpMenu>();
             jMenuBar = new JMenuBar();
             setJMenuBar(jMenuBar);
             //panel.add(jMenuBar);
             file2 = new JMenu("File");
             jMenuBar.add(file2);
             about = new JMenu("About");
             jMenuBar.add(about);
             help = new JMenuItem("Help");
             about.add(help);
             about.addSeparator();
     
             load = new JMenuItem("Load");
             file2.add(load);
             file2.addSeparator();
             save = new JMenuItem("Save");
             file2.add(save);
             file2.addSeparator();
             gamp.setBackground(new Color(40, 255, 40));
     
             hsf = new HighScoreFile();
             hsf.setVisible(false);
     
             setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     
             help.addActionListener(
                   new ActionListener() {
     
                      public void actionPerformed(ActionEvent e)
                      {
     
                         if (!hm.isShowing())
                         {
     
     
                            hm.setVisible(true);
     
     
                         }
     
                         else
                         {
                            JOptionPane.showMessageDialog(null, "You already have the help menu open!", "Help menu is already open!", JOptionPane.WARNING_MESSAGE);
     
                         }
     
     
                      }});
     
             highScores2 = new JMenuItem("High scores");
             file2.add(highScores2);
             file2.addSeparator();
     
             highScores2.addActionListener(
                   new ActionListener()
                   {
     
                      public void actionPerformed(ActionEvent e)
                      {
                         if (!hsf.isShowing())
                         {
     
     
                            hsf.setVisible(true);
     
     
                         }
     
                         else
                         {
                            JOptionPane.showMessageDialog(null, "You already have the high score menu open!", "High score menu is already open!", JOptionPane.WARNING_MESSAGE);
     
                         }
     
                      }});
     
             setContentPane(gamp);
     
          }
     
     
     
          public static void main(String[] args)
          {
             PegGame pg = new PegGame();
     
     
          }
     
     
     
       }


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Will this keep staying at 0 like I fear it might?

    Quote Originally Posted by javapenguin View Post
    why does it require me to use new Long(0), instead of just 0? I tried 0 and it said

    found: int
    required: Long
    Literal values by default are ints. If you want to assign them to a long variable you need to append an 'l' or 'L' after the value. Use uppercase to avoid confusing it with the number 1.
    long value = 100L;
    As for all your other problems, that is way too much code and too vague a discription of your problem for anyone to track down. Can you scale it back to a SSCCE?
    Improving the world one idiot at a time!

  3. The Following User Says Thank You to Junky For This Useful Post:

    javapenguin (July 13th, 2011)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Will this keep staying at 0 like I fear it might?

    The first part was kinda a SSCCE.

    In short, if I have an Timer that keeps calling the ActionListener in the GameTimer method, will it keep resetting my values used for minutes, hours, and seconds back to 0 per the way I set up the Clock constructor in the Clock object clock?

    I mean, will it, if called say every second, go

    0 seconds , 1 second, 0 seconds, 1 second, etc?

    I mean, will it keep going up and up into minutes and hours or will it never get that far the way I've got it set up.

    The original code at top, if added a main method and making GameTimer public instead of private should make a compilable program I think.

    In particular, does the setup of this Clock object being declared make it so that it never increased beyond one second if called by some Timer object?

    clock = new Clock(new Long(0), new Long(0), new Long(0), millis3);

    Also, I never heard of the 100L thing before. However, I'll try that from now on.

    I fear that every time actionPerformed is called, it will keep resetting my minutes, hours, and seconds to 0 because of my Clock object's parameters.

    Where should I put it so that it could work right?

    Hmmmmmm....I could have a separate, bigger class called Clock and just call it inside the main program's constructor and see if that avoids the (potential) problem.

    Or I could make a GameTimer constructor and define the Clock object in there.

    I'm just trying to make sure that my values for hours, minutes, and seconds won't keep resetting themselves to 0 every time the ActionListener inside GameTimer is called by a Timer object.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Will this keep staying at 0 like I fear it might?

    If your actionPerformed method creates a new Clock object then yes the values will be reset. Actually the values would not be reset because you throw away the old Clock and get a new one with all its values set to the default value. Which I assume is 0.
    Improving the world one idiot at a time!

  6. #5
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Will this keep staying at 0 like I fear it might?

    Ok, my old Clock is lost.

    But if the Clock object were declared inside a GameTimer constructor, would it still keep changing or still be lost?

    Wait, how would I be able to get millis3 anyway as it would be null perhaps?

    So if I put millis2 in the constructor and have mills3 there as well, but have millis in the main class constructor, then it might work, but my Timer might be off by a millisecond or two.

    But how would my ActionListener work on the seconds, minutes, and hours?



    I've got to get the ActionListener to change values on the Clock thing, but then I'd need the Clock values again to change a JLabel for seconds, minutes, and hours.

    Hmmmmm....having Clock as its own separate class might make it able to be defined as a main class object and therefore be valid and be set BEFORE the ActionListener comes around, thus avoiding null pointers.

    So that could work, but is there a way to do it if I leave Clock as an inner class of GameTimer?

    If not, I could try the other route.

    Right now my brain is fried from too much heat exposure.