Showing posts with label Java Applet. Show all posts
Showing posts with label Java Applet. Show all posts

Wednesday, December 28, 2011

Using Applet dimension to print center aligned text

package org.best.example;
   
    /*
            Using Applet dimension to print center aligned text Example
            This Java example shows how to print text in center of an applet window using
            Dimension class.
    */
    
    import java.applet.Applet;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics;
    
    /*
            <applet code = "AppletDimensionExample" width = 500 height = 300>
            </applet>
    */
    
    public class AppletDimensionExample extends Applet{
          
            public void paint(Graphics g){
                    int x,y;
                    String s = "Hello World";
                  
                    //get applet size using getSize method
                    Dimension d = getSize();
                    Font f = new Font("Arial",Font.BOLD,24);
                    g.setFont(f);
                  
                    //determine x and y coordinates
                    FontMetrics fm = g.getFontMetrics();
                    x = d.width/2 - fm.stringWidth(s)/2;
                    y = d.height/2 - fm.getHeight();
                  
                    //print string at specified location using drawString method
                    g.drawString(s,x,y);
            }
    }

Tuesday, December 27, 2011

Set Status Message in Applet Window

package org.best.example;
   
    /*
            Set Status Message in Applet Window Example
            This java example shows how to set a status message of an applet window
            using showStatus method of an Applet class.
    */
    
    /*
    <applet code="SetStatusMessageExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class SetStatusMessageExample extends Applet{
    
            public void paint(Graphics g){
                    /*
                     * Show status message in an Applet window using
                     * void showStatus(String msg) method of an applet class.
                     */
                  
                    //this will be displayed inside an applet
                    g.drawString("Show Status Example", 50, 50);
                  
                    //this will be displayed in a status bar of an applet window
                    showStatus("This is a status message of an applet window");
            }
    }

Monday, December 26, 2011

Set Foreground Color Of an Applet Window

package org.best.example;
   
    /*
            Set Foreground Color Of an Applet Window Example
            This java example shows how to set foreground color of an Applet window using
            setForeground method of component class.
    */
    
    /*
    <applet code="SetForegroundColorExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class SetForegroundColorExample extends Applet{
    
            public void paint(Graphics g){
                    /*
                     * Set foreground color of an applet using
                     * void setForeground(Color c) method.
                     */
                  
                    setForeground(Color.red);
                    g.drawString("Foreground color set to red", 50, 50);
            }
    }

Sunday, December 25, 2011

Set Background Color Of an Applet Window

package org.best.example;

    /*
            Set Background Color Of an Applet Window Example
            This java example shows how to set background color of an Applet window using
            setBackground method of component class.
    */
    
    /*
    <applet code="SetBackgroundColorExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class SetBackgroundColorExample extends Applet{
    
            public void paint(Graphics g){
                    /*
                     * Set background color of an applet using
                     * void setBackground(Color c) method.
                     */
                  
                    setBackground(Color.red);
            }
    }

Saturday, December 24, 2011

Resize Applet Window

package org.best.example;

    /*
            Resize Applet Window Example
            This java example shows how to resize an aplet window using resize method
            of an applet class.
    */
    
    /*
    <applet code="ResizeAppletWindowExample" width=200 height=200>
    </applet>
    */
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class ResizeAppletWindowExample extends Applet{
    
            public void paint(Graphics g){
                    /*
                     * To resize an applet window use,
                     * void resize(int x, int y) method of an applet class.
                     */
                    resize(300,300);
                    g.drawString("Window has been resized to 300,300", 50, 50);
            }
          
    }

Friday, December 23, 2011

Load New HTML File Using Applet Context

package org.best.example;

    /*
            Load New HTML File Using Applet Context Example
            This java example shows how load a new file in a browser using
            showDocument() method of AppletContext.
    */
    
    import java.applet.Applet;
    import java.applet.AppletContext;
    import java.net.MalformedURLException;
    import java.net.URL;
    
    /*
    <applet code="LoadNewHTMLFileExample" width=200 height=200>
    </applet>
    */
    
    
    public class LoadNewHTMLFileExample extends Applet{
    
            public void start(){
                  
                    //get code base of an Applet using getCodeBase() method
                    URL codeBase = getCodeBase();
                  
                    //get AppletContext using getAppletContext() method
                    AppletContext context = getAppletContext();
                  
                    /*
                     * To load a new HTML file in a browser use
                     * void showDocument(URL url)
                     * method of AppletContext class.
                     *
                     * PLEASE NOTE that the new HTML document must be in the
                     * same directory.
                     */
                  
                    try{
                          
                            URL url = new URL(codeBase + "AppletContextExample.html");
                            context.showDocument(url);
                          
                    }catch(MalformedURLException me){
                            //do nothing
                    }
            }
    }

