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>>>>>>>>>>>>>>>>>>>>>>>
Tag: code
Aug 28
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 …
Aug 27
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”, …
Aug 27
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 …
Aug 27
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 …
Aug 27
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()); } }
Aug 27
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 …
Aug 27
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 …
Aug 27
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)); } }
Aug 27
Lists.java
Lists.java Inherits from CloseableFrame.java. /./././././././././ import java.awt.*; /*****************/ public class Lists extends CloseableFrame { public static void main(String[] args) { new Lists(); } public Lists() { super(“Lists”); setLayout(new FlowLayout()); setBackground(Color.lightGray); setFont(new Font(“SansSerif”, Font.BOLD, 18)); List list1 = new List(3, false); list1.add(“Vanilla”); list1.add(“Chocolate”); list1.add(“Strawberry”); …