Coinflip program

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 994 Views - Last Post: 20 June 2019 - 04:41 AM Rate Topic: -----

#1 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Coinflip program

Posted 28 May 2019 - 03:05 PM

Hi guys, I'm working on a Coinflip program and I'm stumped at the if and else if statement section of my code. This part of the code will generate a random number (either a 1 or a 2) and then display an image of the 'Heads' or 'Tails' side of a coin respectively. I noticed that after clicking the 'Flip Coin' button, no coin image appears sometimes until two or three flips.
Can anyone shed light on where I went wrong with this if statement?

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CoinToss 
{
	public static void main(String args[])
	{
		JFrame frame = new JFrame("Coin Flip"); 
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel(); 
		
		ImageIcon imageIcon = new ImageIcon("images/heads.png");
		ImageIcon imageIcon2 = new ImageIcon("images/tails.png");
		ImageIcon frameIcon = new ImageIcon("images/coinflipIcon.png");
		JLabel label = new JLabel(); 
		
		Random randGenerator = new Random();
		
		JButton flipCoin = new JButton("Flip coin"); 
		flipCoin.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent v)
      	  	{
				int randomNum = randGenerator.nextInt(2)+1;
								
				System.out.println(randomNum);
				
				if(randomNum == 1)
				{
					label.setIcon(imageIcon);
					frame.getContentPane().add(label, BorderLayout.CENTER);
				}
				else if (randomNum == 2)
				{
					
					label.setIcon(imageIcon2);
					frame.getContentPane().add(label, BorderLayout.CENTER);
				}
      	  	}
      	  });
	
		frame.getContentPane().add(flipCoin, BorderLayout.SOUTH);
		frame.setIconImage(frameIcon.getImage());
		frame.pack();
		frame.setSize(350, 350);
		frame.setVisible(true);
		
		
		
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Coinflip program

#2 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Coinflip program

Posted 28 May 2019 - 04:03 PM

Try only adding the JLabel one time vs every time a change is made.

This post has been edited by NormR: 28 May 2019 - 04:03 PM

Was This Post Helpful? 0
  • +
  • -

#3 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 28 May 2019 - 04:10 PM

View PostNormR, on 28 May 2019 - 04:03 PM, said:

Try only adding the JLabel one time vs every time a change is made.


Like this?
Attached Image
Was This Post Helpful? 0
  • +
  • -

#4 Martyr2   User is offline

  • Programming Theoretician
  • member icon

Reputation: 5437
  • View blog
  • Posts: 14,372
  • Joined: 18-April 07

Re: Coinflip program

Posted 28 May 2019 - 04:20 PM

I think you are just suffering from a painting problem. I tried your code and it worked fine other than needing to trigger a repaint. In your original post, put frame.validate(); between the curly braces on lines 51 and 52.

    label.setIcon(imageIcon2);
    frame.getContentPane().add(label, BorderLayout.CENTER);
  } 
  
  frame.validate();
}



I believe that should fix things up.
Was This Post Helpful? 0
  • +
  • -

#5 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 28 May 2019 - 04:33 PM

View PostMartyr2, on 28 May 2019 - 04:20 PM, said:

I think you are just suffering from a painting problem. I tried your code and it worked fine other than needing to trigger a repaint. In your original post, put frame.validate(); between the curly braces on lines 51 and 52.

    label.setIcon(imageIcon2);
    frame.getContentPane().add(label, BorderLayout.CENTER);
  } 
  
  frame.validate();
}



I believe that should fix things up.



Your solution worked, thanks! I greatly appreciate it!
Was This Post Helpful? 0
  • +
  • -

#6 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: Coinflip program

Posted 28 May 2019 - 04:47 PM

You just need a little rearrangement and to set the label/icon instead of adding it
See http://technojeeves....h/coin-toss.jar
Source is in the jar. Run thus

java -jar coin-toss.jar

Was This Post Helpful? 0
  • +
  • -

#7 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 29 May 2019 - 03:32 PM

View Postg00se, on 28 May 2019 - 04:47 PM, said:

You just need a little rearrangement and to set the label/icon instead of adding it
See http://technojeeves....h/coin-toss.jar
Source is in the jar. Run thus

java -jar coin-toss.jar


Awesome thanks! Also, I've been trying to turn this program into a executable which I was able to do. The problem now is that clicking the "Flip Coin" button does not display either of the images. I believe it has something to do with the way I set up both image icons in the main method. Can anyone advise?

ImageIcon imageIcon = new ImageIcon("Images/heads.png");
ImageIcon imageIcon2 = new ImageIcon("Images/tails.png");
Was This Post Helpful? 0
  • +
  • -

#8 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Coinflip program

Posted 29 May 2019 - 04:01 PM

Quote

The problem now is that clicking the "Flip Coin" button does not display either of the images.

What changed from when the code used to work (at least some times)?

Otherwise can you post the new code so we can see what it is doing?
Was This Post Helpful? 0
  • +
  • -