Wednesday, December 21, 2011

Get Foreground Color Of an Applet Window

package org.best.example;

    /*
            Get Foreground Color Of an Applet Window Example
            This java example shows how to get foreground color of an Applet window using
            getForeground method of component class.
    */
    
    /*
    <applet code="GetForegroundColorExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    
    public class GetForgroundColorExample extends Applet{
    
            public void paint(Graphics g){
                    /*
                     * Get Foreground color of an applet using
                     * Color getForeground() method of Component class.
                     */
                  
                    Color c = getForeground();
                    g.drawString(c.toString(),50,50);
            }
    }

Tuesday, December 20, 2011

Get Background Color Of an Applet Window

package org.best.example;

    /*
            Get Background Color Of an Applet Window Example
            This java example shows how to get background color of an Applet window using
            getBackground method of component class.
    */
    
    /*
    <applet code="GetBackGroundColorExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class GetBackGroundColorExample extends Applet{
          
            public void paint(Graphics g){
                    //get background color using getBackground method
                    Color c = getBackground();
                    g.drawString(c.toString(),50,50);
            }
    }

Monday, December 19, 2011

Get Applet's Document URL or Directory Base

package org.best.example;

    /*
            Get Applet's Document URL or Directory Base Example
            This java example shows how get the applet's HTML document URL or
            document base using getDocumentBase() method of Java Applet class.
    */         
    
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.net.URL;
    
    /*
    <applet code="GetDocumentBaseExample" width=200 height=200>
    </applet>
    */
    
    public class GetDocumentBaseExample extends Applet{
    
            public void paint(Graphics g){
                  
                    /*
                     * To get Applet's HTML Document directory URL or the document base use
                     * String getDocumentBase()
                     * method of Java Applet class.
                     */
                  
                    URL appletDocDir = getDocumentBase();
                    g.drawString(appletDocDir.toString(), 50, 50);
                  
            }
    }

Sunday, December 18, 2011

Get Applet parameter

package org.best.example;

    /*
            Get Applet parameter Example
            This java example shows how get the parameter passed to an applet using
            getParameter() method of Java Applet class.
    */
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    /*
     * To pass a parameter to an Applet use <param> HTML tag.
     * Specify name and value attribute to pass a parameter to an applet like
     * given below.
     *
     * <param name="paramName" value="paramValue"/>
     *
     * You can pass multiple parameters for an Applet using multiple
     * <param> HTML tag.
     */
    
    /*
    <applet code="GetAppletParameterExample" width=200 height=200>
            <param name="msg" value="This is a parameter example program"/>
            <param name="xPosition" value="50"/>
            <param name="yPosition" value="50"/>
    </applet>
    */
    
    public class GetAppletParameterExample extends Applet{
    
            public void paint(Graphics g){
                  
                    int x = 0;
                    int y = 0;
                    String msg = "";
                  
                    try{
                            x = Integer.parseInt(getParameter("xPosition"));
                    }
                    catch(NumberFormatException ne){
                            msg = msg + "Invalid x Value";
                    }
                  
                    try{
                            y = Integer.parseInt(getParameter("yPosition"));
                    }
                    catch(NumberFormatException ne){
                            msg = msg + "Invalid y Value";
                    }
                  
                    msg = getParameter("msg");
                  
                    g.drawString(msg, x, y);
            }
    }

Saturday, December 17, 2011

Get Applet Info

package org.best.example;

    /*
            Get Applet Info Example
            This java example shows how get applet information using getAppletInfo method
            of an Applet class.
    */
    
    /*
    <applet code="GetAppletInfoExample" width=400 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class GetAppletInfoExample extends Applet{
    
            public void paint(Graphics g){
                  
                    String info = getAppletInfo();
                    g.drawString(info,50,50);
            }
          
            /*
             * Applet class provides default implementation of getAppletInfo()
             * method which returns null.
             *
             * Your Applet class should override this method to provide useful
             * information like applet name,author name and copyright information.
             */
          
            public String getAppletInfo(){
                    String info = "";
                  
                    info = info + "GetAppletInfoExample";
                    info = info + "@java-examples.com";
                  
                    return info;
            }
    }

