Results 1 to 7 of 7
  1. #1
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default What happened to my graphics? -converting to applet

    so I want to upload my game online, but to do that I must make it an applet...

    so I create a new class that extends JApplet and that replaces the JFrame.

    After doing this and running it in applet viewer (from eclipse) my game looses all it's graphics (the only thing shown are lines, text, enemy health bars and the background)

    so I guess this means my resources (images, sound files etc) are not available to the applet viewer.

    can someone explain the following things to me peas

    how do I make my resources available to the applet viewer
    do I need to put everything into a .jar file before uploading? could you explain the process and how I would go about it and make sure all my classes are seeing each other.

    thanks

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

    Default Re: What happened to my graphics? -converting to applet

    I think that it's hard for us to assume anything at all as to the source of your problem based on your description alone. I think that your best bet is to start small and simple -- create the most basic JApplet that attempts to show the most basic of graphics, and if it still doesn't work, post your attempt here, and let's work on it together.

  3. #3
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: What happened to my graphics? -converting to applet

    Quote Originally Posted by Fubarable View Post
    I think that it's hard for us to assume anything at all as to the source of your problem based on your description alone. I think that your best bet is to start small and simple -- create the most basic JApplet that attempts to show the most basic of graphics, and if it still doesn't work, post your attempt here, and let's work on it together.
    Java Code:
    //Test class
    package pkg;
    
    import java.awt.GridLayout;
    import javax.swing.JApplet;
    
    public class Test extends JApplet
    {	
    	public void init()
    	{
    		setSize(300, 300);
    		setLayout(new GridLayout(1, 1, 0, 0)); 
    		PanelTest x = new PanelTest();
    		add(x);
    		setVisible(true);
    		
    	}	
    }
    
    //PanelTest class
    package pkg;
    
    import java.awt.Graphics;
    import java.awt.Image;
    import javax.swing.ImageIcon;
    import javax.swing.JPanel;
    
    public class PanelTest extends JPanel
    {
    	Image image;
    	
    	public PanelTest()
    	{
    		System.out.println("constructor");
    	}
    	
    	public void paintComponent(Graphics g)
    	{
    		image = new ImageIcon("resources/gold.png").getImage();
    		g.drawImage(image, 1, 1, 26, 26, null);
    		
    		System.out.println("paint component");
    	}
    }
    actually replicating the problem in something simple is really a good idea in general.

    anyway it seems the image I have stored in resources isn't showing. which is the problem with my game.

    it must follow that this relational address means nothing to the applet. Must I keep the resources in the same folder and use getClass.getResource()?

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

    Default Re: What happened to my graphics? -converting to applet

    Your problem is that you're using the ImageIcon(String filename) constructor when files won't exist in your app. Instead you should either use the URL constructor or get your Image with ImageIO.read(InputStream input). Repeat after me: files don't exist in jar files.

  5. #5
    Codeless is offline Member
    Join Date
    Nov 2010
    Posts
    37
    Rep Power
    0

    Default Re: What happened to my graphics? -converting to applet

    ...files don't exist in jar files...

    I get easily confused when it comes to computers locating things. I just sort of hope it will and leave it at that... that's why I'm not good with Linux I guess.

    so let me try to make sense of this. I use this forum because it says beginners...

    an applet will be a set of classes and resource files inside a jar archive.
    a class can not locate files in a jar archive because the files have been reduced to data
    so to use that data we need to encapsulate it in a inputstream and then perform operations on it? ImageIO.read(InputStream input)
    so how would I make that inputstream reference the image file(data) I want in the jar archive?

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

    Default Re: What happened to my graphics? -converting to applet

    By getting the image as a resource as you've mentioned above. But there's a difference in the relative path names that you will need to use since resources are located relative to the class files, not the user.dir.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,006
    Rep Power
    20

    Default Re: What happened to my graphics? -converting to applet

    getResource returns a URL, so you can use that with the ImageIcon constructor that takes a URL.
    If you're already using that constructor that would probably be the least amount of refactoring.

    As Fubarable says, the "path" is relative to the class, unless you start it with a "/", in which case it will be the "root" of the jar file.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Need Help Converting This to Applet Plz
    By RecursiveEndUserFromMars in forum Java Applets
    Replies: 2
    Last Post: 01-19-2011, 04:10 AM
  2. Converting java to applet...?
    By xtrapx in forum Java Applets
    Replies: 1
    Last Post: 10-16-2010, 05:38 PM
  3. Converting this Applet to an Application
    By karthus in forum New To Java
    Replies: 4
    Last Post: 07-07-2010, 04:30 AM
  4. Converting an App to Applet
    By josephdcoleman in forum New To Java
    Replies: 1
    Last Post: 02-21-2009, 07:07 AM
  5. Converting netbeans app to applet
    By paulious in forum New To Java
    Replies: 3
    Last Post: 10-23-2008, 11:25 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
  •