Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[JAVA] Introduction to Java Applets. filter_list
Author
Message
[JAVA] Introduction to Java Applets. #2
Introduction to Java Applets.


I'm assuming you already know the basics of java, because it would be really hard to understand this before knowing the basics.

What are Java Applets:
An applet is a small program that is intended to be embedded inside another application such as a browser. They are stored as .class extension. The JApplet class must be the superclass of any applet that is to be embedded in a Web page or viewed by the Java Applet Viewer (appletviewer.exe). The JApplet class provides a standard interface between applets and their environment. JApplet hierarchy is as follows:
Code:
java.lang.Object
      java.awt.Component
        java.awt.Container
          java.awt.Panel
            java.applet.Applet
              javax.swing.JApplet

Now, applets can be used for hacking too, but we are not there to discuss that. Lets make something creative.

Applets Structure:
Code:
- There is no main method.
- Two methods that are called automatically- init() and paint()
- The init method initializes variables and objects; if you don't have one you will inherit one from the JApplet class.
- Use paint to draw screen
- A lot of methods exist in JApplet class so the "extends" keyword inherits everything that the class has. In the above example JApplet is parent class and shellapplet is the subclass so use the keyword "extends" to create inheritance.
- You have to import JApplet and java.awt.Graphics (abstract windowing toolkit) to get Graphics to paint.
- All applets must inherit JApplet
- Use the "super" keyword in subclass to invoke method in the superclass.
- super.paint( g ); this says uses the paint method from JApplet in my paint class and I'm not adding anything to it.

Lets start with some programs.

Drawing Lines:
Code:
import java.applet.*;
    import java.awt.*;

    public class DrawingLines extends Applet {

       int width, height;

       public void init() {
          width = getSize().width;
          height = getSize().height;
          setBackground( Color.black );
       }

       public void paint( Graphics g ) {
          g.setColor( Color.green );
          for ( int i = 0; i < 10; ++i ) {
             g.drawLine( width, height, i * width / 10, 0 );
          }
       }
    }
Spoiler:
[Image: unledbum.png]


Color:
Code:
import java.applet.*;
import java.awt.*;

public class Graphic extends Applet {

   int width, height;
   int N = 25;          // the number of colors created
   Color[] spectrum;    // an array of elements, each of type Color
   Color[] spectrum2;   // another array

   public void init() {
      width = getSize().width;
      height = getSize().height;
      setBackground( Color.black );

      // Allocate the arrays; make them "N" elements long
      spectrum = new Color[ N ];
      spectrum2 = new Color[ N ];

      // Generate the colors and store them in the arrays.
      for ( int i = 1; i <= N; ++i ) {
         // The three numbers passed to the Color() constructor
         // are RGB components in the range [0,1].
         // The casting to (float) is done so that the divisions will be
         // done with floating point numbers, yielding fractional quotients.

         // As i goes from 1 to N, this color goes from almost black to white.
         spectrum[ i-1 ] = new Color( i/(float)N, i/(float)N, i/(float)N );

         // As i goes from 1 to N, this color goes from almost pure green to pure red.
         spectrum2[ i-1 ] = new Color( i/(float)N, (N-i)/(float)N, 0 );
      }
   }

   public void paint( Graphics g ) {

      int step = 90 / N;
      for ( int i = 0; i < N; ++i ) {
         g.setColor( spectrum[ i ] );
         g.fillArc( 0, 0, 2*width, 2*height, 90+i*step, step+1 );

         g.setColor( spectrum2[ i ] );
         g.fillArc( width/3, height/3, 4*width/3, 4*height/3, 90+i*step, step+1 );
      }
   }
}
Spoiler:
[Image: unled1vh.png]


3D:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.Math;

class Point3D {
   public int x, y, z;
   public Point3D( int X, int Y, int Z ) {
      x = X;  y = Y;  z = Z;
   }
}

class Edge {
   public int a, b;
   public Edge( int A, int B ) {
      a = A;  b = B;
   }
}

