Results 1 to 5 of 5
  1. #1
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default Java Applet Not Running

    For some reason, my code won't compile. I don't understand why it won't. I'm new to Java & even newer to JApplets.

    Java Code:
    //AppletDrawingMain.java
    
    import javax.swing.JPanel;
    import javax.swing.JFrame;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JApplet;
    
    public class AppletDrawingMain extends JApplet
    {
        public static void main(String[] args)
        {
            //create frame
            JFrame frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    
            //declaring & adding dBoard to frame
            DrawingBoard dBoard = new DrawingBoard();
            frame.add(dBoard); 
    
            //frame paramaters
            frame.setSize(800, 800);
            frame.setVisible(true);
        }
    }

    Java Code:
    //DrawingBoard.java
    
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Graphics;
    import javax.swing.JPanel;
    import javax.swing.JApplet;
    
    //package drawinginapplet;
    
    public class DrawingBoard extends JApplet
    {
        public void paintComponent(Graphics g)
        {
            //call to superclass
            super.paintComponent(g);
    
            //upper left "Hello World" declaration
            g.setFont( new Font("Comic Sans", Font.PLAIN, 12) );
            g.drawString("Hello World", 0, 0);
    
            //lower right "I am here!" declaration
            g.setFont(new Font("Impact", Font.PLAIN, 10) );
            g.drawString("I am here!", 775, 800);
    
            //square with a black border and red fill
            g.setColor(Color.red);
            g.drawRect(15, 15, 50, 50);
            g.fillRect(15, 15, 50, 50);
    
            //rectangle with a blue border and no fill
            g.setColor(Color.white);
            g.drawRect(50, 50, 60, 130);
            g.fillRect(50, 50, 60, 130);
    
            //circle with a grey border and blue fill
            g.setColor(Color.blue);
            g.drawOval(80, 130, 30, 30);
            g.fillOval(80, 130, 30, 30);
    
            //oval with a pink border and no fill
            g.setColor(Color.PINK);
            g.drawOval(130, 150, 75, 25);
    
            //arch with a green border and yellow fill
            g.setColor(Color.yellow);
            g.fillArc(220, 220, 75, 75, 0, 180);
            g.drawArc(220, 220, 75, 75, 0, 180);
        }
    }

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Learn to read the error messages that the compiler gives you. They tell you the reason why the code doesn't compile. What error messages did you get?

  3. #3
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    Ahhh!

    When I had the following code,
    Java Code:
    //DrawingBoard.java
    
    import java.applet.*;
    import java.awt.Font;
    import java.awt.Color;
    import java.awt.Graphics;
    //import java.awt.Graphics2D;
    //import javax.swing.JPanel;
    //import javax.swing.JApplet;
    
    public class DrawingBoard extends Applet
    {
        public void init()
        {
            setSize(500,550);
            setVisible(true);
        }
    
        public void paint(Graphics g)
        {
            //call to superclass
            //super.paintComponent(g);
    
            //upper left "Hello World" declaration
            g.setFont( new Font("Comic Sans", Font.PLAIN, 12) );
            g.drawString("Hello World", 0, 0);
    
            //lower right "I am here!" declaration
            g.setFont(new Font("Impact", Font.PLAIN, 10) );
            g.drawString("I am here!", 775, 800);
    
            //square with a black border and red fill
            g.setColor(Color.black);
            g.drawRect(15, 15, 50, 50);
            g.setColor(Color.red);
            g.fillRect(16, 16, 49, 49);
    
            //rectangle with a blue border and no fill
            g.setColor(Color.blue);
            g.drawRect(50, 50, 60, 130);
    
            //circle with a gray border and blue fill
            g.setColor(Color.gray);
            g.drawOval(80, 130, 30, 30);
            g.setColor(Color.blue);
            g.fillOval(81, 131, 29, 29);
    
            //oval with a pink border and no fill
            g.setColor(Color.PINK);
            g.drawOval(130, 150, 75, 25);
    
            //arch with a green border and yellow fill
            g.setColor(Color.green);
            g.fillArc(220, 220, 75, 75, 0, 180);
            g.setColor(Color.yellow);
            g.drawArc(219, 219, 75, 75, 0, 180);
        }
    }
    and attempted to debug it, it told me that there was no main class. I added this:
    Java Code:
        public static void main(String[] args)
        {
    
        }
    I honestly didn't think that this was right, but was just trying out different things to get this applet running. It said that the build was successful, but it never displayed the applet. I am completely confused at this point.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    You are running the Applet as a normal Java application. Read the Applets tutorial from Sun to find out how to run Applets.

  5. #5
    kahaj is offline Member
    Join Date
    Sep 2009
    Posts
    12
    Rep Power
    0

    Default

    Yeah, I figured that out. Sorry for the bother & thanks. :)

Similar Threads

  1. Replies: 0
    Last Post: 08-26-2009, 01:03 PM
  2. [SOLVED] [newbie] running an applet ??
    By jon80 in forum New To Java
    Replies: 4
    Last Post: 05-31-2009, 09:42 PM
  3. Running applet from command prompt
    By niteshwar.bhardwaj in forum Java 2D
    Replies: 1
    Last Post: 03-12-2009, 08:10 AM
  4. First Applet not running on browsers
    By Centinela66 in forum Java Applets
    Replies: 11
    Last Post: 10-09-2008, 01:58 PM
  5. Getting url of page applet is running on
    By damounh in forum Java Applets
    Replies: 1
    Last Post: 05-09-2008, 05:49 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
  •