Results 1 to 6 of 6
  1. #1
    teamzerosc is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default I need help with implementing an image in to my applet

    Hi,

    I would be extremely grateful if someone can help me with my problem. I have a game that I made for a class project. The game is a guessing game where you click on 1 box in a grid of gray boxes and a color appears. You then click again and try to match it to another color, but if it does not match than the grid resets. If you match them than the colors stay.

    I want to have images instead of the gray boxes. I currently have two classes for this applet.

    Java Code:
    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;
    public class GameCube extends Applet implements ActionListener, MouseListener
    {
    	
    	Box [][]gamebox = new Box [4][4];
    	Box boxChoice;
    	int x,y;
    	int col, row;
        int rint = (int)(Math.random() * 8); 
        int xStart = 0;
        int yStart = 0;
        int boxHeight = 100;
        int boxWidth = 100;
        int count = 0;
        Graphics g;  
        Color colors[]= {Color.blue, Color.red, Color.cyan, 
        		Color.green, Color.magenta, Color.orange, Color.pink, Color.yellow};
        Box boxP[]= new Box[3];
    
    
    	
    	
    		public void init() {
    			Button newGame = new Button("New Game");
    			setLayout(new BorderLayout());
    			add(newGame, BorderLayout.SOUTH);
    			newGame.addActionListener(this);
    			
    			addMouseListener(this);
    			setBackground(Color.white);
    			
    			setNewGame(); 
    		}
    		
    	
    	public void setNewGame() 
    	{
    		 
    		int chosenColors[] = {0,0,0,0,0,0,0,0}; 
    		for(int col = 0; col <=3; col++){
    			System.out.println("my col value is: "+col);
    			for(int row = 0; row < 4; row++){ 
    				System.out.println("my row value is: " + row);
    				int y1 = yStart + row * boxHeight;
    				rint = (int)(Math.random() * 8); 
    				while (chosenColors[rint] >= 2) 
    					{rint = (int)(Math.random() * 8);} 
    				chosenColors[rint] = chosenColors[rint] +1;
    				int x1 = xStart + col * boxWidth;
    				gamebox[row][col] = new Box(x1, y1, boxHeight, boxWidth, colors[rint]);
    				System.out.println("coordinates: row " + row + " column " + col);
    				}
    			
    		}
    		repaint();
    	}
    	public void paint(Graphics g){
    		displayGame(g);
    	
    		}
    		
    		public void displayGame(Graphics g){
    		
    			for (int i = 0; i <= 3; i++) {
    				for (int j = 0; j<= 3; j++){
    				gamebox[i][j].display(g);
    				} }
    				for (int row = 0; row <= 4; row++) {
    					g.setColor(Color.black);
    						g.fillRect(row * boxWidth + 0, yStart, 1, 400); 
    						}
    		  			 for (int col = 0; col <= 4; col++) {
    		  				 g.setColor(Color.black);
    						g.fillRect(xStart, col * boxHeight + 0, 400, 1);   
    		   				}
    				
    			
    		}
    
    		
    		public void playGame(Box newestChosen) 
    		{ 
    		if (count==0){	
    		boxP[0]=boxChoice;
    		boxP[0].showColor=true;
    		}
    
    	else if (count==1){	
    		boxP[1]=boxChoice;
    		boxP[1].showColor=true;
    		}
    	else{
    		boxP[2]=boxChoice;
    			if (boxP[0].boxColor==boxP[1].boxColor){
    				boxP[0].showColor=true;
    				boxP[2].showColor=true;
    				}
    	else{
    	boxP[0].showColor=false;
    	boxP[1].showColor=false;
    	}
    	boxP[0]=boxP[2];
    	}
    System.out.println("boxP["+ count +"]");	
    		} 
    		
    		public void actionPerformed(ActionEvent event)
    		{
    			setNewGame();
    		}
    
    		public void mouseClicked (MouseEvent e)
    		{
    		}
    		public void mouseEntered(MouseEvent e)
    		{
    		}
    		public void mouseExited(MouseEvent e)
    		{
    		}
    		public void mousePressed(MouseEvent e)
    		{
    		}
    		public void mouseReleased(MouseEvent e)
    		{
    			x=e.getX();
    			y=e.getY();
    			System.out.println("the coordinates clicked are x " + x + "and y" + y);
    		
    		selectBox(x,y);
    		boxChoice.chosen();
    		playGame(boxChoice);
    		if (count==2)
    			count =1;
    		else 
    			count++;			
    		repaint();
    		}
    		public void selectBox(int x, int y) {
    		int locI = (y - xStart)/boxHeight;
    		int locJ = (x - yStart)/boxWidth;
    		boxChoice = gamebox[locI][locJ];
    		
    		}
    }
    and
    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class Box {
    	    int x;
    	    int y;
    	    int Height;
    	    int Width;
    	    Color boxColor;
    	    boolean showColor = false;
    	    Graphics g;
    	   
    	    
    	    public Box(int X1,int Y1,int h ,int w, Color myColor){
    	    x = X1;
    	    y = Y1;
    	    Height = h;
    	    Width = w;
    	    boxColor = myColor;
    	    }
    		public void display (Graphics graph){
    		g = graph;
    		if (showColor == false) {
    			g.setColor(Color.darkGray); //gray boxes
    			g.fillRect(x, y, Height, Width);}
    		else  {
    			g.setColor(boxColor);
    			g.fillRect (x, y, Height, Width);}
    			
    
    
    		}
    		public void chosen()
    		{
    		showColor =true;	
    		}
    	}
    The gray boxes are in the above class. I need help asap because this project is due on Monday and it is vital to my grade. I am done, but I just wanted to add images instead.


    Thanks,

    CR

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

    Also I noticed your reply to a similar post in another forum was less than tactful and in fact was down-right ugly:
    Java 2D - How do I put an image in to my applet?

    This may not help you much either. Best of luck and hoping your replies here will be better and will conform to accepted forum standards.
    Last edited by Fubarable; 12-14-2008 at 10:49 AM.

  3. #3
    teamzerosc is offline Member
    Join Date
    Dec 2008
    Posts
    2
    Rep Power
    0

    Default

    Quote Originally Posted by Fubarable View Post
    To the original poster, cross-posting can frustrate anyone who tries to help you only to find out later that the same answer was given hours ago in a cross-posted thread. No one likes wasting their time, especially a volunteer. The polite thing to do would be to not do this, but if you feel that you absolutely must, to at least provide links in both cross-posts to each other.

    Also I noticed your reply to a similar post in another forum was less than tactful and in fact was down-right ugly:
    *link* Java 2D - How do I put an image in to my applet?[/url]

    This may not help you much either. Best of luck and hoping your replies here will be better and will conform to accepted forum standards.
    Lulz, my bad I am used to the "slums" of forums. The guy that I was rude to didn't even post with any relative information. He didn't explain what cross posting was and I got annoyed with him.

    It doesn't matter to me. You posted the other forum I asked the question on, so you can monitor if I got any assistance. If anyone wants to help me, go for it. I will still be grateful.

  4. #4
    neilcoffey is offline Senior Member
    Join Date
    Nov 2008
    Posts
    286
    Rep Power
    6

    Default

    There are different image APIs in Java, but one usually convenient way is:
    - Look at the ImageIO class to turn your image file/bytes into a BufferedImage
    - In your draw method: case the Graphics to a Graphics2D, and use the drawImage() method that takes a BufferedImage (don't worry about the 'BufferedImageOp' -- just pass it null)

  5. #5
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    8

    Default

    I ran the code you posted and got this:
    Java Code:
    C:\jexp>appletviewer GC.java
    my col value is: 0
    my row value is: 0
    coordinates: row 0 column 0
    my row value is: 1
    coordinates: row 1 column 0
    my row value is: 2
    java.lang.ArrayIndexOutOfBoundsException: 6
            at GC.setNewGame(gc.java:59)
            at GC.init(gc.java:35)
            at sun.applet.AppletPanel.run(AppletPanel.java:419)
            at java.lang.Thread.run(Thread.java:619)
    Line 59 is
    Java Code:
                    gamebox[row][col] = new GCBox(x1, y1, boxHeight,
                                                  boxWidth, colors[rint]);

  6. #6
    zakaralli is offline Member
    Join Date
    Feb 2009
    Posts
    1
    Rep Power
    0

    Default Hi All,

    I am learning Java since a one year from now. I wonder if i can find what i am looking for here, its look great to get all this java topics in one place.

Similar Threads

  1. Background Image of Applet
    By BangJava in forum Java Applets
    Replies: 8
    Last Post: 11-24-2010, 05:48 AM
  2. loading image into applet
    By balaram in forum Java Applets
    Replies: 1
    Last Post: 11-06-2008, 02:13 PM
  3. problem getting the image(APPLET)
    By kof20012 in forum Java Applets
    Replies: 10
    Last Post: 09-11-2008, 06:22 PM
  4. Image upload applet 2.0
    By jfileupload in forum Java Software
    Replies: 0
    Last Post: 03-24-2008, 04:15 PM
  5. how to place an image in an applet
    By paty in forum Java Applets
    Replies: 2
    Last Post: 08-04-2007, 05:15 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •