Results 1 to 2 of 2
  1. #1
    fatmat9 is offline Member
    Join Date
    Nov 2010
    Rep Power

    Default adding a buffered image over a jpanel

    I am making a game and am having some trouble adding extra images over a background image. I put the background image in a JPanel and am trying to add images over it that can be easily changed. The images dont show up until i resize the window with my mouse, and then they appear underneath the background. Any help would be appreciated.
    Here is my code, it is spread out over several classes (I don't think there are any problems with the first one, it is only included for completeness):

    Java Code:
    import javax.swing.*;
    import java.awt.Color;
    import java.awt.event.*;
    import java.awt.*;
    public class ViewerA {
    	public JFrame frame;
    	Item s = new Item();
    	 public ViewerA(){ 
    		JFrame frame = new GameViewer(800, 480);
    	public static void main(String[] args){
    		new ViewerA();
    public class GameViewer extends JFrame implements MouseListener{ 
    	private javax.swing.ImageIcon fun;
    	public static String bImage;
    	private JLabel pane;
    	private static int xLocation;
    	private static int yLocation;
    	Look change = new MRcnVb(this); 
    	public static int getxLoc(){
    		return xLocation;
    	public static int getyLoc(){
    		return yLocation;
    	//creates new frame
    	public GameViewer(int _x, int _y) { 
    		this.setSize(_x, _y);		
    		//change = new MRcnVb(this);
    		bImage = change.getBground();
    	public void initializeBackground(){
    		pane = new JLabel();
    	public void changeBackground(String bImage){
    		fun = new javax.swing.ImageIcon(bImage);
    	public void nextScreen(){
    	public void mousePressed(MouseEvent e) {
    		xLocation = e.getXOnScreen();
    		yLocation = e.getYOnScreen();
    		change = change.nextOptions();
    		bImage = change.getBground();
       		System.out.println(xLocation + ", " + yLocation);
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    	public void mouseExited(MouseEvent e) {
    		// TODO Auto-generated method stub
    public class MRcnVb extends Look {
    	private String bGround;
    	//private BufferedImage handle;
    	private GameViewer myGame;
    	private ImageIcon image;
    	public MRcnVb(GameViewer _Game){
    		myGame = _Game;
    	public String getBground() {
    		bGround = "bin\\MRcnVbS.png";
    		return bGround;
    	public Look nextOptions() {
    		int myX = GameViewer.getxLoc();
    		int myY = GameViewer.getyLoc();
    		if((myX > 716 && myY<290) ||(myY < 290 && myX < 100)){
    			return new MRcnVa(myGame);
    		else if (myY < 75){
    			return new MRcnVa(myGame);
    			return new MRcnVb(myGame);
    	public Component drawItems(){
    		Item handleA = new Item();
    		System.out.println("end stuff");
    		return handleA;
    public class Item extends JPanel{
    	private BufferedImage image;
    	public void paintComponent(Graphics g){
    		try {
    			image = File("c:\\users\\matthew\\desktop\\handle.png"));
    		} catch (IOException e) {
    		g.drawImage(image, 200, 200, null);
    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-23-2010 at 04:21 AM. Reason: Moderator Edit: Code tags added

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    One problem I see: You really really don't want to be reading in images from within a component's paintComponent method as this will slow your graphics to a crawl, which also means that you're doing program logic in the paintComponent method, another no-no, and finally also means that you'll be reading from the disk many more times than is necessary.

    Let me look at this some more....

    edit: that's about all I can see since your code does not compile nor run for me due to missing dependencies. If you still have problems, consider creating and posting an SSCCE (please read the 3rd link in my signature links below). Also, please check out how to use code tags when posting code in this forum (the first link). I've edited your post and added tags for you this go around.

    Much luck!
    Last edited by Fubarable; 11-23-2010 at 04:31 AM.

Similar Threads

  1. Replies: 4
    Last Post: 03-23-2010, 04:46 PM
  2. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 03:27 PM
  3. Update existing buffered image
    By rosh72851 in forum New To Java
    Replies: 24
    Last Post: 12-04-2008, 04:43 AM
  4. adding a jpanel in the middle of the script
    By 2o2 in forum AWT / Swing
    Replies: 11
    Last Post: 10-12-2008, 06:50 PM
  5. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 04:49 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