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 6 of 6

Thread: Painfully simple Applet problem.

  1. #1
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Painfully simple Applet problem.

    Hey all, I'm trying to learn how to build applets, and I simply can't get my browser to run one! Originally I was using Chrome, but I switched to Firefox and got the same error.

    Here's the applet trying to load: ImageShack® - Online Photo and Video Hosting

    I "click for details" and this window comes up: ImageShack® - Online Photo and Video Hosting

    I click "details" and this comes up: ImageShack® - Online Photo and Video Hosting

    Unfortunately, I have no idea what the problem means....

    Here's my applet code and my html code:
    import java.applet.*;
    import java.awt.*;
     
    class MyApplet extends Applet {
     
    	Font bigFont;
    	Color whiteColor;
     
    	public void init() {
    		bigFont = new Font("Arial", Font.BOLD, 16);
    		whiteColor = Color.white;
    		setBackground(whiteColor);
    	}
     
    	public void paint(Graphics g) {
    		g.setFont(bigFont);
    		g.drawString("Austin Rose", 80, 20);
    	}
     
    	public void stop() {
     
    	}
    }

    <HTML>
     
    <HEAD> 
    <TITLE> Austin's Applet </TITLE>
    </HEAD>
     
    <BODY>
    <applet code="MyApplet.class" width="300" height="300">
    </BODY>
     
    </HTML>

    Any help is greatly appreciated!


  2. #2
    Forum VIP
    Join Date
    Oct 2010
    Posts
    275
    My Mood
    Cool
    Thanks
    32
    Thanked 54 Times in 47 Posts
    Blog Entries
    2

    Default Re: Painfully simple Applet problem.

    I think it is saying that your class has to be public

  3. #3
    Junior Member
    Join Date
    Dec 2011
    Posts
    9
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painfully simple Applet problem.

    Changed "class MyApplet" to "public class MyApplet", still doesn't work :/

  4. #4
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: Painfully simple Applet problem.

    The error description says that it can not access the modifiers "" of MyApplet class. Well, i am not sure what exactly the problem is but try to comment
    bigFont = new Font("Arial", Font.BOLD, 16);
    this line and in paint() method draw something else and see.

  5. #5
    Forum VIP
    Join Date
    Jun 2011
    Posts
    317
    My Mood
    Bored
    Thanks
    47
    Thanked 89 Times in 74 Posts
    Blog Entries
    4

    Default Re: Painfully simple Applet problem.

    The problem is in your paint method. Try this instead.

        public void paint (Graphics g) {
     
            //Cast the Graphics object into a Graphics2D object
            Graphics g2 = (Graphics2D) g;
     
            // Draw the string
            g2.drawString("Hello", 10, 10);
     
            //Finished painting
            g2.finalize();
        }

    And here is a full template that works for me if you have any more problems.

    import java.applet.Applet;
    import java.awt.*;
     
    public class MyApplet extends Applet {
     
        public void init() {} 
     
        public void stop() {} 
     
        public void destroy() {} 
     
        public void paint (Graphics g) {
     
            //Cast the Graphics object into a Graphics2D
            Graphics g2 = (Graphics2D) g;
     
            // Draw the string
            g2.drawString("Hello", 10, 10);
     
            //Finished painting
            g2.finalize();
        }
     
        public void run () {
              repaint();
        }
     
    }
    Last edited by ChristopherLowe; January 4th, 2012 at 04:38 AM.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Painfully simple Applet problem.

    still doesn't work
    Please copy and paste here the contents of the java console that shows the error message.

Similar Threads

  1. Problem Applet Error
    By mohsendeveloper in forum Java SE APIs
    Replies: 24
    Last Post: January 19th, 2012, 04:00 PM
  2. Created simple game applet but wanting it to access a DSN on the server
    By hirsty in forum What's Wrong With My Code?
    Replies: 7
    Last Post: April 21st, 2011, 03:11 AM
  3. Applet problem with MouseEvent
    By smashX in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 10th, 2011, 03:24 PM
  4. [SOLVED] Help with simple Applet code
    By that_guy in forum What's Wrong With My Code?
    Replies: 13
    Last Post: January 11th, 2011, 10:22 PM
  5. applet and JPS problem.... please help me
    By rockster14 in forum Java Applets
    Replies: 0
    Last Post: August 7th, 2009, 03:59 PM