#9 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 29 May 2019 - 05:14 PM

Add frame.validate(); Other than that, nothing else was changed.

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CoinToss 
{
	public static void main(String args[])
	{
		JFrame frame = new JFrame("Coin Flip"); 
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JPanel panel = new JPanel(); 
		
		ImageIcon imageIcon = new ImageIcon("Images/heads.png");
		ImageIcon imageIcon2 = new ImageIcon("Images/tails.png");
		ImageIcon frameIcon = new ImageIcon("Images/coinflipIcon.png");
		JLabel label = new JLabel(); 
		
		Random randGenerator = new Random();
		
		JButton flipCoin = new JButton("Flip coin"); 
		flipCoin.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent v)
      	  	{
				int randomNum = randGenerator.nextInt(2)+1;
								
				System.out.println(randomNum);
				
				if(randomNum == 1)
				{
					label.setIcon(imageIcon);
					frame.getContentPane().add(label, BorderLayout.CENTER);
				}
				else if (randomNum == 2)
				{
					
					label.setIcon(imageIcon2);
					frame.getContentPane().add(label, BorderLayout.CENTER);
				}
				frame.validate();
      	  	}
      	  });
	
		frame.getContentPane().add(flipCoin, BorderLayout.SOUTH);
		frame.setIconImage(frameIcon.getImage());
		frame.pack();
		frame.setSize(350, 350);
		frame.setVisible(true);
		
		
		
	}
}

Was This Post Helpful? 0
  • +
  • -

#10 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Coinflip program

Posted 29 May 2019 - 06:29 PM

If that code is not working, try what g00se and I suggested: Only add the JLabel component one time and change its icon as needed.

I don't see how adding the same component over and over again and then requesting the layout manager to relayout the components for the frame is a good solution.

I notice that the name of the images folder changed from images to Images.

This post has been edited by NormR: 29 May 2019 - 06:30 PM

Was This Post Helpful? 0
  • +
  • -

#11 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 29 May 2019 - 10:11 PM

View PostNormR, on 29 May 2019 - 06:29 PM, said:

If that code is not working, try what g00se and I suggested: Only add the JLabel component one time and change its icon as needed.

I don't see how adding the same component over and over again and then requesting the layout manager to relayout the components for the frame is a good solution.

I notice that the name of the images folder changed from images to Images.


The code I posted works fine when I compile and run it from Eclipse. When I try to create a runnable JAR executable file for this program and actually test it out by clicking the 'Flip Coin' button, none of the images show.
Was This Post Helpful? 0
  • +
  • -

#12 NormR   User is online

  • D.I.C Lover
  • member icon

Reputation: 780
  • View blog
  • Posts: 5,858
  • Joined: 25-December 13

Re: Coinflip program

Posted 30 May 2019 - 03:55 AM

Quote

create a runnable JAR executable file

Where are the image files when you try to run the program from the jar file? Are they in the same folder with the jar file?
The java program will look in the current folder for the image files.
If you want to put the image files in the jar file, then the program needs to treat them as resources and use a get resource method to read them.

This post has been edited by NormR: 30 May 2019 - 03:56 AM

Was This Post Helpful? 0
  • +
  • -

#13 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 30 May 2019 - 01:32 PM

View PostNormR, on 30 May 2019 - 03:55 AM, said:

Quote

create a runnable JAR executable file

Where are the image files when you try to run the program from the jar file? Are they in the same folder with the jar file?
The java program will look in the current folder for the image files.
If you want to put the image files in the jar file, then the program needs to treat them as resources and use a get resource method to read them.


Looks like I was able to resolve the issue by replacing
ImageIcon imageIcon = new ImageIcon("Images/heads.png");
with
ImageIcon imageIcon = new ImageIcon(getClass().getClassLoader().getResource("heads.png"));


Thank you guys for the help!
Was This Post Helpful? 0
  • +
  • -

#14 g00se   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3639
  • View blog
  • Posts: 16,718
  • Joined: 20-September 08

Re: Coinflip program

Posted 30 May 2019 - 03:03 PM

Quote

When I try to create a runnable JAR executable file for this program and actually test it out by clicking the 'Flip Coin' button, none of the images show.


So why didn't you run the jar (with source) to which i posted a link?
Was This Post Helpful? 0
  • +
  • -

#15 ik3   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 28-May 19

Re: Coinflip program

Posted 19 June 2019 - 09:29 AM

View Postg00se, on 30 May 2019 - 03:03 PM, said:

Quote

When I try to create a runnable JAR executable file for this program and actually test it out by clicking the 'Flip Coin' button, none of the images show.


So why didn't you run the jar (with source) to which i posted a link?


I ran it but wasn't sure how to get to the source code, my fault. This program works exactly how I want except I'm having an issue making the imageicon background for these images transparent. I tried making these images have transparent backgrounds using GIMP but when I try to add the images to the src folder and run the program, the images no longer display. Any suggestions?

Attached Image
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2