Results 1 to 6 of 6
  1. #1
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default error when adding Images

    I have been trying my hand at making 2d top down view games and have found myself repeatedly using the same code so i created a file with all the functions so when it is down i just import the jar and dont have to keep rewriting functions. I have trouble because the background is loaded on the screen and i dont know why as i didn't tell it to load. In fact I set the panels visiblity to false. Both of my classes are in the same package. Could someone tell me why I still get the Image.

    Walking Turtle
    Java Code:
    import javax.swing.*;
    import java.awt.event.*;
    public class turtleWalker extends JFrame{
    
    	public static void main(String[] args){
    		JFrame frame = new JFrame("Walking Turtle");
    		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		frame.setSize(400,300);
    		App mapController = new App();
    		mapController.init();
    		frame.add(mapController);
    		frame.setVisible(true);
    	}
    }
    class App extends JPanel implements KeyListener{
    	characterMover characterMover = new characterMover();
    	int[] cright=new int[7];
    	int[] cleft=new int[7];
    	int[] ctop= new int[7];
    	int[] cbottom=new int[7];
    	boolean collision=false;
    	public void init(){
    		cleft[0]=0;
    		cright[0]=184;
    		cleft[1]=83;
    		cright[1]=638;
    		cleft[2]=591;
    		cright[2]=638;
    		cleft[3]=261;
    		cright[3]=507;
    		cleft[4]=0;
    		cright[4]=168;
    		cleft[5]=402;
    		cright[5]=591;
    		cleft[6]=141;
    		cright[6]=478;
    		ctop[0]=0;
    		cbottom[0]=160;
    		ctop[1]=0;
    		cbottom[1]=63;
    		ctop[2]=63;
    		cbottom[2]=510;
    		ctop[3]=135;
    		cbottom[3]=308;
    		ctop[4]=231;
    		cbottom[4]=429;
    		ctop[5]=377;
    		cbottom[5]=545;
    		ctop[6]=504;
    		cbottom[6]=510;
    		setFocusable(true);
    	    addKeyListener(this);
    	    this.setLayout(null);
    	    characterMover.setBounds(0,0,639,507);
    		characterMover.setBackground("C:/Program Files/Java/jdk1.7.0_45/bin/walkingTurtle/background.jpg",0,0,639,507);
    		characterMover.setSprite("C:/Program Files/Java/jdk1.7.0_45/bin/walkingTurtle/sprite.jpg",176,212, 44, 34);
    		this.add(characterMover);
    	}
    	
        public void keyPressed(KeyEvent e){
    			if(e.getKeyCode()==KeyEvent.VK_RIGHT){
    				characterMover.moveRight();
    			}
    			else if(e.getKeyCode()==KeyEvent.VK_LEFT){
    					characterMover.moveLeft();
    					this.add(characterMover);
    			}
    			else if(e.getKeyCode()==KeyEvent.VK_UP){
    					characterMover.moveUp();
    					this.add(characterMover);
    			}
    			else if(e.getKeyCode()==KeyEvent.VK_DOWN){
    					characterMover.moveDown();
    					this.add(characterMover);
    			}
    
    	}	
    	public void keyReleased(KeyEvent e){
    	}
    	public void keyTyped(KeyEvent e){
    	}
    
    
    }
    Character Controller
    Java Code:
    import javax.swing.JPanel;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    class characterMover extends JPanel{
    	private int spriteX,spriteY,spriteWidth,spriteHeight,backgroundX,backgroundY,backgroundWidth,backgroundHeight,screenWidth,screenHeight;
    	private ImageIcon spriteImg,backgroundImg;
    	private JLabel spriteFImg,backgroundFImg;
    	private boolean backgroundAdded=false,spriteAdded=false;
    	public void moveLeft(){
    		addStuff();
    	}
    	public void moveRight(){
    		addStuff();
    	}
    	public void moveUp(){
    		addStuff();
    	}
    	public void moveDown(){	
    		addStuff();
    	}
    	public void setBackground(String url,int x,int y,int width, int height){
    		this.setLayout(null);
    		this.backgroundX=x;
    		this.backgroundY=y;
    		this.backgroundWidth=width;
    		this.backgroundHeight=height;
    		this.backgroundImg=new ImageIcon(url);
    		this.backgroundFImg=new JLabel(backgroundImg);
    		if(spriteAdded==true&&backgroundAdded==false){
    			backgroundAdded=true;
    			addStuff();	
    		}
    		else{ 
    			backgroundAdded=true;
    		}
    	}
    	public void setSprite(String url,int x,int y, int width, int height){
    		this.spriteX=x;
    		this.spriteY=y;
    		this.spriteHeight=height;
    		this.spriteWidth=width;
    		this.spriteImg = new ImageIcon(url);
    		this.spriteFImg=new JLabel(this.spriteImg);
    		spriteAdded=true;
    			addStuff();
    	}
    	public void addStuff(){
    		System.out.println("Stuff");
    		this.setLayout(null);
    		
    		this.setVisible(false);
    	}
    }
    And Results
    error when adding Images-walkingturtle-error.jpg
    With the right know how, anything is possible

  2. #2
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: error when adding Images

    I hope this is allowed. Bumb
    With the right know how, anything is possible

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: error when adding Images

    It doesn't do that for me. Are you certain you are running the posted code?

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: error when adding Images

    Quote Originally Posted by jim829 View Post
    It doesn't do that for me. Are you certain you are running the posted code?

    Regards,
    Jim
    Nope turns out I wasn't. To use the code in my turtleWalker class i just copied and pasted it. So when I updated my CharacterMover class it was saving to the original spot and the turtleWalker still had the old version
    With the right know how, anything is possible

  5. #5
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: error when adding Images

    BTW, I can offer a couple of suggestions for visual improvement. When you initialize your arrays, you can do it like this:

    Java Code:
    int [] a = {1,2,3,4,5};  // and so forth.
         //or
    int [] a;
    a = new int[]{1,2,3,4,5};
    //

    And your backgroundadded = true is set to true regardless of the if outcome. Is that what you want. You may as well just set it to true outside the if blocks.
    (See lines 30 and 34)

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  6. #6
    Daryn is offline Senior Member
    Join Date
    Oct 2012
    Posts
    176
    Rep Power
    3

    Default Re: error when adding Images

    Quote Originally Posted by jim829 View Post
    And your backgroundadded = true is set to true regardless of the if outcome. Is that what you want. You may as well just set it to true outside the if blocks.
    (See lines 30 and 34)

    Regards,
    Jim
    Thanks Jim and backgroundAdded does change but I omitted that parts to make sure it wasn't causing my Problems
    With the right know how, anything is possible

Similar Threads

  1. adding images
    By alexix in forum New To Java
    Replies: 3
    Last Post: 03-04-2013, 11:36 PM
  2. adding different images using if statements
    By JoePenguin in forum New To Java
    Replies: 3
    Last Post: 02-16-2012, 04:54 AM
  3. Help: Displaying Images AND Adding Buttons
    By Rhez in forum New To Java
    Replies: 2
    Last Post: 08-05-2010, 07:19 AM
  4. Adding multiple images to frame
    By 435.mahesh in forum AWT / Swing
    Replies: 1
    Last Post: 04-24-2009, 09:31 PM
  5. [new] need help on GUI adding images
    By counterfox in forum AWT / Swing
    Replies: 5
    Last Post: 03-19-2009, 11:29 AM

Posting Permissions

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