Results 1 to 9 of 9
  1. #1
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Problem with java.swing.JFrame

    I'm trying to make a code that will split a spritesheet so it saves all the sprites in a BufferedImage array. There are no compilation errors but the code wont work. I attach the image i'm using.


    Java Code:
    import java.awt.Graphics;
    import java.awt.Image;
    import java.awt.image.BufferedImage;
    import java.io.File;
    
    import javax.imageio.ImageIO;
    import javax.swing.ImageIcon;
    import javax.swing.JFrame;
    
    
    public class Sprites extends JFrame{
    	
    	private Image dbImage;
    	private Graphics dbg;
    	static BufferedImage bigImg;
    	static BufferedImage[] spriteSheet;
    
    	public Sprites(){
    		setSize(400, 400);
    		setTitle("Título");
    		setResizable(false);
    		setVisible(true);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		
    	}
    	public void paint(Graphics g){
    		dbImage = createImage(getWidth(), getHeight());
    		dbg = dbImage.getGraphics();
    		paintComponent(dbg);
    		g.drawImage(dbImage,0,0,this);
    		
    	}
    	
    	public void paintComponent(Graphics g){
    
    		g.drawImage(spriteSheet[0],80,80,this);
    		
    	}
    	
    	public static void main(String[] args){
    
    		Sprites main = new Sprites();
    		try{
    			bigImg = ImageIO.read(new File("C:/Users/Guille/workspace/Javagame/src/sprite_goku.png"));
    			}catch(Exception e){}
    		
    		spriteSheet= new BufferedImage[8];
    		for(int i = 0; i < 8; i++){
    			spriteSheet[i]=bigImg.getSubimage(i*20,0,41,40);
    		}
    	}
    
    }
    Attached Thumbnails Attached Thumbnails Problem with java.swing.JFrame-sprite_goku.png  

  2. #2
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: Problem with java.swing.JFrame

    What does "won't work" mean?

    And why create subimages?
    You know how wide an individual "frame" is in the image, so I would simply blit that section onto the graphics.
    Saves creating all the sub images.

    Oh, and you shouldn't be overiding paint(), just paintComponent().
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Problem with java.swing.JFrame

    by not working i mean that the code launches a runtime error which i cant understand.

    I want to build an array of subimages because as far as i know is the fastest way to access the images. If I want to use the sprite number 5 I just use sprites[5].
    I dont know if there is any easier way to do that.

    EDIT: I override paint() because i use doble buffering. If i delete it the program doesn't work (at least the other examples of JFrame that I have dont work without it)
    Last edited by Filoflow; 03-13-2013 at 03:13 PM.

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

    Default Re: Problem with java.swing.JFrame

    Quote Originally Posted by Tolls View Post
    What does "won't work" mean?


    Oh, and you shouldn't be overiding paint(), just paintComponent().
    Don't think so. JFrame does not inherit from JComponent.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: Problem with java.swing.JFrame

    Try this:

    Java Code:
    public void paint(Graphics g){
           super.paint(g);
           if (spriteSheet == null)
              return;
            g.drawImage(spriteSheet[0],80,80,this);
    }
    
    // and place this last in main
    
     main.repaint();
    However, once the image will disappear once you move the window offscreen. Best to draw images in a JPanel and add that into the JFrame. Then you can override paintComponent(Graphics g).

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Problem with java.swing.JFrame

    That last code worked and the imagen doesn't disappear when moving the window out of the screen, i dont know if i'm understanding what you meant.

  7. #7
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,851
    Rep Power
    19

    Default Re: Problem with java.swing.JFrame

    Quote Originally Posted by Filoflow View Post
    by not working i mean that the code launches a runtime error which i cant understand.
    You don't think it might be an idea to actually post the error?

    Quote Originally Posted by Filoflow View Post
    I want to build an array of subimages because as far as i know is the fastest way to access the images. If I want to use the sprite number 5 I just use sprites[5].
    I dont know if there is any easier way to do that.
    Most of the ones I've seen simply blit the relevant portion of the animation image onto the screen.
    It's possible I'm an out of date fuddy duddy of course.

    Quote Originally Posted by Filoflow View Post
    EDIT: I override paint() because i use doble buffering. If i delete it the program doesn't work (at least the other examples of JFrame that I have dont work without it)
    Looks like you copied code from a JPanel.

    Quote Originally Posted by jim829 View Post
    Don't think so. JFrame does not inherit from JComponent.

    Regards,
    Jim
    Didn't notice it was extending a JFrame!
    :)
    The paintComponent method made me think I was looking at a JPanel.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Filoflow is offline Member
    Join Date
    Mar 2013
    Posts
    10
    Rep Power
    0

    Default Re: Problem with java.swing.JFrame

    The code at paint() is copied from a Java games developing tutorial , and so is the "extends JFrame". As you can see i'm very unexperienced about these classes :P

  9. #9
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,415
    Rep Power
    5

    Default Re: Problem with java.swing.JFrame

    No, you clearly understood what I said. When I move the JFrame so the image is not visible and then move the JFrame back, the image is gone. This is usually because there is nothing to trigger a repaint of the JFrame. It has been my experience that this usually happens when painting to JFrame. I usually only paint to JPanels and add them to the Frame. I'm not certain why you are not seeing the behavior I am seeing.

    Also, don't ignore Exceptions. In a try - catch block at least print the stack trace. Otherwise you might miss a clue as to why something isn't working.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

Similar Threads

  1. Swing JFrame Maximise Bug?
    By RhysBradbury in forum AWT / Swing
    Replies: 3
    Last Post: 01-07-2013, 02:09 PM
  2. help run java swing (jFrame)
    By azhar21 in forum AWT / Swing
    Replies: 1
    Last Post: 05-14-2012, 07:34 PM
  3. how to know when jframe swing is painted
    By petmoreno in forum AWT / Swing
    Replies: 1
    Last Post: 11-02-2010, 07:06 PM
  4. Java Swing Problem
    By mohitgupta1989 in forum Advanced Java
    Replies: 7
    Last Post: 06-23-2010, 07:46 AM
  5. JFrame/Swing Question
    By Moncleared in forum AWT / Swing
    Replies: 9
    Last Post: 01-16-2009, 08:16 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
  •