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 3 of 3

Thread: Image won't work!

Threaded View

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    17
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Image won't work!

    I am trying to get an image to appear as what you are moving but it won't work!
    I am not good at image stuff and have no experience with it so if someone does please help me!
    package galaga;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.Rectangle;
    import java.awt.event.KeyEvent;
    import java.awt.event.KeyListener;
    import java.awt.event.MouseEvent;
    import java.awt.event.MouseListener;
    import java.awt.image.ImageObserver;
    import java.io.File;
     
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
     
    public class mainmethod extends JFrame {
    	int x = 200, y = 325;
    	KL bob = new KL();
    	int xShoot = (int) (x + 37.5);
    	int yShoot = y;
    	boolean READY = false;
     
    	ImageIcon Spaceship = new ImageIcon("CRAPPYSPACESHIP.png");
    	Image Ship = Spaceship.getImage();
    	ImageObserver bo;
     
    	public mainmethod() {
    		super("Galaga");
    		setVisible(true);
    		setLocation(150, 150);
    		setSize(400, 400);
    		setDefaultCloseOperation(EXIT_ON_CLOSE);
    		addKeyListener(bob);
    	}
     
    	public class KL implements KeyListener {
     
    		public void keyPressed(KeyEvent e) {
     
    			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
     
    				System.out.println("Starting with " + x);
    				System.out.println("x is now " + x);
    					System.out.println(" ");
    					x = x - 5;
    					xShoot=(int) (x+37.5);
    				if (x <= 10) {
    					x = 10;
    					System.out
    						.println("Collision detected with left side of window!");
     
    				}
    			}
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
     
    				System.out.println("Starting with " + x);
    				System.out.println("x is now " + x);
    					x = x + 5;
    					System.out.println(" ");
    					xShoot=(int) (x+37.5);
    				}
    				if (x >= 300) {
    					x = 300;
    					System.out
    						.println("Collision detected with right side of window!");
     
     
    			}
     
    			repaint();
     
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = true;
    				System.out.println("FIRE! \n");
    			}
    		}
     
    		public void keyReleased(KeyEvent e) {
    			if (e.getKeyCode() == KeyEvent.VK_SPACE) {
    				READY = false;
    				System.out.println("Not READY TO FIRE! \n");
    				if(READY=true) {
    					yShoot = yShoot -5;
    					repaint();
    				}
    			}
    		}
     
    		@Override
    		public void keyTyped(KeyEvent e) {
     
    		}
    	}
     
    	public void paint(Graphics g) {
     
    		Image dbImage = createImage(getWidth(), getHeight());
    		Graphics dbGraphics = dbImage.getGraphics();
    		paintComponent(dbGraphics);
    		g.drawImage(dbImage, 0, 0, this);
    		repaint();
     
    	}
     
    	public void paintComponent(Graphics g) {
    		g.setColor(Color.CYAN);
     
    		Rectangle blarp = new Rectangle(x, y, 150, 150);
     
    		bo.imageUpdate(Ship, PROPERTIES, x, y, (int) blarp.getWidth(), (int) blarp.getHeight());
     
    		g.drawImage(Ship, (int) blarp.getX(), (int) blarp.getY(), 90, 50, bo);
     
     
    		if (yShoot<4) {
    			yShoot=(int) blarp.getY();
    			xShoot=(int) ((int) blarp.getX() + 37.5);
    			repaint();
    		}
    		g.setColor(Color.RED);
    		Rectangle Bullet = new Rectangle(xShoot, yShoot, 10, 20);
    		g.fillRect((int) Bullet.getX(), (int) Bullet.getY(), 20, 30);
    		repaint();
     
    		if(yShoot<323) {
    			yShoot = yShoot - 5;
    			repaint();
    		}	
     
     
    	}
     
    }
    thx and please answer. All help will be MUCH appreciated!
    Last edited by jps; October 26th, 2012 at 05:35 PM.


Similar Threads

  1. Why won't this for loop work?
    By vwillis in forum Loops & Control Statements
    Replies: 1
    Last Post: October 14th, 2011, 12:49 PM
  2. Scanner code won't work
    By r19ecua in forum What's Wrong With My Code?
    Replies: 6
    Last Post: April 21st, 2011, 04:49 PM
  3. MergeSort won't work
    By joshft91 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 09:44 PM
  4. My lines won't work!!!
    By The Mewzytion in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 2nd, 2010, 10:24 AM
  5. Why won't this while loop work?
    By trueblue in forum Loops & Control Statements
    Replies: 2
    Last Post: July 17th, 2009, 09:10 AM