public class WireframeViewer extends Applet
   implements MouseListener, MouseMotionListener {

   int width, height;
   int mx, my;  // the most recently recorded mouse coordinates

   Image backbuffer;
   Graphics backg;

   int azimuth = 35, elevation = 30;

   Point3D[] vertices;
   Edge[] edges;

   public void init() {
      width = getSize().width;
      height = getSize().height;

      vertices = new Point3D[ 8 ];
      vertices[0] = new Point3D( -1, -1, -1 );
      vertices[1] = new Point3D( -1, -1,  1 );
      vertices[2] = new Point3D( -1,  1, -1 );
      vertices[3] = new Point3D( -1,  1,  1 );
      vertices[4] = new Point3D(  1, -1, -1 );
      vertices[5] = new Point3D(  1, -1,  1 );
      vertices[6] = new Point3D(  1,  1, -1 );
      vertices[7] = new Point3D(  1,  1,  1 );

      edges = new Edge[ 12 ];
      edges[ 0] = new Edge( 0, 1 );
      edges[ 1] = new Edge( 0, 2 );
      edges[ 2] = new Edge( 0, 4 );
      edges[ 3] = new Edge( 1, 3 );
      edges[ 4] = new Edge( 1, 5 );
      edges[ 5] = new Edge( 2, 3 );
      edges[ 6] = new Edge( 2, 6 );
      edges[ 7] = new Edge( 3, 7 );
      edges[ 8] = new Edge( 4, 5 );
      edges[ 9] = new Edge( 4, 6 );
      edges[10] = new Edge( 5, 7 );
      edges[11] = new Edge( 6, 7 );

      backbuffer = createImage( width, height );
      backg = backbuffer.getGraphics();
      drawWireframe( backg );

      addMouseListener( this );
      addMouseMotionListener( this );
   }

   void drawWireframe( Graphics g ) {

      // compute coefficients for the projection
      double theta = Math.PI * azimuth / 180.0;
      double phi = Math.PI * elevation / 180.0;
      float cosT = (float)Math.cos( theta ), sinT = (float)Math.sin( theta );
      float cosP = (float)Math.cos( phi ), sinP = (float)Math.sin( phi );
      float cosTcosP = cosT*cosP, cosTsinP = cosT*sinP,
             sinTcosP = sinT*cosP, sinTsinP = sinT*sinP;

      // project vertices onto the 2D viewport
      Point[] points;
      points = new Point[ vertices.length ];
      int j;
      int scaleFactor = width/4;
      float near = 3;  // distance from eye to near plane
      float nearToObj = 1.5f;  // distance from near plane to center of object
      for ( j = 0; j < vertices.length; ++j ) {
         int x0 = vertices[j].x;
         int y0 = vertices[j].y;
         int z0 = vertices[j].z;

         // compute an orthographic projection
         float x1 = cosT*x0 + sinT*z0;
         float y1 = -sinTsinP*x0 + cosP*y0 + cosTsinP*z0;

         // now adjust things to get a perspective projection
         float z1 = cosTcosP*z0 - sinTcosP*x0 - sinP*y0;
         x1 = x1*near/(z1+near+nearToObj);
         y1 = y1*near/(z1+near+nearToObj);

         // the 0.5 is to round off when converting to int
         points[j] = new Point(
            (int)(width/2 + scaleFactor*x1 + 0.5),
            (int)(height/2 - scaleFactor*y1 + 0.5)
         );
      }

      // draw the wireframe
      g.setColor( Color.black );
      g.fillRect( 0, 0, width, height );
      g.setColor( Color.white );
      for ( j = 0; j < edges.length; ++j ) {
         g.drawLine(
            points[ edges[j].a ].x, points[ edges[j].a ].y,
            points[ edges[j].b ].x, points[ edges[j].b ].y
         );
      }
   }

   public void mouseEntered( MouseEvent e ) { }
   public void mouseExited( MouseEvent e ) { }
   public void mouseClicked( MouseEvent e ) { }
   public void mousePressed( MouseEvent e ) {
      mx = e.getX();
      my = e.getY();
      e.consume();
   }
   public void mouseReleased( MouseEvent e ) { }
   public void mouseMoved( MouseEvent e ) { }
   public void mouseDragged( MouseEvent e ) {
      // get the latest mouse position
      int new_mx = e.getX();
      int new_my = e.getY();

      // adjust angles according to the distance travelled by the mouse
      // since the last event
      azimuth -= new_mx - mx;
      elevation += new_my - my;

      // update the backbuffer
      drawWireframe( backg );

      // update our data
      mx = new_mx;
      my = new_my;

      repaint();
      e.consume();
   }

   public void update( Graphics g ) {
      g.drawImage( backbuffer, 0, 0, this );
      showStatus("Elev: "+elevation+" deg, Azim: "+azimuth+" deg");
   }

   public void paint( Graphics g ) {
      update( g );
   }
}
Spoiler:
[Image: unled2lo.png]


Painting:
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class Brush1 extends Applet
   implements MouseMotionListener {

   int width, height;
   Image backbuffer;
   Graphics backg;

   public void init() {
      width = getSize().width;
      height = getSize().height;

      backbuffer = createImage( width, height );
      backg = backbuffer.getGraphics();
      backg.setColor( Color.black );
      backg.fillRect( 0, 0, width, height );
      backg.setColor( Color.white );

      addMouseMotionListener( this );
   }

   public void mouseMoved( MouseEvent e ) { }
   public void mouseDragged( MouseEvent e ) {
      int x = e.getX();
      int y = e.getY();
      backg.fillOval(x-5,y-5,10,10);
      repaint();
      e.consume();
   }

   public void update( Graphics g ) {
      g.drawImage( backbuffer, 0, 0, this );
   }

   public void paint( Graphics g ) {
      update( g );
   }
}
Spoiler:
[Image: unled3bdd.png]
Try clicking and dragging on the Applet Viewer, you could use your mouse to draw and thats why they named it Painting. I typed 1337(see the below image). This is how your Paint or Photoshop works =)
Spoiler:
[Image: unled4al.png]


Guidelines and Pro-Tips:
1. Its impossible to explain these programs since they are too enormous. This is why a basic understanding of java is required.

2. Now you can try these programs on your pc itself, try playing with it. Change the colors, size and try different ways. Thats how you will learn.

3. http://www.realapplets.com/ Register here and find pretty much a lot of stuff to play with.

4. If you need any kind of help, post in this thread, I will try my best to help.

I didnt make these programs myself, I had them in my Programming folder, so i used them and the credit goes to whoever made it. It would have taken a some time to make them myself which I dont have.
Spoiler:
[Image: unledbyds.png]

Reply





Messages In This Thread
[JAVA] Introduction to Java Applets. - by 1337 - 07-14-2011, 08:52 AM
[JAVA] Introduction to Java Applets. - by 1337 - 07-14-2011, 08:52 AM



Users browsing this thread: 2 Guest(s)