0

I have been searching for a couple hours now and can't seem to find an answer on this.

I import javax.swing.* so that i can use Timer in my program, but while Timer is imported and seem to be functioning, other methods that Timer has cannot be resolved by intellij and I get the following Errors : Errors picture enter image description here

Here is my code:

`

import java.util.Random;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;


public class Board
{
    Random rn = new Random();
    private SquareType[][] squares;
    private int height;
    private int width;
    public Poly falling;
    private int fallingX;
    private int fallingY;


    public Board(final int height, final int width) {
    this.width = width;
    this.height = height;

    squares = new SquareType[height][width];

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
        squares[i][j] = SquareType.EMPTY;
        }
    }

    }

    public int getHeight() {
    return height;
    }

    public int getWidth() {
    return width;
    }

    public SquareType whichSquareType(int height, int width) {
    //Takes in two integers one for height and one for width and returns
    // the SquareType of the particular cell
    return squares[height][width];

    }


    public void randomizeBoard() {

    SquareType[] myTypes = SquareType.values();

    for (int i = 0; i < height; i++) {
        for (int j = 0; j < width; j++) {
        squares[i][j] = myTypes[rn.nextInt(myTypes.length)];
        }
    }
    }

    public int getFallingX() {
    return fallingX;
    }

    public int getFallingY() {
        return fallingY;
    }

    public Poly getFalling() {
        return falling;
    }


    final Action doOneStep = new AbstractAction()
    {
    public void actionPerformed(ActionEvent e) {

    }
    };

    final Timer clockTimer = new Timer(500, doOneStep);
    clockTimer.setCoalesce(true);
    clockTimer.start();
}

`

Deest
  • 1

1 Answers1

2

Note you are calling Timer class methods at Board class declaration level, not from within a Board class method. Those are illegal Java statements and the actual reason of the error messages. You should encapsulate those calls in a new Board class method -let's say initTimer()- and call this method when needed.

On the other hand clockTimer variable declaration and initialization are ok.

dic19
  • 17,821
  • 6
  • 40
  • 69