Reputation: 21
I have been working on this project for a long time off and on, and just came back to work on it after leaving it for a month. The last time I worked on it, everything worked fine and I was cleaning stuff up for it to be finished. Today when I opened eclipse there were numerous errors with no solutions, most of them relating to brackets, but a few of them relating to the actionPerformed method. I pointed out which lines the errors are on if that helps. Just wondering if anyone could tell me whether there are real errors or there is something wrong with eclipse.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.util.Random;
import javax.swing.*;
public class main extends JFrame implements ActionListener{
JButton rock;
JButton scissors;
JButton paper;
JLabel left;
ImageIcon rock1;
ImageIcon scissors1;
ImageIcon scissors2;
ImageIcon paper2;
ImageIcon paper1;
ImageIcon rock2;
ImageIcon back;
ImageIcon scissorsb;
ImageIcon youWin;
ImageIcon youLose;
ImageIcon Loading;
JLabel background;
JLabel right;
String [] Rock3 = {};
String [] RPS = {"Rock","Paper","Scissors"};
Random rand = new Random ();
Random Prize = new Random ();
int num = rand.nextInt(RPS.length);
int yourChoice = -1;
JLabel winner;
Container contentPane = this.getContentPane();
SpringLayout layout = new SpringLayout();
String Tie = "Tie";
String compwins = "Computer wins!";
String youwin = "You win!";
Font winnerFont = new Font("Font", Font.PLAIN, 35);
int yourScore = 0;
int compScore = 0;
String yourNum = Integer.toString(yourScore);
String compNum = Integer.toString(compScore);
JLabel yourPoints = new JLabel();
JLabel compPoints;
private Timer t;
private Timer t2;
private Timer t3;
public main (){
super("Rock Paper Scissors");
back = new ImageIcon("space-wallpaper-space-32897775-1920-1080.jpg");
youWin = new ImageIcon("you win.png");
youLose = new ImageIcon("you lose.png");
paper1 = new ImageIcon("paper1.png");
paper2 = new ImageIcon("paper2.png");
scissors1 = new ImageIcon("scissors1.png");
scissors2 = new ImageIcon("scissors2.png");
rock1 = new ImageIcon("rock1.png");
rock2 = new ImageIcon("rock2.png");
Loading = new ImageIcon("loadingbig.png");
background = new JLabel();
left = new JLabel();
right = new JLabel();
winner = new JLabel();
compPoints = new JLabel();
yourPoints = new JLabel();
right.setVisible(true);
this.setVisible(true);
this.setSize(1920,1080);
winner.setFont(winnerFont);
winner.setHorizontalAlignment( SwingConstants.CENTER );
contentPane.add(compPoints);
contentPane.add(yourPoints);
contentPane.setLayout(layout);
contentPane.add(winner);
contentPane.add(left);
contentPane.add(right);
rock = new JButton("Rock");
paper = new JButton ("Paper");
scissors = new JButton ("Scissors");
layout.putConstraint(SpringLayout.WEST, rock,
500,
SpringLayout.WEST, contentPane);
this.add(rock, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, rock,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, rock,
40,
SpringLayout.NORTH, contentPane);
this.add(paper, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, paper,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, paper,
200,
SpringLayout.NORTH, contentPane);
this.add(scissors, BorderLayout.NORTH);
layout.putConstraint(SpringLayout.SOUTH, scissors,
-30,
SpringLayout.SOUTH, contentPane);
layout.putConstraint(SpringLayout.WEST, scissors,
360,
SpringLayout.NORTH, contentPane);
rock.setBackground(Color.GRAY);
rock.setPreferredSize(new Dimension(140,50));
paper.setBackground(Color.WHITE);
paper.setPreferredSize(new Dimension(140,50));
scissors.setBackground(Color.LIGHT_GRAY);
scissors.setPreferredSize(new Dimension(140,50));
rock.repaint();
scissors.repaint();
paper.repaint();
Background();
}
public static void main(String args[]){
main framer = new main();
}
@Override
/*Error Here*/public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
if(arg0.getSource() ==rock){
left.setIcon(rock1);
yourChoice = 0;
}else if(arg0.getSource() ==paper){
left.setIcon(paper1);
yourChoice = 1;
}else if(arg0.getSource() ==scissors){
left.setIcon(scissors1);
yourChoice = 2;
}
computerMove();
Score();
winner();
}
public void Background(){
rock.setVisible(false);
scissors.setVisible(false);
paper.setVisible(false);
contentPane.add(background);
background.setIcon(Loading);
t = new Timer(4000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
background.setIcon(back);
rock.setVisible(true);
scissors.setVisible(true);
paper.setVisible(true);
}
});
t.setRepeats(false);
t.start();
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
rock.doClick();
paper.doClick();
scissors.doClick();
rock.addActionListener(this);
paper.addActionListener(this);
scissors.addActionListener(this);
}
public void Score(){
yourPoints.setFont(winnerFont);
compPoints.setFont(winnerFont);
layout.putConstraint(SpringLayout.WEST, yourPoints,
100,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, yourPoints,
800,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, compPoints,
1790,
SpringLayout.WEST, contentPane);
layout.putConstraint(SpringLayout.NORTH, compPoints,
800,
SpringLayout.NORTH, contentPane);
if ((num==0 && yourChoice ==0)||(num==2 && yourChoice ==2)||(num==1 && yourChoice ==1)){
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
890,
SpringLayout.WEST, contentPane);
winner.setText(Tie);
winner.repaint();
t = new Timer(1400, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
winner.setText(null);
}
});
t.setRepeats(false);
t.start();
}else if((num==0 && yourChoice ==2)||(num==2 && yourChoice ==1)||(num==1 && yourChoice ==0)){
compScore ++;
compPoints.setText(compNum);
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
810,
SpringLayout.WEST, contentPane);
winner.setText(compwins);
winner.repaint();
compPoints.repaint();
/*Error Here*/t2 = new Timer(1400, new ActionListener() {
@Override
/*Error Here*/public void actionPerformed(ActionEvent e) {
winner.setText(null);
/*Error Here*/}
/*Error Here*/});
t2.setRepeats(false);
t2.start();
/*Error Here*/}else if((num==2 && yourChoice ==0)||(num==0 && yourChoice ==1)||(num==1 && yourChoice ==2)){
yourScore ++;
yourPoints.setText(yourNum);
layout.putConstraint(SpringLayout.NORTH, winner,
50,
SpringLayout.NORTH, contentPane);
layout.putConstraint(SpringLayout.WEST, winner,
850,
SpringLayout.WEST, contentPane);
winner.setText(youwin);
winner.repaint();
yourPoints.repaint();
t3 = new Timer(1400, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
winner.setText(null);
}
});
t3.setRepeats(false);
/*Error Here*/t3.start();
/*Error Here*/}
yourNum = Integer.toString(yourScore);
compNum = Integer.toString(compScore);
yourPoints.setText(yourNum);
compPoints.setText(compNum);
winner.repaint();
yourPoints.repaint();
compPoints.repaint();
winner.setForeground(Color.RED);
yourPoints.setForeground(Color.RED);
compPoints.setForeground(Color.RED);
/*Error Here*/}
public void winner(){
if(yourScore == 10){
background.setIcon(youWin);
contentPane.add(background);
background.repaint();
left.setVisible(false);
right.setVisible(false);
yourPoints.setVisible(false);
compPoints.setVisible(false);
winner.setVisible(false);
rock.setVisible(false);
paper.setVisible(false);
scissors.setVisible(false);
t = new Timer(3000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
computerMove();
Score();
winner();
}
});
t.setRepeats(false);
t.start();
}else if(compScore == 10){
background.setIcon(youLose);
contentPane.add(background);
background.repaint();
left.setVisible(false);
right.setVisible(false);
yourPoints.setVisible(false);
compPoints.setVisible(false);
winner.setVisible(false);
rock.setVisible(false);
paper.setVisible(false);
scissors.setVisible(false);
/*Error Here*/t = new Timer(3000, new ActionListener() {
@Override
/*Error Here*/public void actionPerformed(ActionEvent e) {
computerMove();
Score();
winner();
}
/*Error Here*/});
t.setRepeats(false);
t.start();
}
}
public void computerMove(){
num = rand.nextInt(RPS.length);
if (num==0){
right.setIcon(rock2);
}else if(num==1){
right.setIcon(paper2);
}else if(num==2){
right.setIcon(scissors2);
}
/*Error Here*/}
Upvotes: 1
Views: 291
Reputation: 21223
Looks like you're missing a closing bracket }
at the end of the file.
Pretty bad indentation and naming makes it hard to notice. Take a look at Code Conventions for the Java Programming Language.
Upvotes: 1
Reputation: 6718
You're missing the closing brace for computerMove
.
When solving problems like this, try commenting out methods until you get something that compiles, then uncomment small blocks of code until you find the offending block.
Upvotes: 1
Reputation: 117597
Whenever you're facing weird issues with eclipse, try to do the following:
Project
--> Clean...
.Project
--> Open Project
/Close Project
.Upvotes: 1