Results 1 to 7 of 7
  1. #1
    doozer8688 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default Program using JPanel - problems

    Hi. I'm making a program that simulates cells in The Game of Life. It's incomplete, but I'm running into errors that I don't understand. In the class that sets up the panel, GamePanel.java, I have:

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

    public class GamePanel extends JPanel {

    private JPanel panelTop, panelBottom;
    private JButton runstop, step, clear;
    private JLabel generation, ms;
    private JTextField delay;
    private JRadioButton singleStep, animate;
    private ButtonGroup mode;
    private boolean animateMode;

    public GamePanel () {

    panelTop = new JPanel();
    panelBottom = new JPanel();

    runstop = new JButton("Run/Stop");
    step = new JButton("Step");
    clear = new JButton("Clear");
    generation = new JLabel("");
    ms = new JLabel("ms");
    delay = new JTextField(5);
    singleStep = new JRadioButton("Single Step", true);
    animate = new JRadioButton("Animate");
    animateMode = false;

    panelTop.add(runstop);
    panelTop.add(step);
    panelTop.add(clear);
    panelTop.add(generation);
    panelTop.add(ms);
    panelTop.add(delay);
    panelTop.add(singleStep);
    panelTop.add(animate);

    mode = new ButtonGroup();
    mode.add(singleStep);
    mode.add(animate);

    CommandListener cmd = new CommandListener();
    runstop.addActionListener(cmd);
    step.addActionListener(cmd);
    clear.addActionListener(cmd);
    delay.addActionListener(cmd);

    Square[] cells = new Square[400];
    for (int i = 0; i > cells.length; i++) {
    cells[i] = new Square();
    }

    private class CommandListener implements ActionListener {

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == runstop) {

    }
    else if (e.getSource() == step) {
    step();
    }
    else if (e.getSource() == clear) {

    }
    else if (e.getSource () == delay) {

    }
    if(e.getSource() == animate)
    animateMode = true;
    if(e.getSource() == singleStep)
    animateMode = false;
    }
    }
    }
    }

    I get these two compiler errors for this:

    C:\Users\Fitrah\Documents\School\CS 1331\hw8\GamePanel.java:55: illegal start of expression
    private class CommandListener implements ActionListener {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\GamePanel.java:77: class, interface, or enum expected
    }
    ^
    2 errors

    Re: the first problem, I've used that syntax before for CommandListener in other codes that have worked. What am I doing wrong? Re: "class, interface, or enum expected," I understand what the message is literally saying, but I don't see what it needs this.

    Also, I want to make a 20 by 20 grid of Squares, which I tried to do with this line:

    Square[] cells = new Square[400];
    for (int i = 0; i > cells.length; i++) {
    cells[i] = new Square();
    }

    By this would just display a column of 400 Squares, right? If I get my GUI to come up, that is. How do I make them a square of Squares?

    Another class in my code is Square.java, which creates cells and gets their status, dead or alive:

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

    public class Square extends JPanel {

    public JPanel square;
    public boolean status;
    public String state;

    public Square () {

    square = new JPanel();
    setBackground(Color.GREEN);
    setPreferredSize(new Dimension(20, 20));
    }
    }

    public void status () {

    if () {

    status = true;
    }
    else if () {

    status = false;
    }
    }

    public String getStatus () {

    if (status) {

    state = "alive";
    }
    else {

    state = "dead";
    }
    }

    public void updateBackground () {

    if (.equals("alive")) {

    setBackground(Color.BLUE);
    }
    else if (.equals("dead")) {

    setBackground(Color.GREEN);
    }
    }
    }

    I get a lot of "class, interface, or enum expected" errors:

    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:20: class, interface, or enum expected
    public void status () {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:25: class, interface, or enum expected
    }
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:29: class, interface, or enum expected
    }
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:32: class, interface, or enum expected
    public String getStatus () {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:37: class, interface, or enum expected
    }
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:41: class, interface, or enum expected
    }
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:44: class, interface, or enum expected
    public void updateBackground () {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:49: class, interface, or enum expected
    }
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:53: class, interface, or enum expected
    }
    ^
    9 errors

    I figure some of it is because the code isn't done, but I don't actually know what the problem specifically is.

    I know this is a lot. Thanks for any help.

    I cannot post links, but this question is also posted at Sun's forum page and Yahoo! Answers.
    Last edited by doozer8688; 11-04-2008 at 07:08 PM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    illegal start of expression
    Check that your {} and () and [] are paired.

  3. #3
    doozer8688 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks. I thought I'd found the problem (I ended the Square class way early), but now I'm getting all new errors. I checked and double checked and triple checked the brackets and I don't understand why I'm getting all these errors:

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

    public class Square extends JPanel {

    public JPanel square;
    public boolean status;
    public String state;

    public Square () {

    square = new JPanel();
    setBackground(Color.GREEN);
    setPreferredSize(new Dimension(20, 20));
    }

    public void status () {

    if () {

    status = true;
    }
    else if () {

    status = false;
    }
    }

    public String getStatus () {

    if (status) {

    state = "alive";
    }
    else {

    state = "dead";
    }
    }

    public void updateBackground () {

    if (.equals("alive")) {

    setBackground(Color.BLUE);
    }
    else if (.equals("dead")) {

    setBackground(Color.GREEN);
    }
    }
    }

    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:21: illegal start of expression
    if () {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:25: illegal start of expression
    else if () {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:45: illegal start of expression
    if (.equals("alive")) {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:45: ';' expected
    if (.equals("alive")) {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:49: 'else' without 'if'
    else if (.equals("dead")) {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:49: invalid method declaration; return type required
    else if (.equals("dead")) {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:49: illegal start of type
    else if (.equals("dead")) {
    ^
    C:\Users\Fitrah\Documents\School\CS 1331\hw8\Square.java:54: class, interface, or enum expected
    }

    Why is it expecting a ';' at the end of the if ()?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    If you feel you must cross-post the same question in multiple forums, the polite thing to do would be to add links from one to the other. Unless you like to frustrate any of the volunteers who have tried to help you.

  5. #5
    doozer8688 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    I apologize. I've never cross-posted before; it didn't occur to me. I don't mean to frustrated anyone.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,619
    Rep Power
    23

    Default

    if () {
    need a boolean expression in the ()s
    else if (.equals("dead")) {
    need an object before the .

    You need to look at some examples of working code before you do any more coding. Your wasting time typing in this kind of code.

  7. #7
    doozer8688 is offline Member
    Join Date
    Nov 2008
    Posts
    4
    Rep Power
    0

    Default

    Thanks. Yeah, the code obviously very incomplete. I wasn't sure if all the errors were from the components I didn't fill in yet or something else. Thanks for clearing it up.

Similar Threads

  1. Problems with this Java Program...
    By Bangtajra123 in forum Java Applets
    Replies: 6
    Last Post: 09-30-2008, 10:04 PM
  2. JPanel / layout problems
    By Warhorsei in forum AWT / Swing
    Replies: 4
    Last Post: 06-04-2008, 05:26 AM
  3. Inventory part 3 program problems
    By badness in forum New To Java
    Replies: 1
    Last Post: 12-17-2007, 07:00 AM
  4. Problems while loading a JPanel to JApplet...
    By Ananth Chellathurai in forum Java Applets
    Replies: 0
    Last Post: 11-24-2007, 10:47 AM
  5. JPanel Problems
    By Riftwalker in forum AWT / Swing
    Replies: 6
    Last Post: 10-15-2007, 11:16 PM

Posting Permissions

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