Results 1 to 2 of 2
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default My image is not displaying

    I am trying to make a little game that moves a picture of peter griffin around. I wrote the code, but the image is not displaying on my JFrame.
    Can someone help out?
    Thanks!
    Java Code:
    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    import java.awt.Color;
    import java.awt.Component;
    import java.awt.Graphics;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int x = 20;
        public int y = 20;
        public int w = 400;
        public int z = 400;
        Random r = new Random();
        Random rand = new Random();
        int ex = r.nextInt(400);
        int why = rand.nextInt(400);
        public void ResetIt(){
        	if(x == ex && y == why){
        		x = 20;
        		y = 20;
        		repaint();
        	}
        }
        KeyListener kw = new KeyListener(){
    
    		@Override
    		public void keyTyped(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
    
    		@Override
    		public void keyPressed(KeyEvent e) {
            int k = e.getKeyCode();
            if (k == KeyEvent.VK_W){
    	    y=y-10;
    	    repaint();
            }
            if (k == KeyEvent.VK_S){
            	y = y+10;
            	repaint();
            }
            if(k == KeyEvent.VK_A){
        	    x=x-10;
        	    repaint();
            }
        	if(k == KeyEvent.VK_D) {
        	    x=x+10;
        	    repaint();
        	            }
        };
            
    		
    
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
    			
    		}
        	
        
        };
        myGame() {
            JFrame f = new JFrame("SquareMove");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kw);
            p.setBackground(Color.blue);
            
        }
     
        public void paintComponent(Graphics g) {
            g.fillRect(x, y, 50, 50);
            g.drawRect(ex, why,50,50);
            g.setColor(Color.white);
            g.drawString("Use WASD to move around", 20, 20);
            Image petergriffin;
            petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png").getImage();
    		g.drawImage(petergriffin,x,y,null);
        }
     
    
        public static void main(String[] args) {
            myGame g = new myGame();
            g.ResetIt();
        	new myGame();
        }
     
    }

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: My image is not displaying

    Try making it a Image not Image Icon
    With the right know how, anything is possible

Similar Threads

  1. Image Not Displaying
    By mDennis10 in forum New To Java
    Replies: 3
    Last Post: 02-26-2012, 02:23 AM
  2. Displaying an Image!
    By javabeginner29 in forum Java Applets
    Replies: 3
    Last Post: 02-15-2012, 01:08 PM
  3. Image not displaying
    By sandeep43 in forum New To Java
    Replies: 20
    Last Post: 07-08-2011, 01:31 AM
  4. Displaying an image
    By BariMutation in forum New To Java
    Replies: 6
    Last Post: 11-12-2010, 02:23 AM
  5. Displaying an image
    By SWEngineer in forum AWT / Swing
    Replies: 11
    Last Post: 01-28-2010, 06:48 AM

Posting Permissions

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