Category: Java/J2EE/J2ME

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);     …

Continue reading

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>>>>>>>>>>>>>>>>>>>>>>>

Illustrates the effect of different transparency

~~~~~~~~~~~~~~~~~~~ TransparencyExample.java Illustrates the effect of different transparency (alpha) values when drawing a shape. ~~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** An illustration of the use of AlphaComposite to make  *  partially transparent drawings.  *  ********************************** public class TransparencyExample extends JPanel {   private static int gap=10, width=60, offset=20,                      deltaX=gap+width+offset;   private Rectangle …

Continue reading

Handling Events

***********************************     * ActionExample1.java Inherits from CloseableFrame.java and uses SetSizeButton.java.     * ActionExample2.java Inherits from CloseableFrame.java.     ********************************************************** ActionExample1.java ******************* import java.awt.*; public class ActionExample1 extends CloseableFrame {   public static void main(String[] args) {     new ActionExample1();   }   public ActionExample1() {     super(“Handling Events in Component”);     setLayout(new FlowLayout());     setFont(new Font(“Serif”, …

Continue reading

A Frame that uses the Confirm dialog to verify quit

ConfirmTest.java **************** import java.awt.*; import java.awt.event.*; /** A Frame that uses the Confirm dialog to verify that  *  users really want to quit.  *   public class ConfirmTest extends Frame {   public static void main(String[] args) {     new ConfirmTest();   }   public ConfirmTest() {     super(“Confirming QUIT”);     setSize(200, 200);     addWindowListener(new …

Continue reading

CircleDrawer3.java Applet that uses a named nested class to handle mouse events and draw circles. Illustrates third approach to event-handling with listeners: using inner classes.

import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** Draw circles centered where the user clicks.  *  Uses named inner classes.  *    ****************** public class CircleDrawer3 extends Applet {   public void init() {     setForeground(Color.blue);     addMouseListener(new CircleListener());   }   private class CircleListener extends MouseAdapter {     private int radius = 25;     public …

Continue reading

Applet handle mouse events

ClickReporter.java A simple applet that uses the class to handle mouse events import java.applet.Applet; import java.awt.*; /** Prints a message saying where the user clicks. * Uses an external listener. * public class ClickReporter extends Applet { public void init() { setBackground(Color.yellow); addMouseListener(new ClickListener()); } }

Custom AWT Slider

***************** Custom AWT Slider     * LabeledCostSlider.java. A numeric slider class with attached label.     * CostSlider.java. A slider class that lets you read numeric values. Used in the LabeledCostSlider class.     * Slider.java. A slider class: a combination of Scrollbar and TextField. Used in the CostSlider class.     * ScrollbarPanel.java A Panel with adjustable …

Continue reading

Layout of a complicated GUI interface with GridLayout

################################## GridBagTest.java Layout of a complicated GUI interface with GridLayout. Uses WindowUtilities.java and ExitListener.java. ################################## import java.awt.*; import java.awt.event.*; import java.util.*; import javax.swing.*; import javax.swing.border.*; /** An example demonstrating a GridBagLayout GUI with  *  input text area and multiple buttons.  *  ********* public class GridBagTest extends JPanel {   private JTextArea textArea;   private JButton …

Continue reading

TextAreas

TextAreas.java ************** import java.applet.Applet; import java.awt.*; /././././././,/././././ public class TextAreas extends Applet {   public void init() {     setBackground(Color.lightGray);     add(new TextArea(3, 10));     add(new TextArea(“Some\nInitial\nText”, 3, 10));   } }