Friday, December 16, 2011

Get Applet Directory URL or Code Base

package org.best.example;

    /*
            Get Applet Directory URL or Code Base Example
            This java example shows how get the current applet's directory URL or
            code base using getCodeBase() method of Java Applet class.
    */
            
    import java.applet.Applet;
    import java.awt.Graphics;
    import java.net.URL;
    
    /*
    <applet code="GetCodeBaseExample" width=200 height=200>
    </applet>
    */
    
    public class GetCodeBaseExample extends Applet{
    
            public void paint(Graphics g){
                  
                    /*
                     * To get Applet directory URL or the code base use
                     * String getCodeBase()
                     * method of Java Applet class.
                     */
                  
                    URL appletDir = getCodeBase();
                    g.drawString(appletDir.toString(), 50, 50);
                  
            }
    }

Thursday, December 15, 2011

Get Applet Context

package org.best.example;

    /*
            Get Applet Context Example
            This java example shows how get applet context object using getAppletContext
            method of Java Applet class.
    */
    
    /*
    <applet code="GetAppletContextExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.applet.AppletContext;
    
    public class GetAppletContextExample extends Applet{
    
            public void init(){
                    /*
                     * To get Applet context object use
                     * AppletContext getAppletContext() method.
                     */
                  
                    AppletContext appContext = getAppletContext();
                                  
            }
    }

Wednesday, December 14, 2011

Generate Bouncing Lines Using Applet

package org.best.example;

    /*
            Generate Bouncing Lines Using Applet Example
            This Java example shows how to create bouncing lines using java Applet
            example.
    */
    
    import java.awt.*;
    import java.applet.Applet;
    
    public class BouncingLines extends Applet implements Runnable {
    
        Thread runner = null;
    
        final static int WIDTH  = 200;
        final static int HEIGHT = 100;
    
        Image    image;
        Graphics graphics;
    
        // bouncing lines member variables
        int[] x1;
        int[] y1;
        int[] x2;
        int[] y2;
    
        int dx1 = 2 + (int)( 3 * Math.random() );
        int dy1 = 2 + (int)( 3 * Math.random() );
        int dx2 = 2 + (int)( 3 * Math.random() );
        int dy2 = 2 + (int)( 3 * Math.random() );
        static int first = 0;
        final static int LINES = 50;
    
        public void init() {
    
            // create arrays to hold the line coordinates
            x1 = new int[LINES];
            y1 = new int[LINES];
            x2 = new int[LINES];
            y2 = new int[LINES];
    
            // initialise the first line
            x1[0] = (int)( WIDTH  * Math.random() );
            y1[0] = (int)( HEIGHT * Math.random() );
            x2[0] = (int)( WIDTH  * Math.random() );
            y2[0] = (int)( HEIGHT * Math.random() );
    
            // initialise all the other lines
           for ( int i = 1; i < LINES; i++ ) {
    
                x1[i] = x1[0];
                y1[i] = y1[0];
                x2[i] = x2[0];
                y2[i] = y2[0];
            }
            image    = createImage( WIDTH, HEIGHT );
            graphics = image.getGraphics();
        }
    
    
        public void start() {
    
            // user visits the page, create a new thread
           if ( runner == null ) {
    
                runner = new Thread( this );
                runner.start();
            }
        }
    
    
        public void stop() {
    
            // user leaves the page, stop the thread
           if ( runner != null && runner.isAlive() )
                runner.stop();
    
            runner = null;
        }
    
    
        public void run() {
    
            while (runner != null) {
    
                repaint();
    
                try {
    
                    Thread.sleep( 20 );
    
                } catch ( InterruptedException e ) {
    
                    // do nothing
                }
            }
        }
    
    
        public void paint( Graphics g ) {
    
            update( g );
        }
    
    
        public void update( Graphics g ) {
    
            // clear the background to white
    
            graphics.setColor( Color.black );
            graphics.fillRect( 0, 0, WIDTH, HEIGHT );
    
            // draw the lines
            for(int r=4;r<=9;r++)
            {
                  
                    graphics.setColor( Color.green );
          
                    int line = first;
          
                    for ( int i = 0; i < LINES; i++ ) {
          
                        graphics.drawLine( x1[line], y1[line],
                                           x2[line], y2[line] );
                        line++;
                        if ( line == LINES ) line = 0;
                    }
          
                    line = first;
          
                    first--;
          
                    if ( first < 0 ) first = LINES - 1;
          
                    x1[first] = x1[line];
                    y1[first] = y1[line];
                    x2[first] = x2[line];
                    y2[first] = y2[line];
          
                    // move the "first" line
          
                    if (x1[first] + dx2 < WIDTH)
                        x1[first] += dx1;
                    else
                        dx1 = -(2 + (int)( 3 * Math.random() ));
          
                    if (x1[first] + dx1 >= 0)
                        x1[first] += dx1;
                    else
                        dx1 = 2 + (int)( 3 * Math.random() );
          
                    if (y1[first] + dy1 < HEIGHT)
                        y1[first] += dy1;
                    else
                        dy1 = -(2 + (int)( 3 * Math.random() ));
          
                    if (y1[first] + dy1 >= 0)
                        y1[first] += dy1;
                    else
                        dy1 = 2 + (int)( 3 * Math.random() );
          
                    if (x2[first] + dx2 < WIDTH)
                        x2[first] += dx2;
                    else
                        dx2 = -(2 + (int)( 3 * Math.random() ));
          
                    if (x2[first] + dx2 >= 0)
                        x2[first] += dx2;
                    else
                        dx2 = 2 + (int)( 3 * Math.random() );
          
                    if (y2[first] + dy2 < HEIGHT)
                        y2[first] += dy2;
                    else
                        dy2 = -(2 + (int)( 3 * Math.random() ));
          
                    if (y2[first] + dy2 >= 0)
                        y2[first] += dy2;
                    else
                        dy2 = 2 + (int)( 3 * Math.random() );
          
                    // copy buffer to screen
                    g.drawImage( image, 0, 0, this );
                }
        }
    }

