|
Simple Applet Program Example shows to create java 2d drawing program (graphics2d java) mix with java swing code. You can directly apply this applet to your website if you like.
Step 1: Create Applet
DrawingTool.java
//copyrighted by topsourcecode.com
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class DrawingTool extends JApplet implements MouseMotionListener
{
int width, height;
Image newImage;
Graphics newGraphics;
public void init()
{
width = getSize().width;
height = getSize().height;
newImage = createImage( width, height );
newGraphics = newImage.getGraphics();
newGraphics.setColor( Color.black );
newGraphics.fillRect( 0, 0, width, height );
newGraphics.setColor( Color.CYAN );
addMouseMotionListener( this );
}
public void mouseMoved( MouseEvent e ) { }
public void mouseDragged( MouseEvent e )
{
int x = e.getX();
int y = e.getY();
newGraphics.fillOval(x-6,y-6,10,10);
repaint();
e.consume();
}
public void update( Graphics g )
{
g.drawImage( newImage, 0, 0, this );
}
public void paint( Graphics g )
{
update( g );
}
}
Step 2: Compile the java applet
Javac DrawingTool.java
Step 3: Add applet code to html (Html file should in same folder of the java class file)
x.html
<html>
<head>
<applet code="DrawingTool.class"
width="600" height="500">
Your browser is completely ignoring the <applet> tag!
</applet>
</head>
<body>
</body>
</html>
Step 4: Run and test
Result:

|