Java/2D Graphics GUI/Arc
Версия от 18:01, 31 мая 2010; (обсуждение)
Содержание
Arc2D.CHORD
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
public class MainClass extends Frame {
public static void main(String[] args) {
(new MainClass()).setVisible(true);
}
public MainClass() {
super("Shape Sampler");
setSize(400, 550);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Arc2D arc = new Arc2D.Float(200, 50, 100, 50, 0, 90, Arc2D.CHORD);
g2d.draw(arc);
}
}
Arc2D.Float: Arc2D.OPEN
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
public class MainClass extends Frame {
public static void main(String[] args) {
(new MainClass()).setVisible(true);
}
public MainClass() {
super("Shape Sampler");
setSize(400, 550);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Arc2D arc = new Arc2D.Float(200, 50, 100, 50, 0, 90, Arc2D.OPEN);
g2d.draw(arc);
}
}
Arc2D.PIE
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Arc2D;
public class MainClass extends Frame {
public static void main(String[] args) {
(new MainClass()).setVisible(true);
}
public MainClass() {
super("Shape Sampler");
setSize(400, 550);
}
public void paint(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
Arc2D arc = new Arc2D.Float(200, 50, 100, 50, 0, 90, Arc2D.PIE);
g2d.draw(arc);
}
}
Compares two arcs and returns true if they are equal or both null.
import java.awt.geom.Arc2D;
public class Main {
/**
* Compares two arcs and returns <code>true</code> if they are equal or
* both <code>null</code>.
*
* @param a1 the first arc (<code>null</code> permitted).
* @param a2 the second arc (<code>null</code> permitted).
*
* @return A boolean.
*/
public static boolean equal(final Arc2D a1, final Arc2D a2) {
if (a1 == null) {
return (a2 == null);
}
if (a2 == null) {
return false;
}
if (!a1.getFrame().equals(a2.getFrame())) {
return false;
}
if (a1.getAngleStart() != a2.getAngleStart()) {
return false;
}
if (a1.getAngleExtent() != a2.getAngleExtent()) {
return false;
}
if (a1.getArcType() != a2.getArcType()) {
return false;
}
return true;
}
}
Draw draw an arc outline
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(400, 400);
f.add(new MainClass());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
g.setColor (Color.yellow);
g.drawArc (5, 15, 50, 75, 25, 165);
}
}
Fill an arc outline
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MainClass extends JPanel {
public static void main(String[] a) {
JFrame f = new JFrame();
f.setSize(400, 400);
f.add(new MainClass());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
}
public void paint(Graphics g) {
g.setColor (Color.yellow);
g.fillArc (5, 15, 50, 75, 25, 165);
}
}