Category: Programming Code Examples

Programming Code Examples

Jan 02

Basic template for a Java applet

AppletTemplate.java >>>>>>>>>>>>>>>>>>>> import java.applet.Applet; import java.awt.*; ******************** public class AppletTemplate extends Applet { // Variable declarations. public void init() { // Variable initializations, image loading, etc. } public void paint(Graphics g) { // Drawing operations. } } >>>>>>>>>>>>>>>>>>>>> Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

Application that reports all command-line arguments

****************** ShowArgs.java Application that reports all command-line arguments. ****************** */ public class ShowArgs { public static void main(String[] args) { for(int i=0; i

Jan 02

HelloWWW2.java Illustrates the ability of an applet to read parameters contained in the HTML document

HelloWWW2.java Illustrates the ability of an applet to read parameters contained in the HTML document (PARAM element containing a NAME-VALUE pair). &&&&&&&&&&&&&&&&&&&&&&&&&&&&&& import java.applet.Applet; import java.awt.*; ************************* public class HelloWWW2 extends Applet { public void init() { setFont(new Font("SansSerif", Font.BOLD, 30)); Color background = Color.gray; Color foreground = Color.darkGray; String backgroundType = getParameter("BACKGROUND"); if (backgroundType …

Continue reading

Jan 02

HelloWWW.java Basic Hello World (Wide Web) Applet

********************* import java.applet.Applet; import java.awt.*; ********************* public class HelloWWW extends Applet { private int fontSize = 40; public void init() { setBackground(Color.black); setForeground(Color.white); setFont(new Font("SansSerif", Font.BOLD, fontSize)); } public void paint(Graphics g) { g.drawString("Hello, World Wide Web.", 5, fontSize+5); } } <

Jan 02

Loading Images

JavaMan1.java Applet that loads an image from a relative URL. ************************************************************* import java.applet.Applet; import java.awt.*; /** An applet that loads an image from a relative URL. * >>>>>>>>>>>>>>>>>>> public class JavaMan1 extends Applet { private Image javaMan; public void init() { javaMan = getImage(getCodeBase(),"images/Java-Man.gif"); } public void paint(Graphics g) { g.drawImage(javaMan, 0, 0, this); } …

Continue reading

Jan 02

Message.java Applet that reads customization parameters from an HTML file

******************* Message.java Applet that reads customization parameters from an HTML file ******************* import java.applet.Applet; import java.awt.*; **************** public class Message extends Applet { private int fontSize; private String message; public void init() { setBackground(Color.black); setForeground(Color.white); // Base font size on window height. fontSize = getSize().height – 10; setFont(new Font("SansSerif", Font.BOLD, fontSize)); // Read heading message …

Continue reading

Jan 02

Controlling Image Loading

~~~~~~~~~~~~~~~~~~~ ImageBox.java A class that incorrectly tries to load an image and draw an outline around it. The problem is that the size of the image is requested before the image is completely loaded, thus, returning a width and height of -1. ~~~~~~~~~~~~~~~~~~~ import java.applet.Applet; import java.awt.*; /** A class that incorrectly tries to load …

Continue reading

Jan 02

DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground

DropBall.java Uses a while loop to determine how long it takes a ball to fall from the top of the Washington Monument to the ground ************************************************************ /** Simulating dropping a ball from the top of the Washington * Monument. The program outputs the height of the ball each * second until the ball hits the …

Continue reading

Jan 02

Accesses instance variables in a Ship object.

Test1.java Accesses instance variables in a Ship object. ******************************************************** // Create a class with five instance variables (fields): // x, y, speed, direction, and name. Note that Ship1 is // not declared "public", so it can be in the same file as // Test1. A Java file can only contain one "public" class // definition. …

Continue reading

Jan 02

Accesses methods in a Ship2 object

********************************************* Test2.java Accesses methods in a Ship2 object ********************************************* // Give the ship public move and printLocation methods. class Ship2 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name = "UnnamedShip"; private double degreesToRadians(double degrees) { return(degrees * Math.PI / 180.0); } public void move() { double angle = degreesToRadians(direction); x = x + …

Continue reading

Jan 02

Using the this reference in class Ship3

/./././././././././. // Give Ship3 a constructor to let the instance variables // be specified when the object is created. /./././././././././ class Ship3 { public double x, y, speed, direction; public String name; public Ship3(double x, double y, double speed, double direction, String name) { this.x = x; // "this" differentiates instance vars this.y = y; …

Continue reading

Jan 02

Demonstrates overloading methods in class Ship4

********************* class Ship4 { public double x=0.0, y=0.0, speed=1.0, direction=0.0; public String name; // This constructor takes the parameters explicitly. public Ship4(double x, double y, double speed, double direction, String name) { this.x = x; this.y = y; this.speed = speed; this.direction = direction; this.name = name; } // This constructor requires a name but …

Continue reading

Jan 02

ListFonts.java Lists all local fonts available for graphical drawing.

ListFonts.java Lists all local fonts available for graphical drawing. *********************** import java.awt.*; /** Lists the names of all available fonts. * ****************** public class ListFonts { public static void main(String[] args) { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); String[] fontNames = env.getAvailableFontFamilyNames(); System.out.println("Available Fonts:"); for(int i=0; i>>>>>>>>>>>>>>>>>>>>>>> Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, …

Continue reading

Jan 02

A Ship class illustrating object-oriented programming concepts

************************ Ship.java A Ship class illustrating object-oriented programming concepts. Incorporates Javadoc comments. See ShipTest.java for a test. ************************ /** Ship example to demonstrate OOP in Java. * * @author * Larry Brown * @version 2.0 */ public class Ship { // Instance variables private double x=0.0, y=0.0, speed=1.0, direction=0.0; private String name; // Constructors /** …

Continue reading

Jan 02

Illustrates using a local font (Goudy Handtooled BT) to perform drawing in Java 2D

import java.awt.*; /** An example of using local fonts to perform drawing in * Java 2D. * ********************** public class FontExample extends GradientPaintExample { public FontExample() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); env.getAvailableFontFamilyNames(); setFont(new Font("Goudy Handtooled BT", Font.PLAIN, 100)); } protected void drawBigString(Graphics2D g2d) { g2d.setPaint(Color.black); g2d.drawString("Java 2D", 25, 215); } public void paintComponent(Graphics g) { …

Continue reading

Jan 02

Speedboat.java Illustrates inheritance from Ship class

***************************** Speedboat.java Illustrates inheritance from Ship class. See SpeedboatTest.java for a test. ***************************** /** A fast Ship. Red and going 20 knots by default. * *********************** public class Speedboat extends Ship { private String color = "red"; /** Builds a red Speedboat going N at 20 knots. */ public Speedboat(String name) { super(name); setSpeed(20); } …

Continue reading

Jan 02

Demonstrates setting the pen width (in pixels) using a BasicStroke prior to drawing. Inherits from FontExample.java.

StrokeThicknessExample.java >>>>>>>>>>>>>>>>>>>>>>>>>>> import java.awt.*; /** An example of controlling the Stroke (pen) widths when * drawing. * ****************** */ public class StrokeThicknessExample extends FontExample { public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawBigString(g2d); drawThickCircleOutline(g2d); } protected void drawThickCircleOutline(Graphics2D g2d) { g2d.setPaint(Color.blue); g2d.setStroke(new BasicStroke(8)); // 8-pixel wide pen g2d.draw(getCircle()); } public static …

Continue reading

Jan 02

Code examples for interfaces

**************************** Code examples for interfaces: * Class1.java implements Interface1.java * Abstract Class2.java implements Interface1.java and Interface2.java * Class3.java extends abstract class Class2.java * Interface3.java extends Interface1.java and Interface2.java *************************** ~~~~~~~~~~~~~~~~~~~~~~~~~~~ Class1.java ~~~~~~~~~~~~~~~~~~~~~~~~~~~ // This class is not abstract, so it must provide // implementations of method1 and method2. public class Class1 extends SomeClass implements Interface1 …

Continue reading

Jan 02

DashedStrokeExample.java Draws a circle with a dashed line segment (border). Inherits from FontExample.java.

>>>>>>>>>>>>>>>>> import java.awt.*; /** An example of creating a custom dashed line for drawing. * ********************* public class DashedStrokeExample extends FontExample { public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawBigString(g2d); drawDashedCircleOutline(g2d); } protected void drawDashedCircleOutline(Graphics2D g2d) { g2d.setPaint(Color.blue); // 30-pixel line, 10-pixel gap, 10-pixel line, 10-pixel gap float[] dashPattern = { …

Continue reading

Jan 02

Example illustrating inheritance and abstract classes

*********************************** # Example illustrating inheritance and abstract classes. * Shape.java The parent class (abstract) for all closed, open, curved, and straight-edged shapes. * Curve.java An (abstract) curved Shape (open or closed). * StraightEdgedShape.java A Shape with straight edges (open or closed). * Measurable.java Interface defining classes with measurable areas. * Circle.java A circle that extends …

Continue reading

Jan 02

LineStyles.java Provides examples of the available styles for joining line segments

import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** A demonstration of different controls when joining two line * segments. The style of the line end point is controlled * through the capStyle parameter. * ************************************ public class LineStyles extends JPanel { private GeneralPath path; private static int x = 30, deltaX = 150, y = 300, …

Continue reading

Jan 02

Example demonstrating the use of packages

&&&&&&&&&&&&&&&&&&& Example demonstrating the use of packages. * Class1.java defined in package1. * Class2.java defined in package2. * Class3.java defined in package2.package3. * Class1.java defined in package4. * PackageExample.java Driver for package example &&&&&&&&&&&&&&&&&&&&& ~~~~~~~~~~~~~~~~~~~~~ Class1.java defined in package1. ~~~~~~~~~~~~~~~~~~~~~ package package1; ***************** public class Class1 { public static void printInfo() { System.out.println("This is Class1 …

Continue reading

Jan 02

RotationExample.java An example of translating and rotating the coordinate system prior to drawing

import java.awt.*; /** An example of translating and rotating the coordinate * system before each drawing. * ******************************* public class RotationExample extends StrokeThicknessExample { private Color[] colors = { Color.white, Color.black }; public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawThickCircleOutline(g2d); // Move the origin to the center of the circle. g2d.translate(185.0, …

Continue reading

Jan 02

Statics.java Demonstrates static and non-static methods.

*/ public class Statics { public static void main(String[] args) { staticMethod(); Statics s1 = new Statics(); s1.regularMethod(); } public static void staticMethod() { System.out.println("This is a static method."); } public void regularMethod() { System.out.println("This is a regular method."); } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

ShearExample.java. Illustrates the effect of applying a shear transformation prior to drawing a square

import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** An example of shear transformations on a rectangle. * *********************** public class ShearExample extends JPanel { private static int gap=10, width=100; private Rectangle rect = new Rectangle(gap, gap, 100, 100); public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; for (int i=0; i<5; i++) { g2d.setPaint(Color.red); g2d.fill(rect); …

Continue reading

Jan 02

Basic Hello World application

******************* HelloWorld.java Basic Hello World application. ******************* */ public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, world."); } } /* Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, 2017

Jan 02

ButtonExample.java Uses the following

/./././././././././ # ButtonExample.java Uses the following classes: * CloseableFrame.java * FgReporter.java * BgReporter.java * SizeReporter.java ****************** ButtonExample.java ****************** import java.awt.*; import java.awt.event.*; /././././././././././ public class ButtonExample extends CloseableFrame { public static void main(String[] args) { new ButtonExample(); } public ButtonExample() { super("Using ActionListeners"); setLayout(new FlowLayout()); Button b1 = new Button("Button 1"); Button b2 = new …

Continue reading

Jan 02

A textfield and three buttons arranged by a verticle BoxLayout

BoxLayoutTest.java A textfield and three buttons arranged by a verticle BoxLayout. Uses WindowUtilities.java and ExitListener.java. ################## import java.awt.*; import java.awt.event.*; import javax.swing.*; /** An example of BoxLayout. * *********** public class BoxLayoutTest extends JPanel implements ActionListener{ BoxLayout layout; JButton topButton, middleButton, bottomButton; public BoxLayoutTest() { layout = new BoxLayout(this, BoxLayout.Y_AXIS); setLayout(layout); JLabel label = new …

Continue reading

Jan 02

Applet that uses processXxx methods to print detailed reports on mouse events. Illustrates low-level alternative to handling events with listeners.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Prints non-detailed reports of mouse events. * Uses the low-level processXxxEvent methods instead * of the usual event listeners. * ***************** public class MouseReporter extends Applet { public void init() { setBackground(Color.blue); // So you can see applet in page enableEvents(AWTEvent.MOUSE_EVENT_MASK | AWTEvent.MOUSE_MOTION_EVENT_MASK); } public void processMouseEvent(MouseEvent event) …

Continue reading

Jan 02

Checkboxes

Checkboxes.java Inherits from CloseableFrame.java. ****************** import java.awt.*; /./././././././././ public class Checkboxes extends CloseableFrame { public static void main(String[] args) { new Checkboxes(); } public Checkboxes() { super("Checkboxes"); setFont(new Font("SansSerif", Font.BOLD, 18)); setLayout(new GridLayout(0, 2)); Checkbox box; for(int i=0; i<12; i++) { box = new Checkbox("Checkbox " + i); if (i%2 == 0) { box.setState(true); } …

Continue reading

Skip to toolbar