Category: Advanced Swing

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

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

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

A Frame that can actually quit

import java.awt.*; import java.awt.event.*; /** A Frame that you can actually quit. Used as the starting * point for most Java 1.1 graphical applications. * public class CloseableFrame extends Frame { public CloseableFrame(String title) { super(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } /** Since we are doing something permanent, we need * to call super.processWindowEvent first. */ public void …

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

Jan 02

ListEvent2.java

# ListEvents.java Uses the following classes: * CloseableFrame.java * SelectionReporter.java * ActionReporter.java /././././././././././././ import java.awt.event.*; /././././././ public class ListEvents2 extends ListEvents { public static void main(String[] args) { new ListEvents2(); } /** Extends ListEvents with the twist that * typing any of the letters of "JAVA" or "java" * over the language list will result …

Continue reading

Jan 02

BorderLayout divides the window into five regions

# BorderTest.java Five buttons arranged by BorderLayout BorderLayout divides the window into five regions: NORTH, SOUTH, EAST, WEST, and CENTER. /./././././././././././././ import java.applet.Applet; import java.awt.*; /** An example of BorderLayout. * &&&&&&&&&&&&&&&&&&&&&&&&&&& public class BorderTest extends Applet { public void init() { setLayout(new BorderLayout()); add(new Button("Button 1"), BorderLayout.NORTH); add(new Button("Button 2"), BorderLayout.SOUTH); add(new Button("Button 3"), …

Continue reading

Jan 02

Six buttons arranged in a 2 row x 3 column grid by GridLayout

