Results 1 to 6 of 6

Thread: Timer Class to flicker the color of the numbers

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Unhappy Timer Class to flicker the color of the numbers

    i have wrote most of my sudoku code including the gui. i need help flickering two colors for a solved row, col, or 3*3 box. i have a writeNumber method that will probably make the new method easier(it takes the logical x/y coordinates and translate them into phyiscal coordinates.). i am very new to the timer class and i know i need it for this feature. i also think i need another method to detect a row,col, or the box being solve.



    Here is the writeNumber method:
    private void writeNumber(Graphics g, byte x, byte y, String number, Color color)
    {

    final int factor = 40;

    g.setFont(new Font("Serif", Font.BOLD, 30));
    g.setColor(color);

    if (!number.equals(""))
    {

    byte temp = Byte.parseByte(number);

    g.drawString(String.valueOf(temp), 12 + x * factor, 30 + y * factor);
    }
    }

  2. #2
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Timer Class to flicker the color of the numbers

    Code:
    import java.util.*;
    
    public class TimerExample {
      private Timer timer;
      public TimerExample() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
          public void run() {
            //Add here what you want to do everytime
          }
          //The timer will run the task after 0ms and will repeat after each 500ms
        }, 0, 500);
      }
    }
    EDIT: to terminate all timer tasks use
    Code:
    timer.cancel();
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  3. #3

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Re: Timer Class to flicker the color of the numbers

    How would i incorporate the flickering of the colors into my (Sudoku)gameboard.

  4. #4
    Arabic Poster ComputerJy's Avatar
    Join Date
    Nov 2005
    Location
    Happily misplaced
    Posts
    2,513

    Re: Timer Class to flicker the color of the numbers

    Code:
    import java.util.*;
    import java.util.Timer;
    
    import java.awt.*;
    import javax.swing.*;
    
    public class SudokuGame
        extends JFrame {
      private Timer timer;
      private Color color;
      public SudokuGame() {
        timer = new Timer();
        timer.schedule(new TimerTask() {
          public void run() {
            if (color.equals(Color.BLUE)) {
              color = Color.RED;
            }
            else {
              color = Color.BLUE;
            }
            repaint();
          }
          //The timer will run the task after 0ms and will repeat after each 500ms
        }, 0, 500);
        timer.cancel();
      }
    }
    This code switches between Blue and Red every 500ms, override the paint(Graphics g) and use the g.setColor(color); before painting
    "I'm not normally a praying man, but if you're up there, save me... Superman!" - Homer Simpson
    My Blog

  5. #5
    Frenzied Member System_Error's Avatar
    Join Date
    Apr 2004
    Posts
    1,111

    Re: Timer Class to flicker the color of the numbers

    A Timer object is probably the best route to go. You can always use Thread objects, but the coding and running of the program can get quit messy when you want everything to flow smoothly. Use CJ's example and you should be fine.

  6. #6

    Thread Starter
    New Member
    Join Date
    Jul 2006
    Posts
    3

    Re: Timer Class to flicker the color of the numbers

    Thanks very much ^^^^^^^^^^^

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width