Tuesday, December 13, 2011

Fill Rectangle & Square in Applet Window

package org.best.example;

    /*
            Fill Rectangle & Square in Applet Window Example
            This java example shows how to draw filled rectangles and squares in an
            applet window using fillRect method of Graphics class.
    */
    
    /*
    <applet code="FilledRectangleExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class FilledRectangleExample extends Applet{
    
            public void paint(Graphics g){
                  
                    //set color to red
                    setForeground(Color.red);
                  
                    /*
                     * to draw a filled rectangle in an applet window use,
                     * void fillRect(int x1,int y1, int width, int height)
                     * method.
                     *
                     * This method draws a filled rectangle of specified width and
                     * height at (x1,y1)
                     */
                  
                    //this will draw a filled rectangle of width 50 & height 100 at (10,10)
                    g.fillRect(10,10,50,100);
                  
                    /*
                     * If you speficy same width and height, the fillRect method
                     * will draw a filled square!
                     */
                  
                    //this will draw a filled square
                    g.fillRect(100,100,50,50);
            }
    
    }

Monday, December 12, 2011

Draw Smiley In Applet

package org.best.example;

    /*
            Draw Smiley In Applet Example
            This Java example shows how to to draw smiley in an applet window using
            drawString Java Applet class.
    */
    
    
    import java.awt.*;
    import java.applet.*;
    
    public class Smiley extends Applet{
          
            public void paint(Graphics g){
                  
                    Font f = new Font("Helvetica", Font.BOLD,20);
                    g.setFont(f);
                    g.drawString("Keep Smiling!!!", 50, 30);
                    g.drawOval(60, 60, 200, 200);
                    g.fillOval(90, 120, 50, 20);
                    g.fillOval(190, 120, 50, 20);
                    g.drawLine(165, 125, 165, 175);
                    g.drawArc(110, 130, 95, 95, 0, -180);
            }
    }

Sunday, December 11, 2011

Draw Rounded Corner Rectangle & Square in Applet Window

