FlowTest.java ************* FlowTest.java Five buttons arranged by FlowLayout By default, FlowLayout arranges components in rows, left to right, and centered. /././././././././././././ import java.applet.Applet; import java.awt.*; /** FlowLayout puts components in rows. * ************************************ public class FlowTest extends Applet { public void init() { for(int i=1; i<6; i++) { add(new Button(“Button ” + …
Category: Java/J2EE/J2ME
Aug 26
A Frame that lets you draw circles with mouse clicks
SavedFrame.java **************** A Frame that lets you draw circles with mouse clicks //************** import java.awt.*; import java.awt.event.*; import java.io.*; /** A Frame that lets you draw circles with mouse clicks * and then save the Frame and all circles to disk. * public class SavedFrame extends CloseableFrame implements ActionListener { /** If …
Aug 26
FrameExample1.java and 2
****************** # FrameExample1.java ****************** import java.awt.*; /** */ public class FrameExample1 { public static void main(String[] args) { Frame f = new Frame(“Frame Example 1”); f.setSize(400, 300); f.setVisible(true); } } ********************* # FrameExample2.java ********************* import java.awt.*; /** */ public class FrameExample2 extends Frame { public static void main(String[] args) …
Aug 26
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 …
Aug 26
Eight ungrouped buttons in an Applet using FlowLayout
mport java.applet.Applet; import java.awt. *; ************************** /** Eight ungrouped buttons in an Applet using FlowLayout. * */ public class ButtonTest1 extends Applet { public void init() { String[] labelPrefixes = { “Start”, “Stop”, “Pause”, “Resume” }; for (int i=0; i<4; i++) { add(new Button(labelPrefixes[i] + ” Thread1″)); } for (int i=0; i<4; i++) { add(new …
Aug 26
A Circle component built using a Canvas
import java.awt.*; /** A Circle component built using a Canvas. * */ public class Circle extends Canvas { private int width, height; public Circle(Color foreground, int radius) { setForeground(foreground); width = 2*radius; height = 2*radius; setSize(width, height); } public void paint(Graphics g) { …
Aug 26
Simplifies the setting of native look and feel
WindowUtilities.java Simplifies the setting of native look and feel. #################### 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 …
Aug 26
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 …
Aug 26
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, …
Aug 26
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 …