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

Thread: my background always hiding my button

  1. #1
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default my background always hiding my button

    Hi why is that my background or other images that I paint hides my button??
    pic1.jpgpic2.jpg

    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
    import java.util.ArrayList;
    import javax.swing.Timer;
     
    import javax.sound.sampled.AudioInputStream;
    import javax.sound.sampled.AudioSystem;
    import javax.sound.sampled.Clip;
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
     
    public  class Core extends JPanel implements ActionListener{
     
     
     
     
     
    	public Core(){
    			addKeyListener(new TAdapter());
    			setFocusable(true);
    	        setDoubleBuffered(true);
    	        bg = new ImageIcon(getClass().getResource("space.png")).getImage();
    	        menu = new ImageIcon(getClass().getResource("menu.png")).getImage();
    			running = true;
    			setSize(700,700);
    			player = new Player();
    			getButton()
    			;
    			initHeart();	
    			timer = new Timer(5, this);
    	        timer.start();	
     
     
     
    	}
     
    	public void getButton(){
    		JButton shop = new JButton("Rod");
    		shop.setVisible(true);
    		add(shop); 
     
    	}
    	public void addNotify(){
    		super.addNotify();
    		C_width = getWidth();
    		C_hiegth = getHeight();
    	}
     
     
    	private Timer timer;
     
    	public JButton option;
     
    	////////////SOUNDS/////////////////////
     
    	public String explosion = "explosion.wav"; 
    	////////////SOUNDS/////////////////////
     
    	////////////CLASSOBJECT/////////////////////
    	private JButton shop;
    	private Image bg;
    	private Image menu;
    	int score = 0 ;
    	public eHearts heart;
    	public Laser laser;
    	public Explosion explode;
    	////////////CLASSOBJECT/////////////////////
     
    	/////////////SPRITES//////////////////
    	public Player player;
     
     
    	/////////////SPRITES//////////////////
     
    	public Image fireHit;
    	private boolean running;
     
    	///////////IMAGEES////////////////////
    	public Image heart1;
    	public Image boom;
    	///////////IMAGEES////////////////////
    	public Image p;
    	///////////XandYintercep////////////////////
    	public int x;
    	public int y;
    	///////////XandYintercep////////////////////
     
    	///////////ARRAYLIST//////////////////////
    	private ArrayList destroy;
    	private ArrayList h;
    	///////////ARRAYLIST//////////////////////
     
    	///////////POSITION OF HEART/////////////////
    	private int [] [] xANDy = {{0,-200},{100,100},{300,300},{400, 200},{350,150},{240,-100},{350,-150},{450,-150},{200,150},{60,60}};
    	///////////POSITION OF HEART/////////////////
    	private int C_width;
    	private int C_hiegth;
     
     
    	public void stop(){
    		running = false;
    	}
     
    	public void run(){
     
    	}//call init and gameLoop
     
    	public void initHeart(){	
    		/////////////////DELETEHEARTSWHENHIT/////////////
    		h = new ArrayList();
     
    		for (int i= 0; i < xANDy.length; i++){
    			h.add(new eHearts(xANDy[i][0], xANDy[i][1]));
    		}
    		/////////////////DELETEHEARTSWHENHIT/////////////
     
    		}
     
     
     
    //////////////////////////////////CHECKINGFORCOLLISION/////////////////////////	
    	public void checkCollision(){
    		Rectangle pc = player.getBounds();
    		for(int c = 0 ; c < h.size() ; c++)
    		{
    			eHearts eh = (eHearts) h.get(c) ;
     
    			Rectangle e = eh.getBounds();
    			if(pc.intersects(e)){
    				running = false;
    			}
    		}
     
    		ArrayList f = player.getFire();
     
    		for(int i = 0 ; i < f.size() ; i++){
    			Laser l = (Laser) f.get(i) ;
     
    		Rectangle p = l.getBounds();
     
    		if(l.getY() < 0){
    			l.setVisible(false);
    		}
     
     
    			for(int c = 0 ; c < h.size() ; c++)
    			{
    				eHearts eh = (eHearts) h.get(c) ;
     
    				Rectangle e = eh.getBounds();
     
     
    				if(p.intersects(e))
    				{ 
    					 ++score;
    					new SoundsEffects(explosion);
    					l.setVisible(false);
    					eh.setVisible(false);	
    					 ArrayList b = player.getExplode();
    					 for(int ex = 0; i < b.size(); i++){
    						 Explosion explode = (Explosion) b.get(ex);
     
    						 explode.setX(eh.getX());
    						 explode.setY(eh.getY());
    						 explode.setVisible(false);
    					 }
    				}		
     
    			}				
     
    		}
    	}
     
    //////////////////////////////////CHECKINGFORCOLLISION/////////////////////////	
     
     
    	 public void actionPerformed(ActionEvent e) {
    		 if(player.getPause()){	
    		 	ArrayList f = player.getFire();
     
    			for(int i = 0 ; i < f.size() ; i++){
    				Laser l = (Laser) f.get(i) ;
    				if(l.getVisible()){
    					l.move();
     
    				}else{
    					f.remove(i);
    				}
    			}
     
    		 for(int i2 = 0 ; i2 < h.size() ; i2++){
    				eHearts eh = (eHearts) h.get(i2) ;
    				if(eh.getVisible()){
    					eh.move();
    				}else{
    					h.remove(i2);
    				}
    		 }	 
    		 player.move();
    		 checkCollision();
    		 }//end pause
     
    		 repaint();
     
     
    	 }
     
    	public void paint(Graphics g){
    		super.paint(g);
     
    		if (running){
     
    			Graphics2D g1 = (Graphics2D)g;
    		g.drawImage(bg,0,0,null);
    		if(player.getVisible()){	
    		g1.drawImage(player.getImage(), Math.round(player.getX()), Math.round(player.getY()), null);
    		}
     
    		ArrayList f = player.getFire();	
     
     
    		for(int i = 0 ; i < f.size() ; i++){
    			Laser l = (Laser) f.get(i) ;
     
    			g1.drawImage(l.getLaser(), l.getX(), l.getY(), null);	
    		}
     
    		////////////////////FOR BOOM////////////////////////////////////////////////
     
    		 ArrayList b = player.getExplode();  
    		 for(int i = 0; i < b.size(); i++){
    			 Explosion explode = (Explosion) b.get(i);
    			 if(explode.getVisible() == false){
    				g1.drawImage(explode.getExplosion(), explode.getX(), explode.getY(), null); 
    		 }	 
    		}	
    		////////////////////FOR BOOM////////////////////////////////////////////////
     
    		////////////////////TARGET///////////////////////////////////////////////////	
    		for(int i = 0; i< h.size(); i++){
    			eHearts eh = (eHearts) h.get(i);
    			if(eh.getVisible()){
    				g1.drawImage(eh.getEnemy(), eh.getX(), eh.getY(), null);
    			}	
    		}
    		////////////////////TARGET///////////////////////////////////////////////////
    		g1.setColor(Color.WHITE);
    		g1.drawString("Score: " + score, 5, 15);
    		if(player.getPause() == false){
    			g1.drawImage(menu, 250,150,null);
     
     
     
    		}
    		}//end game ender
     
    		Toolkit.getDefaultToolkit().sync();
            g.dispose();
     
    	} 
     
     
     private class TAdapter extends KeyAdapter{
     
    	 public void keyReleased(KeyEvent e){
     
    		 player.keyReleased(e);
    	 }
     
    	 public void keyPressed(KeyEvent e){
    		 player.keyPressed(e);
    	 }
     }
     
     
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,140
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: my background always hiding my button

    How can the posted code be compiled and executed for testing? It does not have a main() method.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: my background always hiding my button

    For one thing, your code formatting is not so good. If you're going to ask volunteers to set aside time to help you, the least you can do is to put in some effort yourself to post only well-formatted (and preferably compilable) code. I suggest that you don't use tabs for indentation but rather spaces, and 3 spaces ought to be sufficient. Please be sure that your indentations are uniform across a block of code. Please avoid excess whitespace and avoid too little whitespace as well. There are Java code formatting guidelines that can help you with this.

    Next, don't override a JPanel's paint method but rather its paintComponent method, especially if you're doing animation since the latter has double buffering help by default. Next and your main problem: *never* dispose of a Graphics object given to you by the JVM. So that line
    g.dispose();

    simply get rid of it.

    Else the program will have no Graphics object to draw its child components like JButtons and such.

  4. #4
    Junior Member
    Join Date
    Sep 2012
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: my background always hiding my button

    Quote Originally Posted by curmudgeon View Post
    For one thing, your code formatting is not so good. If you're going to ask volunteers to set aside time to help you, the least you can do is to put in some effort yourself to post only well-formatted (and preferably compilable) code. I suggest that you don't use tabs for indentation but rather spaces, and 3 spaces ought to be sufficient. Please be sure that your indentations are uniform across a block of code. Please avoid excess whitespace and avoid too little whitespace as well. There are Java code formatting guidelines that can help you with this.

    Next, don't override a JPanel's paint method but rather its paintComponent method, especially if you're doing animation since the latter has double buffering help by default. Next and your main problem: *never* dispose of a Graphics object given to you by the JVM. So that line
    g.dispose();

    simply get rid of it.

    Else the program will have no Graphics object to draw its child components like JButtons and such.
    Thank you very much now it is working. It is the paint method fault. now Im using the paintComponent method. that helps alot.

  5. #5
    Super Moderator curmudgeon's Avatar
    Join Date
    Aug 2012
    Posts
    1,130
    My Mood
    Cynical
    Thanks
    64
    Thanked 140 Times in 135 Posts

    Default Re: my background always hiding my button

    Quote Originally Posted by poldz123 View Post
    Thank you very much now it is working. It is the paint method fault. now Im using the paintComponent method. that helps alot.
    You're welcome!

Similar Threads

  1. [2 Part Qestion] Saving Game Data & Hiding GUI from taskbar
    By Jello36 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 28th, 2012, 09:38 PM
  2. I need help with removing arrays or hiding them.
    By seaofFire in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 23rd, 2012, 07:09 PM
  3. Button does not change Background
    By Noob in forum AWT / Java Swing
    Replies: 6
    Last Post: November 16th, 2011, 01:34 PM
  4. Hiding\Showing parts of the frame
    By liron50 in forum AWT / Java Swing
    Replies: 17
    Last Post: April 29th, 2011, 12:14 PM
  5. Data hiding and maintainability
    By humdinger in forum Object Oriented Programming
    Replies: 11
    Last Post: December 5th, 2009, 01:10 PM