Tag: Java/J2EE/J2MEDrawing

Jan 02

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>>>>>>>>>>>>>>>>>>>>>>> Note: Brought from our old site: http://www.salearningschool.com/example_codes/ on Jan 2nd, …

Continue reading

Jan 02

Illustrates using a local font (Goudy Handtooled BT) to perform drawing in Java 2D

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

Continue reading

Jan 02

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); g2d.setStroke(new BasicStroke(8)); // 8-pixel wide pen g2d.draw(getCircle()); } public static …

Continue reading

Jan 02

LineStyles.java Provides examples of the available styles for joining line segments

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

Continue reading

Jan 02

RotationExample.java An example of translating and rotating the coordinate system prior to 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, …

Continue reading

Jan 02

ShearExample.java. Illustrates the effect of applying a shear transformation prior to drawing a square

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

Continue reading

Jan 02

Draws a filled ellipse

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 …

Continue reading

Jan 02

Draws a circle with a gradient fill

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

Continue reading

Jan 02

Demonstrates using a TexturePaint to fill an shape with a tiled image

^^^^^^^^^^^^^^^^^ TiledImages.java Demonstrates using a TexturePaint to fill an shape with a tiled image. Uses the following class and images: * ImageUtilities.java Simplifies creating a BufferedImage from an image file. ~~~~~~~~~~~~~~~~~~ import javax.swing.*; import java.awt.*; import java.awt.geom.*; import java.awt.image.*; /** An example of using TexturePaint to fill objects with tiled * images. Uses the getBufferedImage …

Continue reading

Jan 02

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 blueSquare = new …

Continue reading

Skip to toolbar