package org.best.example;

    /*
            Draw Rounded Corner Rectangle & Square in Applet Window Example
            This java example shows how to draw rounded corner rectangles and squares
            in an applet window using drawRoundRect method of Graphics class. It also
            shows how to draw a filled rounded corner rectangles and squares using
            fillRoundRect method of Graphics class.
    */
    
    /*
    <applet code="DrawRoundedRectExample" width=500 height=500>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class DrawRoundedRectExample extends Applet{
    
            public void paint(Graphics g){
                  
                    //set color to red
                    setForeground(Color.red);
                  
                    /*
                     * to draw a rounded corner rectangle in an applet window use,
                     * void drawRoundRect(int x1,int y1, int width, int height, int arcWidth, int arcHeight)
                     * method.
                     *
                     * This method draws a rounded corner rectangle of specified width and
                     * height at (x1,y1)
                     */
                  
                    //this will draw a round cornered rectangle of width 50 & height 100 at (10,10)
                    g.drawRoundRect(10,10,50,100,10,10);
                  
                    /*
                     * If you speficy same width and height, the drawRoundRect method
                     * will draw a round cornered square!
                     */
                  
                    //this will draw a round cornered square
                    g.drawRoundRect(100,100,50,50,10,10);
                  
                    /*
                     * To draw a filled round cornered rectangle or square use
                     * fillRoundRect(int x1,int y1, int width, int height, int arcWidht, int arcHeight)
                     * method of Graphics class.
                     */
                  
                    //set color to blue
                    //setForeground(Color.blue);
                  
                    //draw filled rounded corner rectangle
                    g.fillRoundRect(200,20,50,100,10,10);
                  
                    //draw filled square
                    g.fillRoundRect(200,200,50,50,10,10);
            }
    }

Saturday, December 10, 2011

Draw Rectangle & Square in Applet Window

package org.best.example;

    /*
            Draw Rectangle & Square in Applet Window Example
            This java example shows how to draw rectangles and squares in an applet
            window using drawRect method of Graphics class.
    */
    
    /*
    <applet code="DrawRectanglesExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    public class DrawRectanglesExample extends Applet{
    
            public void paint(Graphics g){
                  
                    /*
                     * to draw rectangle in an applet window use,
                     * void drawRect(int x1,int y1, int width, int height)
                     * method.
                     *
                     * This method draws a rectangle of specified width and
                     * height at (x1,y1)
                     */
                  
                    //this will draw a rectangle of width 50 & height 100 at (10,10)
                    g.drawRect(10,10,50,100);
                  
                    /*
                     * If you speficy same width and height, the drawRect method
                     * will draw a square!
                     */
                  
                    //this will draw a square
                    g.drawRect(100,100,50,50);
            }
    }

Friday, December 9, 2011

Draw Oval & Circle in Applet Window

package org.best.example;

    /*
            Draw Oval & Circle in Applet Window Example
            This java example shows how to draw ovals & circles in an applet window using
            drawOval method of Graphics class. It also shows how to draw a filled
            ovals and circles using fillOval method of Graphics class.
    */
    
    /*
    <applet code="DrawOvalsExample" width=500 height=500>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class DrawOvalsExample extends Applet{
    
            public void paint(Graphics g){
                  
                    //set color to red
                    setForeground(Color.red);
                  
                    /*
                     * to draw a oval in an applet window use,
                     * void drawOval(int x1,int y1, int width, int height)
                     * method.
                     *
                     * This method draws a oval of specified width and
                     * height at (x1,y1)
                     */
                  
                    //this will draw a oval of width 50 & height 100 at (10,10)
                    g.drawOval(10,10,50,100);
                  
                  
                    /*
                     * To draw a filled oval use
                     * fillOval(int x1,int y1, int width, int height)
                     * method of Graphics class.
                     */
                  
                    //draw filled oval
                    g.fillOval(100,20,50,100);
                  
            }
    }

Thursday, December 8, 2011

Draw Line in Applet Window

package org.best.example;

    /*
            Draw Line in Applet Window Example
            This java example shows how to draw lines in an applet window using
            drawLine method of Graphics class.
    */
    
    /*
    <applet code="DrawLineExample" width=200 height=200>
    </applet>
    */
    
    
    import java.applet.Applet;
    import java.awt.Graphics;
    
    
    public class DrawLineExample extends Applet{
    
            public void paint(Graphics g){
                  
                    /*
                     * to draw line in an applet window use,
                     * void drawLine(int x1,int y1, int x2, int y2)
                     * method.
                     *
                     * This method draws a line between (x1,y1) and (x2,y2)
                     * coordinates in a current color.
                     */
                  
                    //this will draw a line between (10,10) and (50,50) coordinates.
                    g.drawLine(10,10,50,50);
                  
                    //draw vertical line
                    g.drawLine(10,50,10,100);
                  
                    //draw horizontal line
                    g.drawLine(10,10,50,10);
            }
    }