-1

Here is my main program to draw a bug when rolling a dice

I need to set a timer for it?

Here I added somethings from the examples but im not sure what to put in

for (Shape shape : shapes) {

shape.move();

shape.decreaseDelay();

repaint();

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Timer;

public class Yahtzee extends JFrame implements ActionListener {

   Die[] d; // array to hold the 5 dice
   FourOfAKind[] f;
   JPanel dicePanel; // panel to hold the dice
   JPanel bugPanel;
   Timer timer = new Timer();

   public static void main(String[] args) 
   {
      Yahtzee y = new Yahtzee();
   }

   public Yahtzee() 

   {

      setDefaultCloseOperation(EXIT_ON_CLOSE);

      setLayout(new FlowLayout());

      setLayout(new BorderLayout());

      timer = new Timer(30, new ActionListener() {

        public void actionPerformed(ActionEvent e) {

            for (Shape shape : shapes) {

                shape.move();

                shape.decreaseDelay();

                repaint();
            }
        }
    });

    JButton start = new JButton("Start");

    start.addActionListener(new ActionListener()

    {


        public void actionPerformed(ActionEvent e) 

        {

            timer.start();
        }
    });

    JPanel panel = new JPanel();

    panel.add(start);

      dicePanel = new JPanel();

      dicePanel.setLayout(new GridLayout(5, 1));

      dicePanel.setLayout(new FlowLayout());

      dicePanel.setSize(new Dimension(50, 50));

      add(dicePanel, BorderLayout.CENTER);

      d = new Die[1];

      for (int i = 0; i < 1; i++) {

         d[i] = new Die(this);

         dicePanel.add(d[i]);

      }
      bugPanel = new JPanel();

      bugPanel.setLayout(new GridLayout(5, 5));

      bugPanel.setLayout(new FlowLayout());

      bugPanel.setSize(new Dimension(50, 50));

      add(bugPanel, BorderLayout.SOUTH);

      f = new FourOfAKind[1];

      for (int w = 0; w < 1; w++) {

         f[w] = new FourOfAKind(this);

         bugPanel.add(f[w]);

      }

      setSize(new Dimension(715, 705));

      setVisible(true);
   }

   public void actionPerformed(ActionEvent e) {

      repaint();

      timer.start(); // start timer

   }

   public void setChoice(int choice) {

      f[0].setChoice(choice);
   }

   public void drawBug() {

      f[0].setChoice(d[0].getChoice());

      f[0].drawBug();
   }
}

1 Answers1

1

I think you want a javax.swing.Timer istead. In that case:

  • This Timer timer = new Timer(); won't work. You need to pass arguments to it

    Timer timer = new Timer(1000, this);
    
  • Also not import java.util.Timer; instead import javax.swing.Timer;

  • Also don't start the timer in the actionPerformed since that's where the magic happens. Start it in the constructor, or create a separate listener for the timer and a button to start the timer, or put an if statement to check for a button or a timer object as the ActionEvent source.

  • Also besides just repaint()ing, in the actionPerformed, you'll want to change the value of something then call repaint(). If you don't change anything, no animation effects will been see.

  • See more at How to use Swing Timers

  • See a bunch of examples here and here and here and here and here


Other than that, your question is not specific enough. In its current state, it just reads as "Help me develop my program"

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • If you don't mind can you write it in a code. – user3385677 Mar 12 '14 at 01:54
  • 2
    @user3385677: um, that's ***your*** job. At least **try** and then if stuck, come up with a specific question. – Hovercraft Full Of Eels Mar 12 '14 at 01:55
  • 1
    @user3385677 look at some of the examples I linked. If you still don't get it, look at the tutorial I linked. Try something, then come back if you're stuck. But right now, we have idea what you want to do, because your question is too vague. So work on and come back if you need help. I think I gave you enough resources to start with – Paul Samsotha Mar 12 '14 at 02:00
  • for (Shape shape : shapes) { shape.move(); shape.decreaseDelay(); repaint(); not sure what to put in this part? – user3385677 Mar 12 '14 at 02:26
  • First do you know what that code does? You need to figure what what you need animated, and and figure out what values need to be changed to create the animation. You question again is too broad. As something specific regarding _your_ code, not _my_ code. – Paul Samsotha Mar 12 '14 at 02:31
  • I am really confused, I need after I rolled my dice and clicked on the screen and drew all the parts for my bug( which are lines and circles) i would click on the timer and see how long it toke me to draw my bug. I am really stuck I cant seem to understand this whole timer thing. If you dont mind helping. – user3385677 Mar 12 '14 at 03:14
  • @user3385677 Here's what I truly suggest. Ditch your current program for a couple hours. Put some time learning the `Timer` and how it works. Try the simplest of cases like making a circle move across the screen. Work it out. If you get stuck just on that program, ask another question here. Right now you are asking about a program that we have no idea how it works. A circle moving across the screen seems easy to get help with is you get stuck. If you are able to make this work on your own, then move to something more complex. Walk be before your run ;) – Paul Samsotha Mar 12 '14 at 03:19
  • Thanks. I want to post my whole program to show what im talking about but it is going to be alot, but actually im not moving anything im just drawing on the screen and I need a timer down there, it could not even be connected to what i drew, if im making sense. Thanks anyways. – user3385677 Mar 12 '14 at 03:24