
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class TransformScale extends JComponent {
Shape axes, shape;
int length = 54, arrowLength = 4, tickSize = 4;
public TransformScale() {
axes = createAxes();
shape = createShape();
}
protected Shape createAxes() {
GeneralPath path = new GeneralPath();
// Axes.
path.moveTo(-length, 0);
path.lineTo(length, 0);
path.moveTo(0, -length);
path.lineTo(0, length);
// Arrows.
path.moveTo(length - arrowLength, -arrowLength);
path.lineTo(length, 0);
path.lineTo(length - arrowLength, arrowLength);
path.moveTo(-arrowLength, length - arrowLength);
path.lineTo(0, length);
path.lineTo(arrowLength, length - arrowLength);
// Half-centimeter tick marks
float cm = 72 / 2.54f;
float lengthCentimeter = length / cm;
for (float i = 0.5f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize / 2);
path.lineTo(tick, tickSize / 2);
path.moveTo(-tick, -tickSize / 2);
path.lineTo(-tick, tickSize / 2);
path.moveTo(-tickSize / 2, tick);
path.lineTo(tickSize / 2, tick);
path.moveTo(-tickSize / 2, -tick);
path.lineTo(tickSize / 2, -tick);
}
// Full-centimeter tick marks
for (float i = 1.0f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize);
path.lineTo(tick, tickSize);
path.moveTo(-tick, -tickSize);
path.lineTo(-tick, tickSize);
path.moveTo(-tickSize, tick);
path.lineTo(tickSize, tick);
path.moveTo(-tickSize, -tick);
path.lineTo(tickSize, -tick);
}
return path;
}
protected Shape createShape() {
float cm = 72 / 2.54f;
return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Use antialiasing.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Move the origin to 75, 75.
AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
g2.transform(at);
// Draw the shapes in their original locations.
g2.setPaint(Color.black);
g2.draw(axes);
g2.draw(shape);
// Transform the Graphics2D.
g2.transform(AffineTransform.getScaleInstance(3, 3));
// Draw the "new" shapes in dashed.
g2.transform(AffineTransform.getTranslateInstance(75, 75));
Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0);
g2.setStroke(stroke);
g2.draw(axes);
g2.draw(shape);
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.getContentPane().add(new TransformScale());
f.setSize(650, 550);
f.show();
}
}
|
|

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TransformDemo extends JPanel {
public void init() {
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Rectangle rect = new Rectangle(5,5,200,200);
int w = getSize().width;
int h = getSize().height;
AffineTransform saveXform = g2.getTransform();
AffineTransform toCenterAt = new AffineTransform();
toCenterAt.translate(w / 2 - (rect.width / 2), h / 2 - (rect.height / 2));
g2.transform(toCenterAt);
g2.fill(rect);
g2.transform(saveXform);
}
public static void main(String s[]) {
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
TransformDemo p = new TransformDemo();
f.getContentPane().add("Center", p);
p.init();
f.pack();
f.setSize(new Dimension(300, 300));
f.show();
}
}
|
|
|
Этот совет Java демонстрирует метод рисования по Buffered изображения. Это включает в себя создание графического контекста на Buffered изображения.
// No. 1
// Create a graphics context on the buffered image
Graphics2D g2d = bimage.createGraphics();
// Draw on the buffered image
g2d.setColor(Color.red);
g2d.fill(new Ellipse2D.Float(0, 0, 200, 100));
g2d.dispose();
// No.2
// In case the buffered image supports transparency
g2d = bimage.createGraphics();
// Transparency is created on all the filled pixels
Color transparent = new Color(0, 0, 0, 0);
g2d.setColor(transparent);
g2d.setComposite(AlphaComposite.Src);
g2d.fill(new Rectangle2D.Float(20, 20, 100, 20));
g2d.dispose();
|
|
|
Этот совет Java демонстрирует методы выполнения различных операций, таких как масштабирование, стрижки, перевод и вращающиеся фигуры. Эти операции могут помочь разработчику, чтобы новая форма подходит для приложений.
AffineTransform transform = new AffineTransform();
transform.scale(scalex, scaley);
transform.shear(shiftx, shifty);
transform.translate(x, y);
transform.rotate(radians);
Shape newShape = transform.createTransformedShape(shape);
|
|

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class TransformTransRotation extends JComponent {
Shape axes, shape;
int length = 54, arrowLength = 4, tickSize = 4;
public TransformTransRotation() {
axes = createAxes();
shape = createShape();
}
protected Shape createAxes() {
GeneralPath path = new GeneralPath();
// Axes.
path.moveTo(-length, 0);
path.lineTo(length, 0);
path.moveTo(0, -length);
path.lineTo(0, length);
// Arrows.
path.moveTo(length - arrowLength, -arrowLength);
path.lineTo(length, 0);
path.lineTo(length - arrowLength, arrowLength);
path.moveTo(-arrowLength, length - arrowLength);
path.lineTo(0, length);
path.lineTo(arrowLength, length - arrowLength);
// Half-centimeter tick marks
float cm = 72 / 2.54f;
float lengthCentimeter = length / cm;
for (float i = 0.5f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize / 2);
path.lineTo(tick, tickSize / 2);path.moveTo(-tick, -tickSize / 2);
path.lineTo(-tick, tickSize / 2);
path.moveTo(-tickSize / 2, tick);
path.lineTo(tickSize / 2, tick);
path.moveTo(-tickSize / 2, -tick);
path.lineTo(tickSize / 2, -tick);
}
// Full-centimeter tick marks
for (float i = 1.0f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize);
path.lineTo(tick, tickSize);
path.moveTo(-tick, -tickSize);
path.lineTo(-tick, tickSize);
path.moveTo(-tickSize, tick);
path.lineTo(tickSize, tick);
path.moveTo(-tickSize, -tick);
path.lineTo(tickSize, -tick);
}
return path;
}
protected Shape createShape() {
float cm = 72 / 2.54f;
return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Use antialiasing.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Move the origin to 75, 75.
AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
g2.transform(at);
// Draw the shapes in their original locations.
g2.setPaint(Color.black);
g2.draw(axes);
g2.draw(shape);
// Transform the Graphics2D.
AffineTransform rat = new AffineTransform();
rat.setToTranslation(100, 0);
rat.rotate(Math.PI / 6);
g2.transform(rat);
// Draw the "new" shapes in dashed.
Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0);
g2.setStroke(stroke);
g2.draw(axes);
g2.draw(shape);
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.getContentPane().add(new TransformTransRotation());
f.setSize(450, 350);
f.show();
}
}
|
|
|
Демонстрация стиля Java2D линии.

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Stroke;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** A demonstration of Java2D line styles */
public class LineStyles extends JPanel{
public String getName() {
return "LineStyles";
}
public int getWidth() {
return 450;
}
public int getHeight() {
return 180;
}
int[] xpoints = new int[] { 0, 50, 100 }; // X coordinates of our shape
int[] ypoints = new int[] { 75, 0, 75 }; // Y coordinates of our shape
// Here are three different line styles we will demonstrate
// They are thick lines with different cap and join styles
Stroke[] linestyles = new Stroke[] {
new BasicStroke(25.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL),
new BasicStroke(25.0f, BasicStroke.CAP_SQUARE,
BasicStroke.JOIN_MITER),
new BasicStroke(25.0f, BasicStroke.CAP_ROUND,
BasicStroke.JOIN_ROUND), };
// Another line style: a 2 pixel-wide dot-dashed line
Stroke thindashed = new BasicStroke(2.0f, // line width
/* cap style */BasicStroke.CAP_BUTT,
/* join style, miter limit */BasicStroke.JOIN_BEVEL, 1.0f,
/* the dash pattern */new float[] { 8.0f, 3.0f, 2.0f, 3.0f },
/* the dash phase */0.0f); /* on 8, off 3, on 2, off 3 */
// Labels to appear in the diagram, and the font to use to display them.
Font font = new Font("Helvetica", Font.BOLD, 12);
String[] capNames = new String[] { "CAP_BUTT", "CAP_SQUARE", "CAP_ROUND" };
String[] joinNames = new String[] { "JOIN_BEVEL", "JOIN_MITER",
"JOIN_ROUND" };
/** This method draws the example figure */
public void paint(Graphics g1) {
Graphics2D g = (Graphics2D) g1;
// Use anti-aliasing to avoid "jaggies" in the lines
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Define the shape to draw
GeneralPath shape = new GeneralPath();
shape.moveTo(xpoints[0], ypoints[0]); // start at point 0
shape.lineTo(xpoints[1], ypoints[1]); // draw a line to point 1
shape.lineTo(xpoints[2], ypoints[2]); // and then on to point 2
// Move the origin to the right and down, creating a margin
g.translate(20, 40);
// Now loop, drawing our shape with the three different line styles
for (int i = 0; i < linestyles.length; i++) {
g.setColor(Color.gray); // Draw a gray line
g.setStroke(linestyles[i]); // Select the line style to use
g.draw(shape); // Draw the shape
g.setColor(Color.black); // Now use black
g.setStroke(thindashed); // And the thin dashed line
g.draw(shape); // And draw the shape again.
// Highlight the location of the vertexes of the shape
// This accentuates the cap and join styles we're demonstrating
for (int j = 0; j < xpoints.length; j++)
g.fillRect(xpoints[j] - 2, ypoints[j] - 2, 5, 5);
g.drawString(capNames[i], 5, 105); // Label the cap style
g.drawString(joinNames[i], 5, 120); // Label the join style
g.translate(150, 0); // Move over to the right before looping again
}
}
public static void main(String[] a){
JFrame f = new JFrame();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.setContentPane(new LineStyles());
f.setSize(450,200);
f.setVisible(true);
}
}
|
|

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class XORRectangles extends JPanel{
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// using white as the XOR color.
g2.setXORMode(Color.white);
// Paint a red rectangle.
Rectangle2D r = new Rectangle2D.Double(50, 50, 150, 100);
g2.setPaint(Color.red);
g2.fill(r);
g2.transform(AffineTransform.getTranslateInstance(25, 25));
// Draw a blue rectangle.
g2.setPaint(Color.blue);
g2.fill(r);
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(new XORRectangles());
f.setSize(300, 200);
f.setVisible(true);
}
}
|
|

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Shape;
import java.awt.Stroke;
import java.awt.geom.AffineTransform;
import java.awt.geom.GeneralPath;
import java.awt.geom.Rectangle2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class TransformTranslatedRotation extends JComponent {
Shape axes, shape;
int length = 54, arrowLength = 4, tickSize = 4;
public TransformTranslatedRotation() {
axes = createAxes();
shape = createShape();
}
protected Shape createAxes() {
GeneralPath path = new GeneralPath();
// Axes.
path.moveTo(-length, 0);
path.lineTo(length, 0);
path.moveTo(0, -length);
path.lineTo(0, length);
// Arrows.
path.moveTo(length - arrowLength, -arrowLength);
path.lineTo(length, 0);
path.lineTo(length - arrowLength, arrowLength);
path.moveTo(-arrowLength, length - arrowLength);
path.lineTo(0, length);
path.lineTo(arrowLength, length - arrowLength);
// Half-centimeter tick marks
float cm = 72 / 2.54f;
float lengthCentimeter = length / cm;
for (float i = 0.5f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize / 2);
path.lineTo(tick, tickSize / 2);
path.moveTo(-tick, -tickSize / 2);
path.lineTo(-tick, tickSize / 2);
path.moveTo(-tickSize / 2, tick);
path.lineTo(tickSize / 2, tick);
path.moveTo(-tickSize / 2, -tick);
path.lineTo(tickSize / 2, -tick);
}
// Full-centimeter tick marks
for (float i = 1.0f; i < lengthCentimeter; i += 1.0f) {
float tick = i * cm;
path.moveTo(tick, -tickSize);
path.lineTo(tick, tickSize);
path.moveTo(-tick, -tickSize);
path.lineTo(-tick, tickSize);
path.moveTo(-tickSize, tick);
path.lineTo(tickSize, tick);
path.moveTo(-tickSize, -tick);
path.lineTo(tickSize, -tick);
}
return path;
}
protected Shape createShape() {
float cm = 72 / 2.54f;
return new Rectangle2D.Float(cm, cm, 2 * cm, cm);
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
// Use antialiasing.
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
// Move the origin to 75, 75.
AffineTransform at = AffineTransform.getTranslateInstance(75, 75);
g2.transform(at);
// Draw the shapes in their original locations.
g2.setPaint(Color.black);
g2.draw(axes);
g2.draw(shape);
// Transform the Graphics2D.
float cm = 72 / 2.54f;
g2.transform(AffineTransform.getRotateInstance(-Math.PI / 6, 3 * cm, 2 * cm));
// Draw the "new" shapes in dashed.
Stroke stroke = new BasicStroke(1, BasicStroke.CAP_BUTT,
BasicStroke.JOIN_BEVEL, 0, new float[] { 3, 1 }, 0);
g2.setStroke(stroke);
g2.draw(axes);
g2.draw(shape);
}
public static void main(String[] a) {
JFrame f = new JFrame();
f.getContentPane().add(new TransformTranslatedRotation());
f.setSize(450, 350);
f.show();
}
}
|
|