用Java生成一个画出哆啦a梦的JFrome窗口
时间: 2024-04-21 19:24:29 浏览: 10
以下是使用Java生成一个画出哆啦A梦的JFrame窗口的示例代码:
```java
import javax.swing.*;
import java.awt.*;
public class DoraemonFrame extends JFrame {
public DoraemonFrame() {
setTitle("哆啦A梦");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
DoraemonPanel panel = new DoraemonPanel();
add(panel);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> {
DoraemonFrame frame = new DoraemonFrame();
frame.setVisible(true);
});
}
}
class DoraemonPanel extends JPanel {
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.blue);
g.fillOval(100, 100, 300, 300); // 头部
g.setColor(Color.white);
g.fillOval(150, 180, 50, 50); // 左眼白色部分
g.fillOval(300, 180, 50, 50); // 右眼白色部分
g.setColor(Color.black);
g.fillOval(170, 200, 20, 20); // 左眼黑色部分
g.fillOval(320, 200, 20, 20); // 右眼黑色部分
g.setColor(Color.red);
g.fillArc(180, 250, 140, 100, 180, 180); // 嘴巴
g.setColor(Color.white);
g.fillRect(190, 100, 120, 150); // 胸部
g.setColor(Color.blue);
g.fillRect(190, 130, 120, 30); // 铃铛
g.setColor(Color.white);
g.fillOval(230, 100, 40, 40); // 左手
g.fillOval(230, 230, 40, 40); // 左脚
g.setColor(Color.blue);
g.fillRect(230, 100, 40, 70); // 左手袖子
g.fillRect(230, 230, 40, 70); // 左脚袜子
g.setColor(Color.white);
g.fillOval(230, 170, 40, 70); // 肚子
g.setColor(Color.blue);
g.fillOval(270, 100, 40, 40); // 右手
g.fillOval(270, 230, 40, 40); // 右脚
g.setColor(Color.white);
g.fillRect(270, 100, 40, 70); // 右手袖子
g.fillRect(270, 230, 40, 70); // 右脚袜子
g.setColor(Color.blue);
g.fillOval(270, 170, 40, 70); // 肚子下部分
g.setColor(Color.red);
g.fillRect(210, 250, 80, 30); // 口袋
}
}
```
这段代码会生成一个名为`哆啦A梦`的JFrame窗口,窗口大小为500x500像素,绘制了哆啦A梦的头部、眼睛、嘴巴、胸部、手、脚和口袋等元素,颜色和位置可以根据需要进行调整。运行该代码后,会显示出哆啦A梦的图像。