Java Tutorial/2D Graphics/Ellipse — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Версия 17:44, 31 мая 2010
Содержание
Compares two ellipses and returns true if they are equal or both null.
import java.awt.geom.Ellipse2D;
public class Main {
/**
* Compares two ellipses and returns <code>true</code> if they are equal or
* both <code>null</code>.
*
* @param e1 the first ellipse (<code>null</code> permitted).
* @param e2 the second ellipse (<code>null</code> permitted).
*
* @return A boolean.
*/
public static boolean equal(final Ellipse2D e1, final Ellipse2D e2) {
if (e1 == null) {
return (e2 == null);
}
if (e2 == null) {
return false;
}
if (!e1.getFrame().equals(e2.getFrame())) {
return false;
}
return true;
}
}
Create an complex shape by rotating an ellipse.
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class RotateTransformed extends JPanel {
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
Ellipse2D e = new Ellipse2D.Double(0, 0, 80, 130);
for (double i = 0; i < 360; i += 5) {
AffineTransform at = AffineTransform.getTranslateInstance(400 / 2, 400 / 2);
at.rotate(Math.toRadians(i));
g2.draw(at.createTransformedShape(e));
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.add(new RotateTransformed());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400, 400);
frame.setVisible(true);
}
}
Create an ellipse, and then draws it several times at different rotations
import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JComponent;
import javax.swing.JFrame;
public class RotateStrokeEllipse extends JFrame {
public static void main(String[] args) {
new RotateStrokeEllipse();
}
public RotateStrokeEllipse() {
this.setSize(300, 300);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.add(new PaintSurface(), BorderLayout.CENTER);
this.setVisible(true);
}
private class PaintSurface extends JComponent {
public PaintSurface() {
}
public void paint(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
int x = 50;
int y = 75;
int width = 200;
int height = 100;
Shape r1 = new Ellipse2D.Float(x, y, width, height);
for (int angle = 0; angle <= 360; angle += 45) {
g2.rotate(Math.toRadians(angle), x + width / 2, y + height / 2);
g2.setPaint(Color.YELLOW);
g2.fill(r1);
g2.setStroke(new BasicStroke(4));
g2.setPaint(Color.BLACK);
g2.draw(r1);
}
}
}
}
Draw Ellipse2D.Float
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JFrame {
public MainClass() {
getContentPane().add(new DrawingCanvas());
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 500);
setVisible(true);
}
public static void main(String arg[]) {
new MainClass();
}
}
class DrawingCanvas extends JPanel {
DrawingCanvas() {
setSize(300, 300);
setBackground(Color.white);
}
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
int w = getSize().width;
int h = getSize().height;
Ellipse2D e = new Ellipse2D.Float(w / 4.0f, h / 4.0f, w / 2.0f, h / 2.0f);
g2.setClip(e);
g2.setColor(Color.red);
g2.fillRect(0, 0, w, h);
}
}