/././././././ GridTest.java Six buttons arranged in a 2 row x 3 column grid by GridLayout.GridLayout divides the window into equal-sized rectangles based upon the number of rows and columns specified. ****************** import java.applet.Applet; import java.awt.*; /** An example of GridLayout. * /./././././. public class GridTest extends Applet { public void init() { setLayout(new GridLayout(2,3)); // …

Continue reading

Jan 02

A demo providing multiple buttons to select a playing card-A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display

####################### # CardDemo.java A demo providing multiple buttons to select a playing card. A Panel, using CardLayout control which of four possible subpanels, holding a different card, to display.Uses the following class and images: * CardPanel.java A Panel that displays a playing card. * ImageLabel.java A Canvas for displaying images. * Ace.gif, King.gif, Queen.gif, Jack.gif. …

Continue reading

Jan 02

draws a circle wherever mouse was pressed

CircleListener.java A subclass of MouseAdapter that draws a circle wherever mouse was pressed. Illustrates first approach to event-handling with listeners: attaching a separate listener *********** import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** The listener used by CircleDrawer1. Note call * to getSource to obtain reference to the applet. * *************** public class CircleListener extends MouseAdapter …

Continue reading

Jan 02

Adds typing to the freehand drawing.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** A better whiteboard that lets you enter * text in addition to freehand drawing. * ****************** public class Whiteboard extends SimpleWhiteboard { protected FontMetrics fm; public void init() { super.init(); Font font = new Font("Serif", Font.BOLD, 20); setFont(font); fm = getFontMetrics(font); addKeyListener(new CharDrawer()); } private class CharDrawer extends …

Continue reading

Jan 02

Places a Panel holding 100 buttons in a ScrollPane

import java.applet.Applet; import java.awt.*; /** Places a Panel holding 100 buttons in a ScrollPane that is * too small to hold it. * */ public class ScrollPaneTest extends Applet { public void init() { setLayout(new BorderLayout()); ScrollPane pane = new ScrollPane(); Panel bigPanel = new Panel(); bigPanel.setLayout(new GridLayout(10, 10)); for(int i=0; i<100; i++) { bigPanel.add(new …

Continue reading

Jan 02

Template illustrating the first approach for creating a class with thread behavior.

/** Taken from Core Web Programming from * Prentice Hall and Sun Microsystems Press, * © 2001 Marty Hall and Larry Brown; * may be freely used or adapted. */ public class ThreadClass extends Thread { public void run() { // Thread behavior here. } } Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan …

Continue reading

Jan 02

Creates and starts three threaded objects

Creates and starts three threaded objects which count from 0 to 4. Uses the following class: CounterTest.java Counter.java /** Try out a few instances of the Counter class. public class CounterTest { public static void main(String[] args) { Counter c1 = new Counter(5); Counter c2 = new Counter(5); Counter c3 = new Counter(5); c1.start(); c2.start(); …

Continue reading

Jan 02

Template illustrating the second approach for creating a class with thread behavior.

Template illustrating the second approach for creating a class with thread behavior. In this case, the class implements the Runnable interface while providing a run method for thread execution. public class ThreadedClass extends AnyClass implements Runnable { public void run() { // Thread behavior here. } public void startThread() { Thread t = new Thread(this); …

Continue reading

Jan 02

Counter2Test.java Driver class that creates three threaded objects (Counter2) that count from 0 to 4.

/** Try out a few instances of the Counter2 class. Driver class that creates three threaded objects (Counter2) that count from 0 to 4. In this case, the driver does not start the threads, as each thread is automatically started in Counter2’s constructor. Uses the following class: Counter2Test.java Counter2.java ************************** public class Counter2Test { public …

Continue reading

Jan 02

Driver class that creates three threaded objects (Counter2) that count from 0 to 4.

/** Try out a few instances of the Counter2 class. public class Counter2Test { public static void main(String[] args) { Counter2 c1 = new Counter2(5); Counter2 c2 = new Counter2(5); Counter2 c3 = new Counter2(5); } } /** A Runnable that counts up to a specified * limit with random pauses in between each count. …

Continue reading

Jan 02

Buggy Counter Applet.java Demonstrates that data shared by multiple threads is candidate for a potential race condition

import java.applet.Applet; import java.awt.*; /** Emulates the Counter and Counter2 classes, but this time * from an applet that invokes multiple versions of its own run * method. This version is likely to work correctly * except when an important customer is visiting. public class BuggyCounterApplet extends Applet implements Runnable{ private int totalNum = 0; …

Continue reading

Jan 02

ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method.

ThreadedRSAKey.java Illustrates converting a method in an existing class from a single-threaded method to a multi-threaded method. In this example, RSAKey computes an RSA public-private key pair, where the key size has a specified number of digits. As large prime numbers require considerable CPU time, ThreadedRSAKey converts the original computeKey method in RSAKey to a …

Continue reading

Jan 02

JList Examples

All examples, except for FileTransfer use WindowUtilities.java and ExitListener.java. WindowUtilities.java: import javax.swing.*; import java.awt.*; // For Color and Container classes. /** A few utilities that simplify using windows in Swing. * */ public class WindowUtilities { /** Tell system to use native look and feel, as in previous * releases. Metal (Java) LAF is the …

Continue reading

Jan 02

JTree Examples

SimpleTree.java Basic tree built out of DefaultMutableTreeNodes. A DefualtMutableTreeNode is a starting point for a root node, in which children nodes can be added. import java.awt.*; import javax.swing.*; import javax.swing.tree.*; /** Example tree built out of DefaultMutableTreeNodes. * */ public class SimpleTree extends JFrame { public static void main(String[] args) { new SimpleTree(); } public …

Continue reading

Jan 02

JTable Examples

# JTableSimpleExample.java Simple table that takes column names and data from arrays of Strings. import java.awt.*; import javax.swing.*; /** Simple JTable example that uses a String array for the * table header and table data. * */ public class JTableSimpleExample extends JFrame { public static void main(String[] args) { new JTableSimpleExample(); } private final int …

Continue reading

Jan 02

Printing in Java 2

* o PrintExample.java Demonstrates printing a Graphics2D object. import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.print.*; /** An example of a printable window in Java 1.2. The key point * here is that any component is printable in Java 1.2. * However, you have to be careful to turn off double buffering * globally (not …

Continue reading

Jan 02

A simple applet (JApplet) created in Swing.

import java.awt.*; import javax.swing.*; /** Tiny example showing the main differences in using * JApplet instead of Applet: using the content pane, * getting Java (Metal) look and feel by default, and * having BorderLayout be the default instead of FlowLayout. * */ public class JAppletExample extends JApplet { public void init() { WindowUtilities.setNativeLookAndFeel(); Container …

Continue reading

Jan 02

creating a simple Swing application using a JFrame

JFrameExample.java Demonstrates creating a simple Swing application using a JFrame. As with a JApplet, components must be added to the content pane, instead of the window directly.import java.awt.*; import javax.swing.*; /** Tiny example showing the main difference in using * JFrame instead of Frame: using the content pane * and getting the Java (Metal) look …

Continue reading

Jan 02

Implementation of a simple browser in Swing (The user can specify a URL to load into the browser (JEditorPane))

Browser.java Implementation of a simple browser in Swing. The user can specify a URL to load into the browser (JEditorPane). By attaching an Hyperlink Listener, the editor pane is responsive to hyperlinks selected by the user. Uses the following class and image: import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; /** …

Continue reading

Jan 02

BetterCircleTest.java

********************** BetterCircleTest.java ********************** import java.awt.*; import java.applet.Applet; /** Position circles down the diagonal so that their borders * just touch. Illustrates that Java 1.1 lightweight * components can be partially transparent. * */ public class BetterCircleTest extends Applet { public void init() { setBackground(Color.lightGray); setLayout(null); BetterCircle circle; int radius = getSize().width/6; int deltaX = round(2.0 …

Continue reading

Skip to toolbar