JAVA 中的fillOval方法和drawline方法

java的 fillOval方法 和 drawLine 方法他们的坐标是怎么计算的?

2012-01-11 18:57 java菜鸟逢  |  浏览 5809 次
下面是我画的2个图,不知道画的对不对,如果对的话那要怎么计算啊? fillOval方法 : (计算那个圆的坐标是怎么算出来)



" target="_blank" >





drawLine()方法 :(算圆右上角的那条线是怎么算出来的)



" target="_blank" >





注意!!!那个fillOval方法那条线是不要的,贴错了!
答案:
fillOval(30,0,20,30);

drawLine(40,15,50,0);

注释:
1)fillOval
public abstract void fillOval(int x,
                              int y,
                              int width,
                              int height)使用当前颜色填充外接指定矩形框的椭圆。 

参数:
x - 要填充椭圆的左上角的 x 坐标。
y - 要填充椭圆的左上角的 y 坐标。
width - 要填充椭圆的宽度。
height - 要填充椭圆的高度。

2)drawLine
public abstract void drawLine(int x1,
                              int y1,
                              int x2,
                              int y2)在此图形上下文的坐标系中,使用当前颜色在点 (x1, y1) 和 (x2, y2) 之间画一条线。 

参数:
x1 - 第一个点的 x 坐标。
y1 - 第一个点的 y 坐标。
x2 - 第二个点的 x 坐标。
y2 - 第二个点的 y 坐标。
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java实现时钟表盘可以使用Java的Swing组件来绘制。下面是一个简单的示例: ```java import javax.swing.*; import java.awt.*; public class ClockFace extends JPanel { private int hours = 0; private int minutes = 0; private int seconds = 0; private int radius = 0; public ClockFace(int hours, int minutes, int seconds, int radius) { this.hours = hours; this.minutes = minutes; this.seconds = seconds; this.radius = radius; } public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D) g; int width = getWidth(); int height = getHeight(); // 绘制表盘 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.WHITE); g2.fillOval(0, 0, 2 * radius, 2 * radius); // 绘制小时刻度 g2.setColor(Color.BLACK); for (int i = 0; i < 12; i++) { double angle = Math.PI / 6 * i; int x1 = (int) (radius * Math.sin(angle) + radius); int y1 = (int) (radius * Math.cos(angle) + radius); int x2 = (int) ((radius - 15) * Math.sin(angle) + radius); int y2 = (int) ((radius - 15) * Math.cos(angle) + radius); g2.drawLine(x1, y1, x2, y2); } // 绘制分钟刻度 g2.setColor(Color.LIGHT_GRAY); for (int i = 0; i < 60; i++) { if (i % 5 != 0) { double angle = Math.PI / 30 * i; int x1 = (int) (radius * Math.sin(angle) + radius); int y1 = (int) (radius * Math.cos(angle) + radius); int x2 = (int) ((radius - 5) * Math.sin(angle) + radius); int y2 = (int) ((radius - 5) * Math.cos(angle) + radius); g2.drawLine(x1, y1, x2, y2); } } // 绘制时针 g2.setColor(Color.BLACK); double angle = Math.PI / 6 * ((hours % 12) + minutes / 60.0); int x = (int) ((radius - 50) * Math.sin(angle) + radius); int y = (int) ((radius - 50) * Math.cos(angle) + radius); g2.setStroke(new BasicStroke(3.0f)); g2.drawLine(radius, radius, x, y); // 绘制分针 g2.setColor(Color.BLACK); angle = Math.PI / 30 * minutes; x = (int) ((radius - 30) * Math.sin(angle) + radius); y = (int) ((radius - 30) * Math.cos(angle) + radius); g2.setStroke(new BasicStroke(2.0f)); g2.drawLine(radius, radius, x, y); // 绘制秒针 g2.setColor(Color.RED); angle = Math.PI / 30 * seconds; x = (int) ((radius - 20) * Math.sin(angle) + radius); y = (int) ((radius - 20) * Math.cos(angle) + radius); g2.setStroke(new BasicStroke(1.0f)); g2.drawLine(radius, radius, x, y); } public static void main(String[] args) { JFrame frame = new JFrame("Clock"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(400, 400); frame.setLocationRelativeTo(null); ClockFace clockFace = new ClockFace(12, 30, 0, 150); frame.getContentPane().add(clockFace); frame.setVisible(true); } } ``` ClockFace继承JPanel,并重写了paintComponent方法,该方法用于绘制时钟表盘。在构造函数,传入了时、分、秒和表盘半径等参数。在paintComponent方法,首先绘制了表盘圆形,然后绘制了小时刻度和分钟刻度。接着,根据时、分、秒计算出时针、分针和秒针的坐标,并绘制出来。在main方法,创建一个JFrame窗口,并将ClockFace实例添加到窗口。最后设置窗口的可见性即可。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值