I have this line of code (repeated with a bit of change)

if (ropeChosen == 1){
				g.drawImage(rope, 321, 0, 10, 505, this);
				g.drawImage(rope, 521, 0, 10, 704, this);
				g.drawImage(rope, 721, 0, 10, 703, this);
				g.drawImage(rope, 921, 0, 10, 702, this);
				if (p.idol2){
					g.drawImage (idol, 275,485,100,100,this);
				}else{
					g.drawImage (snake, 300,505,50,100,this);
				}
			}

When a player choses a rope, and it is correct (they get the idol) it displays the rope pulled up, and the idol on the end, however if they chose the wrong one it should print a snake on the end of the pulled up rope. How come the ropes disappear when they pull up the snake, and not when they pull up the idol?

Also if I swap the idol, and the snake it displays the snake, and the ropes dissapear when they pull the idol. Why is this?

Thanks for any help.

P.S. All of the code is in the paintComponent method.

The if statements seem simple enough.
There are 4 calls to drawImage and then a choice of one of two calls to drawImage.
Which of the drawImages is not doing what you expect? 1-4 or 5/6??

The ropes draw originally (fully extended), if they pull up the correct rope, then the rope gets pulled up and the idol is on the bottom, however if they pull up the wrong rope instead of the rope being pulled up with a snake on the end, the ropes disappear.

So it is like it only executes this code

g.drawImage(rope, 321, 0, 10, 505, this);

				g.drawImage(rope, 521, 0, 10, 704, this);

				g.drawImage(rope, 721, 0, 10, 703, this);

				g.drawImage(rope, 921, 0, 10, 702, this);

if this is true

if (p.idol2){

					g.drawImage (idol, 275,485,100,100,this);

				}

otherwise it ignores the whole thing.

