Question: For java: I am trying to create a animated game of rock paper scissors where whenever the user inputs rock, paper or scissors the image
For java:
I am trying to create a animated game of rock paper scissors where whenever the user inputs rock, paper or scissors the image of that object will move from left to right on the screen and the computer generated object will move from the right to the left and they both will stop at the middle by using the timer.
It only shows the last position where they both are in the middle. How can I show the animation of them moving? This is my code so far.
Main class:
import javax.swing.*; public class Main {
public static JFrame frame;
public static void main(String[] args) { frame = new JFrame ("Rock Paper Scissors"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); RPSPanel panel = new RPSPanel(); frame.getContentPane().add(panel); frame.pack(); frame.setVisible(true); }
}
RPSPanel class:
import java.util.*; import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.Timer;
public class RPSPanel extends JPanel { private final int width = 1000,height=1000; private final int delay=150; private static ImageIcon lr,lp,ls,rr,rp,rs;
private Timer timer; private int x,y,movex,movex2; private int x2,y2; public static Random r; public static int input,n; public static int again; public RPSPanel() { lr = new ImageIcon("leftrock.jpg"); lp = new ImageIcon("leftpaper.jpg"); ls = new ImageIcon("leftscissor.jpg"); rr = new ImageIcon("rightrock.jpg"); rp = new ImageIcon("rightpaper.jpg"); rs = new ImageIcon("rightscissor.jpg"); timer = new Timer (delay, new RPSListener()); x=0; y=500; x2= 500; y2=500;
movex=250; movex2=750; setPreferredSize (new Dimension(width, height)); setBackground (Color.white); timer.start(); }
public void paintComponent (Graphics page) { super.paintComponent (page); if (input==1&& n==1){ rr.paintIcon (this, page, x, y); lr.paintIcon (this, page, x2, y2); } if (input==1&& n==2){ rr.paintIcon (this, page, x, y); lp.paintIcon (this, page, x2, y2); } if (input==1&& n==3){ rr.paintIcon (this, page, x, y); ls.paintIcon (this, page, x2, y2); } if (input==2&&n==1){ rp.paintIcon (this, page,x, y); lr.paintIcon (this, page, x2, y2); } if (input==2&&n==2){ rp.paintIcon (this, page,x, y); lp.paintIcon (this, page, x2, y2); } if (input==2&&n==3){ rp.paintIcon (this, page,x, y); ls.paintIcon (this, page, x2, y2); } if (input==3&&n==1){ rs.paintIcon (this, page, x, y); lr.paintIcon (this, page, x2, y2); } if (input==3&&n==2){ rs.paintIcon (this, page, x, y); lp.paintIcon (this, page, x2, y2); } if (input==3&&n==3){ rs.paintIcon (this, page, x, y); ls.paintIcon (this, page, x2, y2); } }
private class RPSListener implements ActionListener {
public void actionPerformed (ActionEvent event) { do { r = new Random(); n = r.nextInt((3-1)+1) + 1; String inputStr; inputStr=JOptionPane.showInputDialog("1. ROCK 2. PAPER 3.SCISSORs ENTER YOUR CHOICE (1,2,3)?"); input = Integer.parseInt(inputStr); if (input == 1){ if (n==1){ JOptionPane.showMessageDialog(null,"YOU HAVE ROCK, I HAVE ROCK, WE TIE!"); playAgain(); } if (n==2){ JOptionPane.showMessageDialog(null,"YOU HAVE ROCK, I HAVE PAPER, YOU LOSE!"); playAgain(); } if (n==3){ JOptionPane.showMessageDialog(null,"YOU HAVE ROCK, I HAVE SCISSORS, YOU WIN!"); playAgain(); } } if (input == 2){ if (n==1){ JOptionPane.showMessageDialog(null,"YOU HAVE PAPER, I HAVE ROCK, YOU WIN!"); playAgain(); } if (n==2){ JOptionPane.showMessageDialog(null,"YOU HAVE PAPER, I HAVE PAPER, WE TIE!"); playAgain(); } if (n==3){ JOptionPane.showMessageDialog(null,"YOU HAVE PAPER, I HAVE SCISSORS, YOU LOSE!"); playAgain(); } } if (input ==3){ if (n==1){ JOptionPane.showMessageDialog(null,"YOU HAVE SCISSORS, I HAVE ROCK, YOU LOSE!"); playAgain(); } if (n==2){ JOptionPane.showMessageDialog(null,"YOU HAVE SCISSORS, I HAVE PAPER, YOU WIN!"); playAgain(); } if (n==3){ JOptionPane.showMessageDialog(null,"YOU HAVE SCISSORS, I HAVE SCISSORS, WE TIE!"); playAgain(); } } }while (again==1); x+= movex; x2 -=movex2; timer.stop(); repaint();
} } public static int playAgain(){ String againStr; againStr = JOptionPane.showInputDialog("PLAY AGAIN? ('1' for yes, '2' for no"); again = Integer.parseInt(againStr); return again; } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
