1.编写代码实现以下绘图: (1)一个窗口: 1)窗口标题是“MyfirstJFrame”,调用方法:setTitle()。 2)窗口初始大小800x500,调用方法:setSize()。 3)使用CJLU的logo(CJLU.gif)(双击压缩包图标打开后解压到代码目录)为窗 口的图标:调用方法:setIconImage(); (2)在上面的程序基础上继续,绘制图形 以下提示假设g2为Graphics2D对象。 1)绘制文字【粉红色(Color.PINK),粗体(Font.BOLD),字体为”Arial”,大小为36,坐 标位置为(200,50)】使用g2.setPaint()设置颜色,使用g2.setFont()设置字体,使用g2.drawString() 绘制字符串。 2)绘制椭圆并填充【绿色(Color.GREEN),其外接矩形左上角坐标(100,100),大小为 (600,300)】创建Ellipse2D对象,使用g2.setPaint()设置颜色,使用g2.draw()绘制Ellipse2D对 象,使用g2.fill()填充Ellipse2D对象。 3)绘制矩形并填充【蓝色(Color.GREEN),左上角坐标(150,200),大小为(200,100)】 与上一小题类似,只是换成了Rectangle2D对象。 4)使用绘制矩形的两条对角线【黑色(Color.BLACK),两条对角线分开画,每一条起点 和终点坐标根据矩形位置和大小计算】。创建Line2D对象,使用g2.setPaint()设置颜色,使 用g2.draw()绘制。 5)绘制圆形【红色(Color.RED),其外接矩形左上角坐标(450,150),大小为(200,200)】 与2)中的椭圆类似,只是把椭圆Ellipse2D对象的宽和高设置为相同,就是圆了。 6)每隔π/4弧度,绘制圆形中的半径线。【黑色(Color.BLACK),每一条半径起点为圆 心(550,250),半径为100。半径终点的坐标为(圆心x坐标+半径*cos(θ),圆心y坐标+半径 *sin(θ)),其中θ为该条半径转过的弧度】。可以用for(弧度......){创建半径线段Line2D对象; 调用g2.draw()绘制半径;}来完成。
时间: 2024-03-13 12:47:55 浏览: 30
很抱歉,我无法编写代码。但我可以给你提供代码实现的思路,你可以参考以下代码:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class MyfirstJFrame extends JFrame {
public MyfirstJFrame() {
setTitle("MyfirstJFrame"); // 设置窗口标题
setSize(800, 500); // 设置窗口大小
ImageIcon icon = new ImageIcon("CJLU.gif"); // 设置窗口图标
Image image = icon.getImage();
setIconImage(image);
JPanel panel = new MyPanel(); // 创建绘图面板
add(panel); // 添加面板到窗口
}
public static void main(String[] args) {
MyfirstJFrame frame = new MyfirstJFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
class MyPanel extends JPanel {
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// 绘制粉红色文字
g2.setPaint(Color.PINK);
Font font = new Font("Arial", Font.BOLD, 36);
g2.setFont(font);
g2.drawString("Hello, World!", 200, 50);
// 绘制绿色椭圆并填充
g2.setPaint(Color.GREEN);
Ellipse2D ellipse = new Ellipse2D.Double(100, 100, 600, 300);
g2.draw(ellipse);
g2.fill(ellipse);
// 绘制蓝色矩形并填充
g2.setPaint(Color.BLUE);
Rectangle2D rect = new Rectangle2D.Double(150, 200, 200, 100);
g2.draw(rect);
g2.fill(rect);
// 绘制黑色矩形对角线
g2.setPaint(Color.BLACK);
double x1 = rect.getX();
double y1 = rect.getY();
double x2 = rect.getX() + rect.getWidth();
double y2 = rect.getY() + rect.getHeight();
Line2D line1 = new Line2D.Double(x1, y1, x2, y2);
Line2D line2 = new Line2D.Double(x1, y2, x2, y1);
g2.draw(line1);
g2.draw(line2);
// 绘制红色圆形
g2.setPaint(Color.RED);
Ellipse2D circle = new Ellipse2D.Double(450, 150, 200, 200);
g2.draw(circle);
g2.fill(circle);
// 绘制黑色圆形半径线
g2.setPaint(Color.BLACK);
double centerX = circle.getCenterX();
double centerY = circle.getCenterY();
double radius = circle.getWidth() / 2;
for (double theta = 0; theta <= Math.PI * 2; theta += Math.PI / 4) {
double x = centerX + radius * Math.cos(theta);
double y = centerY + radius * Math.sin(theta);
Line2D radiusLine = new Line2D.Double(centerX, centerY, x, y);
g2.draw(radiusLine);
}
}
}
```