Here is my whole code for that class

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Game2 extends JPanel implements ActionListener{

	Frame f;
	Player p;
	Image background, arrow, idol, rope, snake;
	int rand;
	Timer next = new Timer (3000,this);
	int ropeChosen;
	
	public Game2(Frame frame, Player player) {
		// TODO Auto-generated constructor stub
		f = frame;
		p = player;
		rand = (int) Math.ceil(Math.random() * 4);
		try {
			background = ImageIO.read(new File ("Images/map_two.png"));
			arrow = ImageIO.read(new File ("Images/arrow.png"));
			idol = ImageIO.read(new File ("Images/sun.png"));
			rope = ImageIO.read(new File ("Images/rope.png"));
			snake = ImageIO.read (new File ("Images/snake.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		repaintMethod();
	}
	public void repaintMethod() {
		this.setBackground(Color.black);
		p.handX = 500;
		p.handY = 660;
		repaint();
	}

	public void paintComponent (Graphics g){
		super.paintComponent(g);
		g.drawImage(background, 0, 0, getWidth(), getHeight(), this);
		g.setColor (Color.yellow);
		g.drawString("" + rand, 10, 10);
		if (next.isRunning()){
			if (ropeChosen == 1){
				g.drawImage(rope, 321, 0, 10, 505, this);
				g.drawImage(rope, 521, 0, 10, 704, this);
				g.drawImage(rope, 721, 0, 10, 703, this);
				g.drawImage(rope, 921, 0, 10, 702, this);
				if (p.idol2){
					g.drawImage (idol, 275,485,100,100,this);
				}else{
					g.drawImage (snake, 300,505,50,100,this);
				}
			}
			else if (ropeChosen == 2){
				g.drawImage(rope, 321, 0, 10, 705, this);
				g.drawImage(rope, 521, 0, 10, 504, this);
				g.drawImage(rope, 721, 0, 10, 703, this);
				g.drawImage(rope, 921, 0, 10, 702, this);
				if (p.idol2){
					g.drawImage (idol, 475,485,100,100,this);
				}else{
					g.drawImage (snake, 500,500,50,100,this);
				}
			}
			else if (ropeChosen == 3){
				g.drawImage(rope, 321, 0, 10, 705, this);
				g.drawImage(rope, 521, 0, 10, 704, this);
				g.drawImage(rope, 721, 0, 10, 503, this);
				g.drawImage(rope, 921, 0, 10, 702, this);
				if (p.idol2){
					g.drawImage (idol, 675,485,100,100,this);
				}else{
					g.drawImage (snake, 700,505,50,100,this);
				}
			}
			else if (ropeChosen == 4){
				g.drawImage(rope, 321, 0, 10, 705, this);
				g.drawImage(rope, 521, 0, 10, 704, this);
				g.drawImage(rope, 721, 0, 10, 703, this);
				g.drawImage(rope, 921, 0, 10, 502, this);
				if (p.idol2){
					g.drawImage (idol, 875,485,100,100,this);
				}else{
					g.drawImage (snake, 900,500,50,100,this);
				}
			}
		}
		else {
			g.drawImage(rope, 321, 0, 10, 705, this);
			g.drawImage(rope, 521, 0, 10, 704, this);
			g.drawImage(rope, 721, 0, 10, 703, this);
			g.drawImage(rope, 921, 0, 10, 702, this);
		}
		
		
		if (!next.isRunning()){
			g.drawImage(arrow, p.handX, p.handY, 50, 50, this);
		}
		
	}
	
	public void moveLeft() {
		// TODO Auto-generated method stub
		if (p.handX > 300){
			p.handX -= 200;
			repaint();
		}
	}

	public void moveRight() {
		// TODO Auto-generated method stub
		if (p.handX < 900){
			p.handX += 200;
			repaint();
		}
	}

	public void select() {
		// TODO Auto-generated method stub
		if (p.handX == 300 && rand == 1){
			ropeChosen = 1;
			p.idol2 = true;
		}
		else if (p.handX == 500 && rand == 2){
			ropeChosen = 2;
			p.idol2 = true;
		}
		else if (p.handX == 700 && rand == 3){
			ropeChosen = 3;
			p.idol2 = true;
		}
		else if (p.handX == 900 && rand == 4){
			ropeChosen = 4;
			p.idol2 = true;
		}
		next.start();
		repaint();
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}

}

Is the problem with your if logic
or is the problem with how you are drawing the images?

The if logic seems so simple I don't understand how there could be a problem with it.

otherwise it ignores the whole thing.

Yes that is exactly what this code will do:
if (ropeChosen == 1){

no I mean if the player does not have the idol, and the next timer is running then it ignores the drawing ropes, and the drawing the snake on the end. When it should have drawn the ropes, and then go into the else and draw the snake.

Can you discuss your problem in terms of the program code and not in terms of what the user sees? There are 6 drawImage statements. Statements 1-4 are always executed and then either #5 or #6.
What happens if 1-4 and 5 are executed? Is the output OK?
What happens if 1-4 and 6 are executed? Is the output OK?

If the output is not OK, please explain what is wrong with it?
Does the last drawImage overwrite the some of the first 4 drawImage statements?

What is the value of the ropeChosen variable when the code does work?

Lines1-4 execute only when line 5 will be executed, but when line 6 would be executed lines 1-4 are not drawn. Like you said 1-4 should have been drawn

Lines1-4 execute only when line 5 will be executed

The code does not show that. It shows that 1-4 should ALWAYS be executed.
Then either 5 or 6.

I do not see how the code can skip over executing 1-4.
Add some println to the code to show what lines are being executed.

Can you make a small simple program (SSSCCE) that will execute and show the problem?

new Code

if (ropeChosen == 1){
				g.drawImage(rope, 321, 0, 10, 505, this);
				g.drawImage(rope, 521, 0, 10, 704, this);
				g.drawImage(rope, 721, 0, 10, 703, this);
				g.drawImage(rope, 921, 0, 10, 702, this);
				System.out.println ("Ropes Done");
				if (p.idol2){
					g.drawImage (idol, 275,485,100,100,this);
					System.out.println ("Idol");
				}else if (!p.idol2){
					g.drawImage (snake, 300,505,50,100,this);
					System.out.println ("Snake");
				}
			}

output when Idol rope picked

Left
You Lost
NextArea // from previous stage
Left
Ropes Done
Idol

output when rope one picked with no idol

Left
You Lost // from previous stage
NextArea
Left

Your print out does not show the values of next.isRunning() and ropeChosen?
Their values also control what is drawn.

I fixed it. I didn't set which rope they chose if they got it wrong when they clicked space. Thanks for the help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.