This is a very simple drawing applet in Java. The left hand side of the applet has a Canvas and the right hand side of the applet has buttons to allow you to choose to draw lines, squares or circles. The source code is detailed beneath.

Circles

Lines

Squares

import java.awt.*;
import java.awt.event.*;
import java.applet.*;

public class PaintApplet extends Applet
{
 //declaring the buttons

 private Button line;
 private Button square;
 private Button circle;

 private Panel panel;
 private Canvas canvas;

 //initialise  the applet

 public void init()
 {
 setLayout(new BorderLayout());

 //name of buttons

 line = new Button("LINE");
 square = new Button("SQUARE");
 circle = new Button("CIRCLE");

 //activating the buttons

 line.addActionListener(new PaintButtonListener());
 square.addActionListener(new PaintButtonListener());
 circle.addActionListener(new PaintButtonListener());

 //setting colour of canvas

 canvas = new Canvas();
 canvas.setBackground(new Color(255, 255, 255));
 canvas.setForeground(new Color(0, 0, 0));

 //setting panel

 panel = new Panel(new GridLayout(3, 1));

 panel.add(line);
 panel.add(square);
 panel.add(circle);

 add(panel, "East");
 add(canvas, "Center");
 }

 //display the applet

 public void paint(Graphics g)
 {
 }

 private class PaintButtonListener implements ActionListener
 {
 private LineMouseListener ll = new LineMouseListener();
 private SquareMouseListener sl = new SquareListener();
 private CircleMouseListener cl = new CircleListener();

 public void actionPerformed(ActionEvent event)
 {
 //line condition
 if(event.getSource() == line)
 {    
 if(setCircle ==true)
 {
 setCircle = false;
 }

 if(setSquare == true)
 {
 setSquare = false;
 }                 

 setLine = true;
 canvas.addMouseListener(ll);

 }
 //square condition
 else if(event.getSource() == square)
 {    
 if(setCircle ==true)
 {
 setCircle = false;
 }

 if(setline == true)
 {
 setLine = false;
 }                 

 setSquare = true;
 canvas.addMouseListener(sl);

 canvas.addMouseListener(new SquareMouseListener());

 //circle condition
 if(event.getSource() == circle)
 {    
 if(setSquare ==true)
 {
 setSquare = false;
 }

 if(setLine == true)
 {
 setLine = false;
 }                 

 }

 }

 //line drawing
 private class LineMouseListener implements MouseListener
 {
 //initialise line
 private int startX = 0;
 private int startY = 0;
 private int endX = 0;
 private int endY = 0;

 public void mousePressed(MouseEvent e)
 {
 //start point
 if(setLine == true)
 {
 startX = e.getX();
 startY = e.getY();
 }
 }

 public void mouseReleased(MouseEvent e)
 {    
 //end point
 if(setline == true)            
 {
 endX =  e.getX();
 endY =  e.getY();
 }
 //Setting the colour
 Graphics g = canvas.getGraphics();
 g.setColor(new Color(125, 0, 0));
 g.drawLine(startX, startY, endX, endY);
 repaint();
 }
 //these must be included
 public void mouseExited(MouseEvent e)
 {
 }

 public void mouseEntered(MouseEvent e)
 {
 }

 public void mouseClicked(MouseEvent e)
 {
 }

 }
 //square drawing
 private class SquareMouseListener implements MouseListener
 {
 //initialise square
 private int startX = 0;
 private int startY = 0;
 private int endx = 0;
 private int endy = 0;
 private int width = 0;
 private int height = 0;

 public void mousePressed(MouseEvent e)
 {
 //start point
 if(setSquare == true)
 {
 startX = e.getX();
 starty = e.gety();
 }
 }

 public void mouseReleased(MouseEvent e)
 {
 //end point
 endx =  e.getx();
 endY =  e.getY();

 //get absolute value
 width = Math.abs(startX - e.getX());
 height = Math.abs(startY - e.getY());

 //setting the colour
 Graphics g = canvas.getGraphics();
 g.setColor(new Color(0, 50, 100));
 g.drawRect(startX, startY, width, height);
 repaint();
 }
 //these must be included
 public void mouseExited(MouseEvent e)
 {
 }

 public void mouseEntered(MouseEvent e)
 {
 }

 public void mouseClicked(MouseEvent e)
 {
 }
 }
 //circle drawing
 private class CircleMouseListener implements MouseListener
 {
 //initialise circle
 private int startx = 0;
 private int starty = 0;
 private int endx = 0;
 private int endy = 0;
 private int width = 0;
 private int height = 0;

 public void mousePressed(MouseEvent e)
 {
 //start point
 if(setCircle == true)
 {
 StartX = e.getX();
 StartY = e.getY();
 }    
 }
 public void mouseReleased(MouseEvent e)
 {
 //end point
 endx =  e.getx();
 endy =  e.gety();            

 width = Math.abs(startx - e.getx());
 height = Math.abs(starty - e.gety());

 //setting the colour
 Graphics g = canvas.getGraphics();
 g.setColor(new Color(0, 50, 0));
 g.drawOval(startx, starty, width, height);
 repaint();
 }

 //these must be included
 public void mouseExited(MouseEvent e)
 {
 }

 public void mouseEntered(MouseEvent e)
 {
 }

 public void mouseClicked(MouseEvent e)
 {
 }
 }
}
VN:F [1.8.8_1072]
Rating: 0.0/10 (0 votes cast)
VN:F [1.8.8_1072]
Rating: 0 (from 0 votes)