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

    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);
    		frame.setTitle("funnnnnnnn!!!!!!!!");
    		//frame.add(s);
    		//frame.pack();
    		frame.setVisible(true);
    	}
    	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.addMouseListener(this);
    		this.setSize(_x, _y);		
    		this.setTitle("Museum"); 
    		setVisible(true);
    		//change = new MRcnVb(this);
    		bImage = change.getBground();
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    		initializeBackground();
    		changeBackground(bImage);
    				 
    	}
    	
    	public void initializeBackground(){
    		pane = new JLabel();
    		this.add(pane);
    		setVisible(true);
    		
    	}
    	
    	public void changeBackground(String bImage){
    		fun = new javax.swing.ImageIcon(bImage);
    		pane.setIcon(fun);
    		
    	}
    	
    	public void nextScreen(){
    		changeBackground(bImage);
    	}
    		
    	public void mousePressed(MouseEvent e) {
    		xLocation = e.getXOnScreen();
    		yLocation = e.getYOnScreen();
    		change = change.nextOptions();
    		bImage = change.getBground();
    		nextScreen();
    		change.drawItems();
    		repaint();
    		
    
       		System.out.println(xLocation + ", " + yLocation);
    	}
    	
    	
    
    	@Override
    	public void mouseClicked(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseReleased(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	public void mouseEntered(MouseEvent e) {
    		// TODO Auto-generated method stub
    		
    	}
    	@Override
    	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;
    
    	}
    	
    	
    
    	@Override
    	public String getBground() {
    		bGround = "bin\\MRcnVbS.png";
    		return bGround;
    		
    	}
    
    	@Override
    	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);
    		}
    		else{
    			return new MRcnVb(myGame);
    		}
    	}
    	@Override
    	public Component drawItems(){
    		Item handleA = new Item();
    		myGame.add(handleA);
    		System.out.println("end stuff");
    		myGame.repaint();
    		return handleA;
    	}
    
    
    public class Item extends JPanel{
    	private BufferedImage image;
    	
    	public void paintComponent(Graphics g){
    
    		try {
    			image = ImageIO.read(new File("c:\\users\\matthew\\desktop\\handle.png"));
    		} catch (IOException e) {
    			e.printStackTrace();
    		}
    		g.drawImage(image, 200, 200, null);
    		
    		System.out.println("works");
    	}
    
    }
    Moderator Edit: Code tags added
    Last edited by Fubarable; 11-23-2010 at 03:21 AM. Reason: Moderator Edit: Code tags added

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

    Default

    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 03:31 AM.

Similar Threads

  1. Replies: 4
    Last Post: 03-23-2010, 03:46 PM
  2. Problem on adding JButton on JPanel NEED HELP
    By boisk in forum AWT / Swing
    Replies: 15
    Last Post: 03-15-2009, 02:27 PM
  3. Update existing buffered image
    By rosh72851 in forum New To Java
    Replies: 24
    Last Post: 12-04-2008, 03: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, 05:50 PM
  5. Unable to draw buffered image
    By pedjasmek in forum Java 2D
    Replies: 7
    Last Post: 08-08-2008, 03: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
  •