java中drawarc,Java Graphics2D.drawArc方法代碼示例

import java.awt.Graphics2D; //導入方法依賴的package包/類

public static void main(String[] args) {

DrawFrame df = DrawFrame.create("MyDrawing", 200, 300);

Graphics2D g = df.getGraphics2D();

float linewidth = 5.0f;// line width = 5 pixels

g.setStroke(new BasicStroke(linewidth));

g.setColor(Color.BLUE);

g.drawLine(40, 10, 10, 40);

g.draw(new Line2D.Double(70.2, 10.3, 100.4, 40.5));

g.fillOval(10, 60, 30, 30);

g.drawOval(60, 60, 30, 30);

g.fillRoundRect(110, 60, 30, 30, 10, 10);

g.drawRoundRect(160, 60, 30, 30, 10, 10);

g.setColor(Color.GREEN.darker());

g.fillArc(10, 110, 30, 30, 45, 240);

g.fillArc(60, 110, 30, 30, 45 + 240, 360 - 240);

g.fillArc(110, 110, 30, 30, 90, 270);

g.fillArc(160, 110, 30, 30, 270, 270);

g.setColor(Color.MAGENTA);

g.drawArc(10, 160, 30, 30, 45, 240);

g.drawArc(60, 160, 30, 30, 45 + 240, 360 - 240);

g.drawArc(110, 160, 30, 30, 90, 270);

g.drawArc(160, 160, 30, 30, 270, 270);

g.setColor(Color.ORANGE);

g.fillPolygon(new int[] { 10, 40, 10, 40 }, new int[] { 210, 210, 240, 240 }, 4);

g.drawPolygon(new int[] { 60, 90, 60, 90 }, new int[] { 210, 210, 240, 240 }, 4);

g.drawPolyline(new int[] { 110, 140, 110, 140 }, new int[] { 210, 210, 240, 240 }, 4);

g.drawPolyline(new int[] { 160, 160, 190, 190 }, new int[] {240, 210, 240, 210 }, 4);

// Printing texts

g.setColor(Color.BLACK);

g.setFont(new Font("Monospaced", Font.PLAIN, 14));

g.drawString("Drawn with JGraphix", 10, 275);

df.refresh();

System.out.println("done.");

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些基本的代码来绘制动态太极图。首先,我们需要创建一个继承自 Applet 类的 Java 类,然后在该类实现 paint() 方法。在 paint() 方法,我们将使用 Graphics2D 对象来绘制太极图形。 以下代码演示了如何使用 Graphics2D 绘制一个简单的太极图形: ``` import java.applet.Applet; import java.awt.*; public class TaiChi extends Applet implements Runnable { private Thread thread; private Graphics2D g2d; private int centerX, centerY; private int radius; public void init() { setSize(400, 400); setBackground(Color.WHITE); centerX = getWidth() / 2; centerY = getHeight() / 2; radius = Math.min(getWidth(), getHeight()) / 2; } public void start() { thread = new Thread(this); thread.start(); } public void stop() { thread = null; } public void run() { while (thread != null) { repaint(); try { Thread.sleep(50); } catch (InterruptedException e) { e.printStackTrace(); } } } public void paint(Graphics g) { g2d = (Graphics2D) g; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setColor(Color.BLACK); g2d.fillOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g2d.setColor(Color.WHITE); g2d.fillArc(centerX - radius, centerY - radius, radius * 2, radius * 2, 0, 180); g2d.fillArc(centerX - radius, centerY - radius, radius * 2, radius * 2, 180, 180); g2d.setColor(Color.BLACK); g2d.drawOval(centerX - radius, centerY - radius, radius * 2, radius * 2); g2d.setColor(Color.WHITE); g2d.fillOval(centerX - radius / 2, centerY - radius / 2, radius, radius); g2d.setColor(Color.BLACK); g2d.drawOval(centerX - radius / 2, centerY - radius / 2, radius, radius); g2d.setColor(Color.WHITE); g2d.fillArc(centerX - radius / 2, centerY, radius, radius, 0, 180); g2d.setColor(Color.BLACK); g2d.drawArc(centerX - radius / 2, centerY, radius, radius, 0, 180); g2d.setColor(Color.BLACK); g2d.fillArc(centerX - radius / 2, centerY - radius, radius, radius, 0, -180); g2d.setColor(Color.WHITE); g2d.drawArc(centerX - radius / 2, centerY - radius, radius, radius, 0, -180); } } ``` 在上述代码,我们使用了 Graphics2D 对象来绘制太极图形。首先,我们设置了渲染提示来开启反锯齿功能。然后,我们使用 fillOval() 方法绘制了黑色的圆形。接下来,我们使用 fillArc() 方法绘制了两个白色的半圆形,并将它们分别放置在圆形的上半部分和下半部分。最后,我们使用 fillOval() 和 fillArc() 方法绘制了太极图形的小圆和两个小半圆。 在 init() 方法,我们计算了心点和半径的值,以便在 paint() 方法使用。在 start() 方法,我们启动了一个线程来定期调用 repaint() 方法,从而更新太极图形。在 paint() 方法,我们首先获取 Graphics2D 对象并设置了渲染提示,然后绘制了太极图形的各个部分。 运行上述代码,您将看到一个简单的太极图形在 Applet 窗口动态绘制。您可以尝试修改代码来实现更复杂的动态效果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值