Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
生活随笔
收集整理的這篇文章主要介紹了
Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
控制臺程序。
1 import javax.swing.JComponent;
2 import java.util.*;
3 import java.awt.*;
4 import java.awt.geom.*;
5
6 @SuppressWarnings("serial")
7 public class SketcherView extends JComponent implements Observer {
8 public SketcherView(Sketcher theApp) {
9 this.theApp = theApp;
10 }
11
12 // Method called by Observable object when it changes
13 public void update(Observable o, Object rectangle) {
14 // Code to respond to changes in the model...
15 }
16
17 // Method to draw on the view
18 @Override
19 public void paint(Graphics g) {
20 // Temporary code...
21 Graphics2D g2D = (Graphics2D)g; // Get a Java 2D device context
22 Point2D.Double position = new Point2D.Double(50,10); // Initial position
23 double width = 150; // Width of ellipse
24 double height = 100; // Height of ellipse
25 double start = 30; // Start angle for arc
26 double extent = 120; // Extent of arc
27 double diameter = 40; // Diameter of circle
28
29 // Define open arc as an upper segment of an ellipse
30 Arc2D.Double top = new Arc2D.Double(position.x, position.y,
31 width, height,
32 start, extent,
33 Arc2D.OPEN);
34
35 // Define open arc as lower segment of ellipse shifted up relative to 1st
36 Arc2D.Double bottom = new Arc2D.Double(
37 position.x, position.y - height + diameter,
38 width, height,
39 start + 180, extent,
40 Arc2D.OPEN);
41
42 // Create a circle centered between the two arcs
43 Ellipse2D.Double circle1 = new Ellipse2D.Double(
44 position.x + width/2 - diameter/2,position.y,
45 diameter, diameter);
46
47 // Create a second circle concentric with the first and half the diameter
48 Ellipse2D.Double circle2 = new Ellipse2D.Double(
49 position.x + width/2 - diameter/4, position.y + diameter/4,
50 diameter/2, diameter/2);
51
52 // Draw all the shapes
53 g2D.setPaint(Color.BLACK); // Draw in black
54 g2D.draw(top);
55 g2D.draw(bottom);
56
57 g2D.setPaint(Color.BLUE); // Draw in blue
58 g2D.draw(circle1);
59 g2D.draw(circle2);
60 g2D.drawString("Arcs and ellipses", 80, 100); // Draw some text
61
62 }
63
64 private Sketcher theApp; // The application object
65 }
其他部分與上一例同。
總結
以上是生活随笔為你收集整理的Java基础之在窗口中绘图——绘制圆弧和椭圆(Sketcher 3 drawing arcs and ellipses)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 财神爷最忌讳看到什么
- 下一篇: Layui 文档 官网镜像