Results 1 to 3 of 3
  1. #1
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Paint frame background img and have JButton at same time

    Hello!

    This may seem like a simple issue yet it's got me pretty beat. What I'm wondering is why I'm unable to draw a background image to my frame while, at the same time, having a JButton.

    Please observe the code below which I've tested. As is, this program will open a 900X400 window with a JButton at the top-middle. If you uncomment everything that's been commented out, the same size window will open however, assuming you've got an image handy, a background image will appear and the JButton is nowhere to be found.

    Again, I know the issue's solution may seem simple but I'm trying to make a menu for my game where the user can choose a character name and stats. For this I want to have the background image displayed with a JTextField and JButtons.

    Thanks!

    CODE:

    Java Code:
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Image;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.KeyAdapter;
    
    import javax.swing.ImageIcon;
    import javax.swing.JButton;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.Timer;
    
    public class Main extends JFrame implements ActionListener{
    	
    	/*private Image bg;
    	private Timer timer;*/
    	
    	private JPanel panel = new JPanel();
    	private JButton button = new JButton("Button");
    
    	public Main(){
    		
    		super("Menu Practice");
    		
    		setSize(900, 400);
    		setResizable(false);
    		setLocationRelativeTo(this);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    		
    		/*addKeyListener(new TAdapter());
    		
    		ImageIcon ii = new ImageIcon(this.getClass().getResource("imgs/background.png"));
    		this.bg = ii.getImage();
    		
    		this.timer = new Timer(1000, this);
    		this.timer.start();*/
    		
    		this.panel.add(button);
    		add(this.panel);
    		
    	}
    	
    	/*public void actionPerformed(ActionEvent e){
    		repaint();
    	}
    	
    	public void paint(Graphics g){
    		super.paint(g);
    		Graphics2D g2 = (Graphics2D) g;
    		
    		g2.drawImage(bg, 0, 0, this);
    		
    	}
    	
    	public class TAdapter extends KeyAdapter{
    		
    	}*/
    	
    	public static void main(String args[]){
    		new Main();		
    	}
    	
    }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Paint frame background img and have JButton at same time

    Never paint directly on the JFrame. Instead paint in a JPanel and then use it as the JFrame's contentPane. Also paint in the JPanel's paintComponent method.

  3. #3
    0026sd is offline Member
    Join Date
    Sep 2011
    Posts
    22
    Rep Power
    0

    Default Re: Paint frame background img and have JButton at same time

    Quote Originally Posted by Fubarable View Post
    Never paint directly on the JFrame. Instead paint in a JPanel and then use it as the JFrame's contentPane. Also paint in the JPanel's paintComponent method.
    Yes, that makes sense.

    I should note that I usually use a Board class which extends JPanel and do all my painting there. Then I just create an instance of the Board class in my Main class (which I believe is what you are saying). I only did the code above to simplify the issue.

    Here's another question. You said to paint using the paintComponent method instead of paint. How do you paint a JButton using paintComponent?

Similar Threads

  1. JButton background not displaying (FRUSTRATING)
    By seabass341 in forum AWT / Swing
    Replies: 7
    Last Post: 11-23-2011, 11:25 PM
  2. JButton : Background and Text
    By Keys in forum AWT / Swing
    Replies: 1
    Last Post: 05-26-2011, 04:43 AM
  3. applet background paint
    By footballHunter in forum Advanced Java
    Replies: 12
    Last Post: 11-24-2010, 10:47 PM
  4. paint on JButton event
    By fossiefos in forum AWT / Swing
    Replies: 3
    Last Post: 09-24-2010, 07:46 PM
  5. how i can put image in background of frame??
    By ahmed13 in forum NetBeans
    Replies: 4
    Last Post: 01-15-2009, 05:47 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •