Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 8 of 8

Thread: Java Applet

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Applet

    Hi, I am writing a java applet to simply, display an image on my web browser. I can do it when I am only using one class, but now I wanted to write the program using two classes.

    ======CLASS: GAME======
    import java.awt.*;
    import java.applet.*;
     
    public class Game extends Applet{
     
    	Graphics g;
    	Image img;
    	MainMenu mainMenu;
     
    	public void init(){
    		mainMenu = new MainMenu();
    	}
     
    	public void paint(Graphics gfx){
    		mainMenu.draw(g);
    		gfx.drawImage(img,0,0,this);
    	}
    }

    ======CLASS: MAINMENU======
    import java.awt.*;
    import java.applet.*;
     
    public class MainMenu extends Applet{
     
    	Image img;
     
    	public void init(){
    		img=getImage(getDocumentBase(), getParameter("file"));
    	}
     
    	public void draw(Graphics g){
    		g.drawImage(img,0,0,this);
    	}
    }

    ======HTML======
    <applet code="Game" width=1000 height=550>
    <param name="file" value="Menu.png">
    </applet>

    If anyone has any advice for me I would really appreciate it. For this simple game, I was thinking each class would be responsible for drawing it's own images, that is why I do not want to draw this in the Game class.

    I tried looking in my Java text book, but they do not go far into this topic, and the online resources I have looked up have been hard to understand.

    Post if more details are needed!


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Applet

    I'm not exactly sure what your actual question is. Which part of this is giving you trouble?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Java Applet

    If you want two applets on the html page, add another <applet tag to your html for the MainMenu class.
    If you don't understand my answer, don't ignore it, ask a question.

  4. #4
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet

    My question is this: How do I display an image in my web browser involving at least 2 classes. One class acting as something like a HUB, and it calls to another class to draw that image.

    The code above is an attempt to do that, but it doesn't work.

    I am a new Java programmer so sorry if the question is confusing.

    The ultimate goal might be to make some kind of simple game.

    ---------------------

    Also,

    Norm: I didn't think of that, I'll give that a try also... but I can see how it might get sloppy if I end up with a lot of images.

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Applet

    So you only want one applet? Then only one class should extend Applet (or better yet, JApplet).

    Then you just instantiate whatever other classes you want and call them when appropriate. For example:

    public class AppletClass extends JApplet{
       PlayerClass p = new PlayerClass();
     
       public void paint(Graphics g){
          p.drawMe(g);
       }
     
    }
     
    public class PlayerClass{
       int x;
       int y;
     
       public void drawMe(Graphics g){
          g.drawOval(x, y, 10, 10);
       }
    }
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet

    KevinWorkman: I understand your example and I like it, but the part that is still confusing me is this: what would I replace g.drawOval(x, y, 10, 10); with in order to print an image I have on my hard drive?

  7. #7
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Java Applet

    Quote Originally Posted by Ludicrous View Post
    KevinWorkman: I understand your example and I like it, but the part that is still confusing me is this: what would I replace g.drawOval(x, y, 10, 10); with in order to print an image I have on my hard drive?
    Well, how do you draw an image now? Keep in mind that if you're running an applet, you're going to probably want to have the images be relative to the class files and not read from the hard drive (or better yet, you should package them all up into a jar).
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  8. #8
    Junior Member
    Join Date
    Mar 2012
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Java Applet

    I am an idiot. Problem solved - Thanks to everyone who replied

Similar Threads

  1. Java Applet Coding Help?
    By Drag01 in forum Java Applets
    Replies: 1
    Last Post: April 26th, 2012, 07:03 AM
  2. Java Applet Will Not Load.
    By PaulMoretti in forum What's Wrong With My Code?
    Replies: 3
    Last Post: August 17th, 2011, 05:42 AM
  3. 2D Java Applet Games
    By bbr201 in forum Java Theory & Questions
    Replies: 26
    Last Post: August 27th, 2010, 09:12 AM
  4. Applet using .mp3 control in java
    By ychopade in forum Java Applets
    Replies: 0
    Last Post: July 15th, 2010, 07:26 AM
  5. Replies: 1
    Last Post: June 25th, 2010, 06:59 AM