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

    Default Problem in my Swing program

    I am trying to make a game with an image on it, and I have one class that gets the image and the other is the game. When I run the program, it just shows my image on a gray JFrame. This leads me to think the image might be covering my whole game, which is weird because it is a mostly transparent image. I have tried resizing it and everything but I don't know what to do.. decided to bring it to the experts :)
    Here is my image class
    Java Code:
     import java.awt.BorderLayout;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    import java.io.IOException;
    import javax.imageio.ImageIO;
    import javax.swing.*;
    
    public class DisplayImage extends JPanel {
        private BufferedImage image;
        public DisplayImage(){
            try{
                image = ImageIO.read(new File("/Users/MWaldman/Downloads/petergriffin2.png"));
            } catch(Exception e){System.out.println("Error");
            setPreferredSize(new Dimension(300,200));
        }
        }
        public void paintComponent(Graphics g){
            if (image != null) { // added to prevent null pointer exception
               super.paintComponent(g);
               g.drawImage(image,0,0,null);
            }
        }
    }
    Here is my game class:
    Java Code:
     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 {
    	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);
    
    	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) {
    		}
    
    		@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-generate
    
    		}
    
    	};
    
    	myGame() {
    		DisplayImage I = new DisplayImage();
    		JFrame f = new JFrame("Square Mover 1.0");
    		JPanel p = new JPanel();
    		p.setLayout(new BorderLayout());
    		f.add(I);
    		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.setColor(Color.red);
    		g.fillRect(x, y, 50, 50);
    		g.setColor(Color.black);
    		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);
    		g.drawString("Level " + resetTimes / 5, 20, 50);
    	}
    
    	public static void main(String[] args) {
            new DisplayImage();
    		myGame d = new myGame();
    		d.ResetIt();
    	}
    
    }
    Thanks for your help!

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,026
    Rep Power
    6

    Default Re: Problem in my Swing program

    You've already asked this in another thread. Why are you asking this in a new thread?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Problem in my Swing program

    Well the other thread was on a different subject, and no one answered so I guess it was because that wasn't the original topic. So I made a thread that addresses this issue.

Similar Threads

  1. Replies: 7
    Last Post: 06-27-2012, 06:30 PM
  2. Using MVC architecture in a Swing GUI program?
    By AWJustAGuy in forum New To Java
    Replies: 2
    Last Post: 04-24-2012, 06:57 AM
  3. Java swing program
    By Jake23 in forum AWT / Swing
    Replies: 1
    Last Post: 02-07-2012, 03:22 PM
  4. Problem Using KeyListener in swing program.
    By gauravfzd in forum AWT / Swing
    Replies: 7
    Last Post: 08-24-2011, 11:13 AM
  5. Simple swing program, help please!
    By N00Bie in forum New To Java
    Replies: 5
    Last Post: 02-17-2011, 06:16 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
  •