0

This is a follow up to my previous question. I am making Frogger in Java I'm trying to implement a swing timer but it doesn't seem to be working. I'm having some trouble setting it up.

I've tried implementing it in different areas of my code and have come to no conclusion as to what's wrong with it. I followed multiple tutorials and nothing has worked.

 private int delay = 7;

  public CPT() {
    setLayout(new BorderLayout());
    label = new JLabel("Frogger");
    frame1 = new JFrame("Main");
    label.setFont(new Font("Serif", Font.BOLD,50));
    label.setBounds(275,10,250,250);
    button1 = new JButton("PLAY");
    button1.setBounds(300,350,100,50);
    button1.setOpaque(false);
    button1.setVisible(true);
    this.setOpaque(false);
    this.setLayout(null);
    this.add(label);
    button1.addActionListener(new ActionListener(){ 
      public void actionPerformed(ActionEvent e){
        frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame1.setVisible(true);
        frame1.setSize(700,500);
        frame1.setResizable(false);
        button1.setVisible(false);
        frame1.add(new TrainCanvas());
        frame1.add(p1);
        frame1.addKeyListener(new frog());
      } 
    });
    this.add(button1);
  }

This is the main constructor of my class

  class TrainCanvas extends JComponent {

    private int lastX = 0;
    private int lastX_1 = 0;
    private int lastX_2 = 0;


   public TrainCanvas() {
//      Thread animationThread = new Thread(new Runnable() {
//        public void run() {
//          while (true) {
//            repaint();
//            try {Thread.sleep(10);} catch (Exception ex) {}
//          }
//        }
//      });
//      
//      animationThread.start();
//    }
     Timer time = new Timer(delay, this {
          TrainCanvas.repaint();
    });

    time.start();
   }

    public void paintComponent(Graphics g) {
      Graphics2D gg = (Graphics2D) g;

      int w = getWidth();
      int h = getHeight();

      int trainW_1 = 100;
      int trainH_1 = 5;
      int trainSpeed_1 = 3;

      int x = lastX + trainSpeed_1;

      if (x > w + trainW_1) {
        x = -trainW_1;
      }

      gg.setColor(Color.BLACK);
      gg.fillRect(x, h/2 + trainH_1, trainW_1, trainH_1);

      lastX = x;

  //Draw Frog
      frog = new Rectangle(f_x,f_y,25,25);
      g3.fill(frog);
      g3.setColor(Color.GREEN);
    }
  }

This is the code that draws the main game I previously used a thread but was told that a swing timer is more useful.

The timer is supposed to repaint my game but it seems as if i can't even implement it properly even though I was told this was right. Any help is appreciated!

jhamon
  • 3,603
  • 4
  • 26
  • 37
Zoulis
  • 11
  • 1
  • 2
    Start by learning the basics of a Timer with a simple example: https://stackoverflow.com/a/7816604/131872. Then try a more complex example: https://stackoverflow.com/a/7816604/131872. The key point is that the Timer should invoke a method to change the state of your class. Then you invoke repaint(). The paintComponent() method should NEVER change the state, only paint the current state. *The timer is supposed to repaint my game* - did you add any debug code? Did you verify that the Timer fires? – camickr Jun 11 '19 at 14:33
  • 1
    Variable like trainSpeed etc should NOT be defined in the painting method. They are properties of the class an should be defined as instance variables of the class. Then you add "setter" methods if you ever want to change their values. Don't create a new Frog class in the paintComponent() method. Again the Frog is an instance variable. When the Timer fires you update its x/y position. – camickr Jun 11 '19 at 14:33
  • This is not even valid Java syntax: `Timer time = new Timer(delay, this { TrainCanvas.repaint(); });` Further, try to describe your problem more specific than saying “it doesn't seem to be working” or “I'm having some trouble” or “it seems as if i can't even implement it properly”… – Holger Jun 11 '19 at 17:16

0 Answers0