Image from file on simple java application

How to Put images for the following code...

Page 1 of 1

4 Replies - 2431 Views - Last Post: 26 August 2009 - 05:56 AM Rate Topic: -----

#1 dmmh_22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-August 09

Image from file on simple java application

Post icon  Posted 25 August 2009 - 10:10 AM

Hi.! uhm i just partially finished my card game project "Lucky Nine" in simple java application..

The results of the game are just printed in System.out.println, and i cant manage to turn it into just JOptionPane..

uhm, anyways, i need you guys to help me how to load images from file in just a simple java application, i havnt learned about jframes etc and my code is just ready for a simple java application..

can you help guys? the images is just all i wanted...

hope you still help me how to put for this code...

heres my code...

import javax.swing.JOptionPane;

public class LuckyNine {
	

	public static void main(String[] args) {
	int rc = 0;  // The return code from the showConfirmDialog
do
	  {  
	  playGame();
		 rc = JOptionPane.showConfirmDialog( null, "Play again?",
			"Question", JOptionPane.YES_NO_OPTION );
	  } while ( rc == JOptionPane.YES_OPTION );
   }
 
 private static void playGame ()
   {
  int pera = 0;  
  int rc = 0;  // The return code from the showConfirmDialog
	int deal = 2;	 //dealing of cards
	int betplayer;
	int money = 1000;
	int sum = 0;
	String dealt; //string of dealing card
	String bet;

	//String[] lead = new String[dealer];
	String name;
	String[] suit = { "Clubs", "Diamonds", "Hearts", "Spades" };//card suits
	String[] displayCard = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };  //52 cards to display 
	int DECK = displayCard.length * suit.length; //array length of displaycard and suit


	name = JOptionPane.showInputDialog("Enter name of player: ");
 	bet = JOptionPane.showInputDialog( "Enter bet of " + name + ": " );
	betplayer = Integer.parseInt(bet);
  
	
	int[] valueOfCard = new int[DECK];  
	String[] shuffle = new String[DECK]; /*method of combining & shuffling suit and displaycard */

	for (int i = 0; i < displayCard.length; i++){ 
		for (int j = 0; j < suit.length; j++){
	  
			//method of the printing the cards
			shuffle[suit.length * i + j] = displayCard[i] + " of " + suit[j]; 

			if (displayCard[i]=="Jack")
				valueOfCard[suit.length * i + j]=0;
			else if (displayCard[i]=="Queen")
				valueOfCard[suit.length * i + j]=0;
			else if (displayCard[i]=="King")
				valueOfCard[suit.length * i + j]=0;
			else if (displayCard[i]=="Ace")
				valueOfCard[suit.length * i + j]=1;
			else if (displayCard[i]=="10")
				valueOfCard[suit.length * i + j]=0;
			else valueOfCard[suit.length * i + j]=Integer.parseInt(displayCard[i]);  
		}
	}
	
	//swapping and shuffling of cards
	for (int i = 0; i < DECK; i++){
		int index1 = i + (int) (Math.random() * (DECK - i));

		/*
		if (i!=0)
			{
			for (int x = 0; x < 
			}
		*/
		
		String temp = shuffle[index1];
		shuffle[index1] = shuffle[i];
		shuffle[i] = temp;

		int temp2 = valueOfCard[index1];
		valueOfCard[index1] = valueOfCard[i];
		valueOfCard[i] = temp2;

	}
	
	int counter = 0;
	int count = 0;
	int winner = -1;

	System.out.println( "\n" + "Cards of " + name+ "\n" );
	for ( int y = 0; y < deal; y++, counter++){		   
			System.out.print(shuffle[counter]);  
			System.out.println();
		 sum+= valueOfCard[counter];
		}

	System.out.println( "\nSum of " + name + " is : " );

	if (sum > 9)
	{
	sum = sum - 10;
	}
	System.out.print( sum );
	System.out.println();

	if (sum == 9)
		{
		count++;
		winner=1;
		}
	
	sum = 0;

	System.out.println( "\n" + "cards of dealer is : " );
	for ( int y = 0; y < deal; y++, counter++){
		System.out.print(shuffle[counter]);
		System.out.println();
		sum += valueOfCard[counter];
	}
		
	System.out.println( "\n" + "Sum of dealer is : ");
 
	if (sum > 9)
		{
		sum = sum - 10;
		}	
	System.out.print( sum );
	System.out.println();

	if (sum == 9)
		{
		count++;
		winner=0;
		}

	if (count==0){		
		System.out.println( "\nNo WINNER..." );		
	
			pera = betplayer;
			System.out.println("Your money is still " + pera);
			}
	else if (count>1)
		System.out.println( "\nDRAW!!!" );
	else
		{
		if (winner==0){
			System.out.println( "Dealer WINS!! " );
			pera = money - betplayer;
			System.out.println( "Dealer's money is : " + pera );	   
			}
		else
			{
			System.out.println( name + " WINS!! " );
			pera = money + betplayer;
			System.out.println( "Your money is : " + pera );
			}
		}

}

 
}



Is This A Good Question/Topic? 0
  • +

Replies To: Image from file on simple java application

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Image from file on simple java application

Posted 25 August 2009 - 12:15 PM

Do you want to know how to show the picture using the JFrame or do you want it done some other way?
Was This Post Helpful? 0
  • +
  • -

#3 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Image from file on simple java application

Posted 25 August 2009 - 12:27 PM

You can use BufferedImage to store the image. You can make use of the following lines,

File F = new File(ImageFileName);
BufferedImage img = new BufferedImage();
img = ImageIO.read(F)
Graphics2d g;
g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, observer);



Let me know if you need any help to put them in context.
Was This Post Helpful? 0
  • +
  • -

#4 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8325
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: Image from file on simple java application

Posted 25 August 2009 - 04:09 PM

or make an ImageIcon from the image file

ImageIcon img = new ImageIcon("abc.jpeg");

then put it in a JLabel

JLabel imgLabel = new JLabel(img);

you can then put this JLabel into any GUI
Was This Post Helpful? 0
  • +
  • -

#5 dmmh_22  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 13-August 09

Re: Image from file on simple java application

Posted 26 August 2009 - 05:56 AM

Quote

Let me know if you need any help to put them in context.


can you help me how to put it in my code? i need it really badly...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1