Java 一个简单的绘制类似花瓣的程序

 绘制椭圆说一下:

ellipse = new Ellipse2D.Float(10, 0, 50, 15);// 创建椭圆对象

(10,0) 为椭圆外接矩形左上角坐标,50 为外接矩形的长,15为宽

package com.wk

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class DrawFlowerFrame extends JFrame {
    DrawFlowerPanel drawFlowerPanel = new DrawFlowerPanel(); // 创建面板类的实例
    public static void main(String args[]) { // 主方法
        DrawFlowerFrame frame = new DrawFlowerFrame(); // 创建窗体类的实例
        frame.setVisible(true); // 显示窗体
    }
    
    public DrawFlowerFrame() {
        super(); // 调用超类的构造方法
        setTitle("绘制花瓣"); // 窗体标题
        setBounds(100, 100, 338, 230); // 窗体的显示位置和大小
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 窗体关闭方式
        add(drawFlowerPanel); // 将面板类的实例添加到窗体容器中
    }
    
    class DrawFlowerPanel extends JPanel { // 创建内部面板类
        public void paint(Graphics g) {     // 重写paint()方法
            Graphics2D g2 = (Graphics2D)g; // 获得Graphics2D对象
            g2.translate(drawFlowerPanel.getWidth() / 2, drawFlowerPanel.getHeight() / 2);// 平移坐标轴
            // 绘制绿色花瓣
            Ellipse2D.Float ellipse = new Ellipse2D.Float(30, 0, 70, 20);// 创建椭圆对象
            Color color = new Color(0,255,0);//创建颜色对象
            g2.setColor(color);//指定颜色
            g2.fill(ellipse);// 绘制椭圆
            int i=0;
            while (i<8){
                g2.rotate(30);// 旋转画布
                g2.fill(ellipse);// 绘制椭圆
                i++;
            }
            // 绘制红色花瓣
            ellipse = new Ellipse2D.Float(20, 0, 60, 15);// 创建椭圆对象
            color = new Color(255,0,0);//创建颜色对象
            g2.setColor(color);//指定颜色
            g2.fill(ellipse);// 绘制椭圆
            i=0;
            while (i<15){
                g2.rotate(75);// 旋转画布
                g2.fill(ellipse);// 绘制椭圆
                i++;
            }
            // 绘制黄色花瓣
            ellipse = new Ellipse2D.Float(10, 0, 50, 15);// 创建椭圆对象
            color = new Color(255,255,0);//创建颜色对象
            g2.setColor(color);//指定颜色
            g2.fill(ellipse);// 绘制椭圆
            i=0;
            while (i<8){
                g2.rotate(30);// 旋转画布
                g2.fill(ellipse);// 绘制椭圆
                i++;
            }
            // 绘制红色中心点
            color = new Color(255, 0, 0);// 创建颜色对象
            g2.setColor(color);// 指定颜色
            ellipse = new Ellipse2D.Float(-10, -10, 20, 20);// 创建椭圆对象
            g2.fill(ellipse);// 绘制椭圆
        }
    }
}

 

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单Java应用程序,可以绘制旋转的风扇,你可以根据自己的需要进行修改和调整。 ```java import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Fan extends JFrame { private FanPanel fanPanel; public static void main(String[] args) { Fan fan = new Fan(); fan.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); fan.setSize(400, 400); fan.setVisible(true); } public Fan() { super("Fan"); fanPanel = new FanPanel(); add(fanPanel); } private class FanPanel extends JPanel implements ActionListener { private Timer timer; private int angle; public FanPanel() { timer = new Timer(10, this); timer.start(); } public void actionPerformed(ActionEvent e) { angle += 1; if (angle >= 360) { angle = 0; } repaint(); } public void paintComponent(Graphics g) { super.paintComponent(g); int x = getWidth() / 2; int y = getHeight() / 2; int radius = Math.min(x, y) - 20; int[] xPoints = new int[4]; int[] yPoints = new int[4]; for (int i = 0; i < 4; i++) { xPoints[i] = x + (int)(radius * Math.cos(Math.toRadians(angle + i * 90))); yPoints[i] = y - (int)(radius * Math.sin(Math.toRadians(angle + i * 90))); } g.setColor(Color.RED); g.fillPolygon(xPoints, yPoints, 4); g.setColor(Color.BLACK); g.drawOval(x - radius, y - radius, radius * 2, radius * 2); } } } ``` 代码中使用了一个 `JPanel` 来绘制风扇,通过一个 `Timer` 定时器来定时更新风扇的角度。在 `paintComponent` 方法中,通过计算三角函数来确定风扇的四个点的坐标,并使用 `fillPolygon` 方法来绘制风扇的叶片。最后使用 `drawOval` 方法来绘制风扇的圆形轮廓。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值