Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
2013-12-24 00:02
393 查看
控制台程序。
其他部分与上一例同。
import javax.swing.JComponent; import java.util.*; import java.awt.*; import java.awt.geom.*; @SuppressWarnings("serial") public class SketcherView extends JComponent implements Observer { public SketcherView(Sketcher theApp) { this.theApp = theApp; } // Method called by Observable object when it changes public void update(Observable o, Object rectangle) { // Code to respond to changes in the model... } // Method to draw on the view @Override public void paint(Graphics g) { // Temporary code... Graphics2D g2D = (Graphics2D)g; // Get a Java 2D device context Point2D.Double position = new Point2D.Double(50,10); // Initial position double width = 150; // Width of ellipse double height = 100; // Height of ellipse double start = 30; // Start angle for arc double extent = 120; // Extent of arc double diameter = 40; // Diameter of circle // Define open arc as an upper segment of an ellipse Arc2D.Double top = new Arc2D.Double(position.x, position.y, width, height, start, extent, Arc2D.OPEN); // Define open arc as lower segment of ellipse shifted up relative to 1st Arc2D.Double bottom = new Arc2D.Double( position.x, position.y - height + diameter, width, height, start + 180, extent, Arc2D.OPEN); // Create a circle centered between the two arcs Ellipse2D.Double circle1 = new Ellipse2D.Double( position.x + width/2 - diameter/2,position.y, diameter, diameter); // Create a second circle concentric with the first and half the diameter Ellipse2D.Double circle2 = new Ellipse2D.Double( position.x + width/2 - diameter/4, position.y + diameter/4, diameter/2, diameter/2); // Draw all the shapes g2D.setPaint(Color.BLACK); // Draw in black g2D.draw(top); g2D.draw(bottom); g2D.setPaint(Color.BLUE); // Draw in blue g2D.draw(circle1); g2D.draw(circle2); g2D.drawString("Arcs and ellipses", 80, 100); // Draw some text } private Sketcher theApp; // The application object }
其他部分与上一例同。
相关文章推荐
- Java基础之在窗口中绘图——绘制直线和矩形(Sketcher 2 drawing lines and rectangles)
- Java基础之在窗口中绘图——使用模型/视图体系结构在视图中绘图(Sketcher 1 drawing a 3D rectangle)
- Java绘图: 使用Graphics类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
- Java绘图: 使用Graphics类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
- Java基础之在窗口中绘图——利用多态性使用鼠标自由绘图(Sketcher 7 with a crosshair cursor)
- Java基础之在窗口中绘图——绘制曲线(CurveApplet 1)
- Java绘图: 使用Graphics类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
- Java基础之在窗口中绘图——绘制星星(StarApplet 1)
- Java 绘图: 使用 Graphics 类绘制线段、矩形、椭圆/圆弧/扇形、图片、文本
- Java基础之在窗口中绘图——填充星型(StarApplet 2 filled stars)
- Java基础之处理事件——添加菜单图标(Sketcher 8 with toolbar buttons and menu icons)
- Java基础之在窗口中绘图——渐变填充(GradientApplet 1)
- Java基础之创建窗口——向窗口中添加菜单(Sketcher)
- JAVA基础8.1——绘图基础(1)绘制图形
- Java基础之在窗口中绘图——显示曲线的控制点(CurveApplet 2 displaying control points)
- Java基础之在窗口中绘图——移动曲线的控制点(CurveApplet 3 moving the control points)
- JAVA进阶8.2——绘图基础(2)绘制图像
- JAVA进阶8.3——绘图基础(3)绘制文字
- Win32 绘图基础 -- 绘制直线、边框、贝塞尔曲线、填充、裁剪
- java基础学习之面对对面的图形绘制 坦克