import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** An example of shear transformations on a rectangle. * *********************** public class ShearExample extends JPanel { private static int gap=10, width=100; private Rectangle rect = new Rectangle(gap, gap, 100, 100); public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D)g; for (int i=0; i<5; i++) { g2d.setPaint(Color.red); g2d.fill(rect); …
Category: FromSitesTree.com
May 04
Basic Swing Details #Programming Code Examples #Java/J2EE/J2ME #Basic Swing
* WindowUtilities.java Utility class that simplifies creating a window and setting the look and feel. * ExitListener.java A WindowListener with support to close the window. * JAppletExample.java A simple applet (JApplet) created in Swing. Illustrates setting the look and feel, adding components to the content pane, and changing the layout to FlowLayout (default is BorderLayout). …
May 04
Illustrates the effect of different transparency #Programming Code Examples #Java/J2EE/J2ME #Drawing
~~~~~~~~~~~~~~~~~~~ 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 blueSquare = new …
May 04
ListFonts.java Lists all local fonts available for graphical drawing. #Programming Code Examples #Java/J2EE/J2ME #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>>>>>>>>>>>>>>>>>>>>>>> Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, …
May 04
Illustrates using a local font (Goudy Handtooled BT) to perform drawing in Java 2D #Programming Code Examples #Java/J2EE/J2ME #Drawing
import java.awt.*; /** An example of using local fonts to perform drawing in * Java 2D. * ********************** public class FontExample extends GradientPaintExample { public FontExample() { GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment(); env.getAvailableFontFamilyNames(); setFont(new Font("Goudy Handtooled BT", Font.PLAIN, 100)); } protected void drawBigString(Graphics2D g2d) { g2d.setPaint(Color.black); g2d.drawString("Java 2D", 25, 215); } public void paintComponent(Graphics g) { …
May 04
Demonstrates setting the pen width (in pixels) using a BasicStroke prior to drawing. Inherits from FontExample.java. #Programming Code Examples #Java/J2EE/J2ME #Drawing
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); g2d.setStroke(new BasicStroke(8)); // 8-pixel wide pen g2d.draw(getCircle()); } public static …
May 04
LineStyles.java Provides examples of the available styles for joining line segments #Programming Code Examples #Java/J2EE/J2ME #Drawing
import javax.swing.*; import java.awt.*; import java.awt.geom.*; /** A demonstration of different controls when joining two line * segments. The style of the line end point is controlled * through the capStyle parameter. * ************************************ public class LineStyles extends JPanel { private GeneralPath path; private static int x = 30, deltaX = 150, y = 300, …
May 04
RotationExample.java An example of translating and rotating the coordinate system prior to drawing #Programming Code Examples #Java/J2EE/J2ME #Drawing
import java.awt.*; /** An example of translating and rotating the coordinate * system before each drawing. * ******************************* public class RotationExample extends StrokeThicknessExample { private Color[] colors = { Color.white, Color.black }; public void paintComponent(Graphics g) { clear(g); Graphics2D g2d = (Graphics2D)g; drawGradientCircle(g2d); drawThickCircleOutline(g2d); // Move the origin to the center of the circle. g2d.translate(185.0, …
May 04
Draws a filled ellipse #Programming Code Examples #Java/J2EE/J2ME #Drawing
import javax.swing.*; // For JPanel, etc. import java.awt.*; // For Graphics, etc. import java.awt.geom.*; // For Ellipse2D, etc. /** An example of drawing/filling shapes with Java 2D in * Java 1.2 and later. * ************************** public class ShapeExample extends JPanel { private Ellipse2D.Double circle = new Ellipse2D.Double(10, 10, 350, 350); private Rectangle2D.Double square = new …
May 04
Draws a circle with a gradient fill #Programming Code Examples #Java/J2EE/J2ME #Drawing
GradientPaintExample.java Draws a circle with a gradient fill. Inherits from ShapeExample.java. ************************************** import java.awt.*; /** An example of applying a gradient fill to a circle. The * color definition starts with red at (0,0), gradually * changing to yellow at (175,175). * ********************************** public class GradientPaintExample extends ShapeExample { private GradientPaint gradient = new GradientPaint(0, …
