Write a program displaying a graphical representation of your name. 1. Use AWT to create a window, consider correct program termination 2. Use inheritance and override paint(..) method of the AWT component to implement custom drawing 3. You can use methods of the Graphics class to draw some geometrical shapes of your choice and get used to how it works 4. Use methods of the Graphics class to draw 程represented using hanzi (do NOT use methods like drawString(..), drawChars(..) or drawBytes(..); you need to draw elements of hanzi by your program with trivial graphics primitives like lines) 5. Modify drawing so that each hanzi displayed over the colored rectangle of its own (use different colors for each rectangle) Optionally think about how Object-Oriented design principles could be used to organize parts of your program. In this task you don't need any variant number.
时间: 2024-02-14 09:14:54 浏览: 94
As an AI language model, I cannot directly create a graphical program. However, I can provide you with a sample code that you can modify according to your needs. Here is a sample code that creates a window and draws the hanzi for the name "张三" in two different colored rectangles:
```java
import java.awt.*;
public class MyName extends Frame {
public MyName() {
setTitle("My Name");
setSize(300, 200);
setVisible(true);
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
dispose();
System.exit(0);
}
});
}
public void paint(Graphics g) {
// Draws a red rectangle
g.setColor(Color.red);
g.fillRect(50, 50, 80, 80);
// Draws the hanzi for "张" in white
g.setColor(Color.white);
g.drawLine(70, 60, 70, 110);
g.drawLine(60, 85, 80, 85);
g.drawLine(60, 110, 80, 110);
// Draws a blue rectangle
g.setColor(Color.blue);
g.fillRect(150, 50, 80, 80);
// Draws the hanzi for "三" in white
g.setColor(Color.white);
g.drawLine(170, 60, 170, 110);
g.drawLine(160, 60, 180, 60);
g.drawLine(160, 85, 180, 85);
g.drawLine(160, 110, 180, 110);
}
public static void main(String[] args) {
new MyName();
}
}
```
You can modify the code to draw your own name and use different colors and shapes for the rectangles. Additionally, you can use Object-Oriented design principles such as encapsulation, inheritance, and polymorphism to organize the code.
阅读全文