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

Thread: Can't make this circle disapear!

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

    Post Can't make this circle disapear!

    I am trying to make it so when you shoot a circle it disapears but I can't seem to get it to work!
    Most of what relates to the circle disapearing is at the bottom.
    Can you find my mistake?
    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;
    	boolean enemy1hit = 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);
    		setBackground(Color.white);
    	}
     
    	public class KL implements KeyListener {
     
    		public void keyPressed(KeyEvent e) {
     
    			if (e.getKeyCode() == KeyEvent.VK_LEFT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					System.out.println(" ");
    					x = x - 5;
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot);
    				if (x <= 10) {
    					x = 10;
    					System.out
    						.println("Collision detected with left side of window!");
     
    				}
    			}
    			if (e.getKeyCode() == KeyEvent.VK_RIGHT) {
     
    				System.out.println("\nStarting with " + x);
    				System.out.println("x is now " + x);
    					x = x + 5;
    					System.out.println(" ");
    					xShoot=(int) (x+37.5);
     
    					System.out.println("Bullet is at: x: " + xShoot + " y: " + yShoot );
    				}
    				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);
     
     
     
    		g.fillRect((int) blarp.getX(), (int) blarp.getY(), 90, 50);
     
     
    		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();
     
    		Rectangle enemy1= new Rectangle(50, 50, 10, 10);
    		g.fillOval(50, 50, 10, 10);
     
    		if(yShoot<323) {
    			yShoot = yShoot - 5;
    			repaint();
    		}	
    		if(Bullet.getX()<=57 && Bullet.getX()>=42 && Bullet.getY()<=60 && Bullet.getY()>=40) {
    			System.out.println("ENEMY1 Hit!");
    			enemy1hit= true;
     
     
    		}
    	if(enemy1hit=true) {
    				g.setColor(Color.white);
    				g.fillOval(50,50,10,10);
    		}
    	}	
    }

    Thanks and please if you can help me!


  2. #2
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't make this circle disapear!

    What seems to be the problem?
    Does nothing happen when you shoot?
    Does it shoot but not detect collision?
    Does it detect collision but not disappear?

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

    Default Re: Can't make this circle disapear!

    You can shoot the circle but the circle doesn't disapear it just stays there

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: Can't make this circle disapear!

    Is the collision detection working? Put a println or two in the code and try to see where things stop happening the way you expected.

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

    Default Re: Can't make this circle disapear!

    I have done that and collision is working its just the circle wont go away

  6. #6
    Junior Member
    Join Date
    Sep 2012
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Can't make this circle disapear!

    i guess you need to tell that circle not to paint itself.. on a side note - you have a line that says:
    if (enemylhit=true) 
     // something..
    that line will ALWAYS be true, since you need == for comparison, only 1 = is assignment..

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

    Default Re: Can't make this circle disapear!

    Ohhh so i need == instead of = Ok thanks now all i mee to know is how to tell it to unpaint itself or something like that

Similar Threads

  1. Replies: 1
    Last Post: January 3rd, 2012, 06:28 PM
  2. Issues with selecting a drawn circle on graphics
    By Diplo in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 31st, 2011, 11:53 AM
  3. java (circle)
    By coder.freak in forum Member Introductions
    Replies: 3
    Last Post: March 26th, 2011, 12:01 PM
  4. Moving circle problem help
    By nobleisthyname in forum What's Wrong With My Code?
    Replies: 1
    Last Post: June 3rd, 2010, 11:43 AM
  5. Implement dragging a circle
    By jolly in forum AWT / Java Swing
    Replies: 0
    Last Post: August 19th, 2009, 02:48 PM