Category Archives: java
Deep and shallow cloning in Java
the uber api
Advanced Java tutorials
10 free books for java beginners
Animation tools
java2s
Docjar
tutorials point split
Code Java
java GUI - Snowman
Original Code:
//******************************************************************** // Snowman.java Author: Lewis/Loftus/Cocking // // Demonstrates basic drawing methods and the use of color. //******************************************************************** import java.applet.Applet; import java.awt.*; public class Snowman extends Applet { //----------------------------------------------------------------- // Draws a snowman. //----------------------------------------------------------------- public void paint (Graphics page) { final int MID = 150; final int TOP = 50; setBackground (Color.cyan); page.setColor (Color.blue); page.fillRect (0, 175, 300, 50); // ground page.setColor (Color.yellow); page.fillOval (-40, -40, 80, 80); // sun page.setColor (Color.white); page.fillOval (MID-20, TOP, 40, 40); // head page.fillOval (MID-35, TOP+35, 70, 50); // upper torso page.fillOval (MID-50, TOP+80, 100, 60); // lower torso page.setColor (Color.black); page.fillOval (MID-10, TOP+10, 5, 5); // left eye page.fillOval (MID+5, TOP+10, 5, 5); // right eye page.drawArc (MID-10, TOP+20, 20, 10, 190, 160); // smile page.drawLine (MID-25, TOP+60, MID-50, TOP+40); // left arm page.drawLine (MID+25, TOP+60, MID+55, TOP+60); // right arm page.drawLine (MID-20, TOP+5, MID+20, TOP+5); // brim of hat page.fillRect (MID-15, TOP-20, 30, 25); // top of hat } }
Bits of Binary
cloud of points
import java.awt.geom.Point2D; import java.awt.geom.Ellipse2D; import java.awt.Graphics2D; import java.util.ArrayList; /** This class draws a cloud of circles */ public class Cloud { /** Construct a Cloud object */ public Cloud() { points = new ArrayList(); } /** Add a point to the array list @param aPoint the point to add */ public void add(Point2D.Double aPoint) { points.add(aPoint); } /** Draws the cloud @param g2 the graphics context */ public void draw(Graphics2D g2) { for (int i = 0; i < points.size(); i++) { Point2D.Double p = (Point2D.Double)points.get(i); Ellipse2D.Double e = new Ellipse2D.Double(p.getX(), p.getY(), RADIUS, RADIUS); g2.draw(e); } } private ArrayList points; private final int RADIUS = 5; } /* Class Cloud: 1. Constructor creates an array list. 2. The method add takes a Point2D.Double as an argument and it adds it to the array list. 3. The method draw creates the points and uses g2.draw(aPoint). Note: a Point2D.Double can not be drawn. So, you should draw an Ellipse2D.Double with x and y from Point2D.Double. 4. Write a class that extends Applet or JApplet to draw a cloud's points by creating an object of Cloud and randomly generating the x and y coordinates. */
import javax.swing.JApplet; import java.awt.Graphics; import java.awt.Graphics2D; import java.util.Random; import java.awt.geom.Point2D; /** This applet displays a cloud of circles */ public class ExP13_7JApplet extends JApplet { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; Cloud c = new Cloud(); Random generator = new Random(); double x = 0; double y = 0; for (int i = 0; i < 20; i++) { x = 200 * generator.nextDouble(); y = 200 * generator.nextDouble(); c.add(new Point2D.Double(x, y)); } c.draw(g2); } }