创建2个窗体,分别实现以下内容:1.通过设置Font类和颜色,实现效果如下: 要求:在一个窗体中重写pait()函数,用drawSting()方式实现。2.涂鸦板是一个具有趣味性的小程序,它的实现原理是通过鼠标事件,控制鼠标移动结合画图功能实现。鼠标拖拽才开始绘制,松开鼠标则停止绘制实现的java源代码
时间: 2024-02-24 10:57:55 浏览: 144
很抱歉,我是一个自然语言处理的AI语言模型,无法提供具体的Java代码实现。但是,我可以为您提供一些思路和参考。
1. 创建一个窗体,设置窗体的字体和颜色:
```java
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DrawStringFrame extends JFrame {
public DrawStringFrame() {
setTitle("DrawString Example");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new DrawStringPanel());
}
public static void main(String[] args) {
DrawStringFrame frame = new DrawStringFrame();
frame.setVisible(true);
}
}
class DrawStringPanel extends JPanel {
@Override
public void paint(Graphics g) {
super.paint(g);
g.setFont(new Font("Arial", Font.BOLD, 20));
g.setColor(Color.RED);
g.drawString("Hello, World!", 100, 100);
}
}
```
2. 创建一个涂鸦板窗体,用鼠标事件实现绘图:
```java
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DoodleBoardFrame extends JFrame {
public DoodleBoardFrame() {
setTitle("Doodle Board Example");
setSize(400, 300);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(new DoodleBoardPanel());
}
public static void main(String[] args) {
DoodleBoardFrame frame = new DoodleBoardFrame();
frame.setVisible(true);
}
}
class DoodleBoardPanel extends JPanel {
private ArrayList<Point> points = new ArrayList<Point>();
public DoodleBoardPanel() {
addMouseListener(new MouseAdapter() {
@Override
public void mousePressed(MouseEvent e) {
points.clear();
points.add(e.getPoint());
}
@Override
public void mouseReleased(MouseEvent e) {
points.add(e.getPoint());
}
});
addMouseMotionListener(new MouseAdapter() {
@Override
public void mouseDragged(MouseEvent e) {
points.add(e.getPoint());
repaint();
}
});
}
@Override
public void paint(Graphics g) {
super.paint(g);
g.setColor(Color.BLACK);
for (int i = 1; i < points.size(); i++) {
Point p1 = points.get(i - 1);
Point p2 = points.get(i);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
}
}
}
```
这是一个简单的涂鸦板实现,通过鼠标事件控制绘图。鼠标按下时清空points,鼠标拖拽时添加points并重绘,鼠标松开时再添加一个点。paint()函数中遍历points,依次连接相邻的点。
阅读全文