创建2个窗体,分别实现以下内容:1.绘制文本,通过设置Font类和颜色,实现效果如下:要求:在一个窗体中重写pait()函数,用drawSting()方式实现。2.涂鸦板是一个具有趣味性的小程序,它的实现原理是通过鼠标事件,控制鼠标移动结合画图功能实现。鼠标拖拽才开始绘制,松开鼠标则停止绘制。请用Java图形界面的知识,编写以上软件。
时间: 2024-02-12 13:06:51 浏览: 71
VB2010完美代码库
4星 · 用户满意度95%
好的,我会根据你的要求为你解答。以下是实现代码:
1. 绘制文本窗体:
```
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TextFrame extends JPanel {
public void paint(Graphics g) {
super.paint(g);
Font font = new Font("Arial", Font.BOLD, 30);
g.setFont(font);
g.setColor(Color.BLUE);
g.drawString("Hello World!", 50, 50);
}
public static void main(String[] args) {
JFrame frame = new JFrame("Text Frame");
frame.add(new TextFrame());
frame.setSize(300, 200);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
2. 涂鸦板窗体:
```
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DoodleBoard extends JPanel {
private Point startPoint, endPoint;
public DoodleBoard() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
startPoint = e.getPoint();
}
public void mouseReleased(MouseEvent e) {
endPoint = null;
}
});
addMouseMotionListener(new MouseAdapter() {
public void mouseDragged(MouseEvent e) {
endPoint = e.getPoint();
repaint();
}
});
}
public void paint(Graphics g) {
super.paint(g);
if (startPoint != null && endPoint != null) {
g.setColor(Color.RED);
g.drawLine(startPoint.x, startPoint.y, endPoint.x, endPoint.y);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame("Doodle Board");
frame.add(new DoodleBoard());
frame.setSize(400, 400);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
```
希望能对你有所帮助!
阅读全文