Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Why isn't my program showing the image :(?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    20
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Why isn't my program showing the image :(?

    I couldn't really figure out the easiest way to display an image. I tried the following way (in my code below), and it runs with no error but does not display the image. The image is called petergriffin lol. Sorry for all the other extra code, I am trying to build a little game. If you have any other easier recommendation for showing an image, please give me the code
    Here is my code
    Thanks for any and all help!

    import java.awt.BorderLayout;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
     
    import javax.imageio.ImageIO;
    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.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import java.util.Timer;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import java.util.*;
     
        public class myGame extends JComponent {
        public int w = 400;
        public int z = 400;
        Random ar = new Random();
        public int x = ar.nextInt(400);
        Random har = new Random();
        public int y = har.nextInt(400);
        Random r = new Random();
        Random rand = new Random();
        Random newx;
        Random newy;
        int resetTimes = 0;
        int ex = r.nextInt(400);
        int why = rand.nextInt(400);
        Image petergriffin;
        public void drawImg(){
        	BufferedImage petergriffin = null;
        	try {
        	    petergriffin = ImageIO.read(new File("Users/MW/Downloads/petergriffin.png"));
        	} catch (IOException e) {
        	}
        }
     
        public void ResetIt(){
        	if(x <= ex+17.5 && y <= why+17.5 && x >= ex-17.5 && y >= why-17.5){
     
        		newx = new Random();
        		newy = new Random();
        		int newex = newx.nextInt(450);
        	    int newwhy = newy.nextInt(450);
        		x = newex;
        		y = newwhy;
        		resetTimes++;
        		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();
    	    ResetIt();
     
            }
            if (k == KeyEvent.VK_S){
            	y = y+10;
            	repaint();
        	    ResetIt();
     
            }
            if(k == KeyEvent.VK_A){
        	    x=x-10;
        	    repaint();
        	    ResetIt();
     
            }
        	if(k == KeyEvent.VK_D) {
        	    x=x+10;
        	    repaint();
        	    ResetIt();
        	            }
        	if(k == KeyEvent.VK_Q) {
        	    x=x-10;
        	    y=y-10;
        	    repaint();
        	    ResetIt();
        	            }
        	if(k == KeyEvent.VK_E) {
        	    x=x+10;
        	    y=y-10;
        	    repaint();
        	    ResetIt();
        	            }
        };
     
     
     
    		@Override
    		public void keyReleased(KeyEvent e) {
    			// TODO Auto-generated method stub
     
    		}
     
     
        };
        myGame() {
            JFrame f = new JFrame("SquareMove 1.0");
            JPanel p = new JPanel();
            p.setLayout(new BorderLayout());
            p.add(this);
            f.add(p);
            f.setSize(500, 500);
            f.setVisible(true);
            JLabel l = new JLabel("Users/MW/Downloads/petergriffin.png");
            p.add(l);
            f.add(l);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.addKeyListener(kw);
            p.setBackground(Color.blue);
     
        }
     
        public void paintComponent(Graphics g) {
            g.fillRect(x,y,25,25);
            g.drawRect(ex, why,70,70);
            g.setColor(Color.red);
            g.fillRect(x, y, 50, 50);
            g.setColor(Color.white);
            g.drawString("enter",ex+5,why-10);
            g.setColor(Color.white);
            g.drawString("Score:"+resetTimes,20,20);
            g.drawString("Use WASD to move around", 150, 20);
            petergriffin = new ImageIcon("Users/MW/Downloads/petergriffin.png").getImage();
    		g.drawImage(petergriffin,x,y,null);
    		if(resetTimes > 5 && resetTimes <8){
    			g.drawString("Welcome to level two", 20, 50);
    		}
    		if(resetTimes>8& resetTimes<20){
    			g.drawString("Welcome to level three",20,50);
    		}
        }
     
     
        public static void main(String[] args) {
            myGame d = new myGame();
            d.drawImg();
            d.ResetIt();
        	new myGame();
        }
     
    }


  2. #2
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Why isn't my program showing the image :(?

    Just a quick glance because I'm working ..

        	BufferedImage petergriffin = null;
        	try {
        	    petergriffin = ImageIO.read(new File("Users/MW/Downloads/petergriffin.png"));
        	} catch (IOException e) {
        	}

    You are hiding any exceptions such as incorrect file path (which I almost guarantee you is the problem here). Print the stack trace at the very least:
        	BufferedImage petergriffin = null;
        	try {
        	    petergriffin = ImageIO.read(new File("Users/MW/Downloads/petergriffin.png"));
        	} catch (IOException e) {
                e.printStackTrace();
        	}

  3. The Following User Says Thank You to ChristopherLowe For This Useful Post:

    GregBrannon (January 16th, 2014)

Similar Threads

  1. Image Not Showing...
    By NuclearCherry in forum AWT / Java Swing
    Replies: 3
    Last Post: January 16th, 2014, 02:09 PM
  2. Applet isn't showing up. Perhaps it's a browser fault.
    By javapenguin in forum What's Wrong With My Code?
    Replies: 33
    Last Post: May 14th, 2012, 08:46 PM
  3. [SOLVED] Why isn't the MouseListener showing the X and Y cords like I want it to?
    By javapenguin in forum AWT / Java Swing
    Replies: 33
    Last Post: June 13th, 2011, 07:26 PM
  4. Showing an Image on screen
    By doobybug in forum AWT / Java Swing
    Replies: 1
    Last Post: May 10th, 2011, 07:49 AM
  5. Can't figure out why my image isn't showing up!!
    By thrashingboy in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 8th, 2010, 06:01 PM