/./././././././././. // 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 …
Tag: Java
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 …
Aug 26
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 …
Aug 26
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 …
Aug 26
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 …
Aug 26
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 …
Aug 26
Explicit placement of five buttons with the layout manager turned off
NullTest.java Explicit placement of five buttons with the layout manager turned off (set to null) ########################## import java.applet.Applet; import java.awt.*; /** Layout managers are intended to help you, but there * is no law saying you have to use them. * Set the layout to null to turn them off. * ******************* public class NullTest …
Aug 26
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(); …
Aug 26
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 * …