import java.awt.; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.util.ArrayList; import java.util.List; import javax.swing.; public class Tf extends JFrame { private static final long serialVersionUID = -9207842705924169844L; private static final int width = 500; private static final int height = 500; private MyPanel panel = new MyPanel(); JButton cButton; JPanel panel1; public Tf() { super(); setTitle("三点作图"); setSize(width, height); setLayout(new BorderLayout()); add(panel, BorderLayout.CENTER); panel1 = new JPanel(); cButton = new JButton("清除"); panel1.add(cButton); cButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { panel.repaint(); } }); getContentPane().add(panel); getContentPane().add(panel1, "South"); setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - width) / 2, (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - height) / 2); setDefaultCloseOperation(EXIT_ON_CLOSE); setVisible(true); setResizable(true); } public static void main(String[] args) { new Tf(); } class MyPanel extends JPanel { private static final long serialVersionUID = -701381909057737726L; private List<Point> pl = new ArrayList<Point>(); Graphics g; public MyPanel() { super(); setBackground(Color.WHITE); try { addMouseListener(new MouseAdapter() { @Override public void mouseClicked(MouseEvent e) { // TODO Auto-generated method stub pl.add(e.getPoint()); if (pl.size() == 3) { draw(); pl.clear(); } } }); } catch (Exception e) { System.out.println("捕获异常: " + e); } } private void draw() { if (g == null) g = getGraphics(); try { g.drawLine(pl.get(0).x, pl.get(0).y, pl.get(1).x, pl.get(1).y); g.drawLine(pl.get(1).x, pl.get(1).y, pl.get(2).x, pl.get(2).y); g.drawLine(pl.get(0).x, pl.get(0).y, pl.get(2).x, pl.get(2).y); } catch (Exception e) { System.out.println("捕获异常:" + e); } } } }在这段代码上增加一个保存按钮来保存画完的三角形
时间: 2024-03-01 20:53:14 浏览: 150
好的,我可以为你添加一个保存按钮来保存画完的三角形。以下是修改后的代码:
```java
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.*;
public class Tf extends JFrame {
private static final long serialVersionUID = -9207842705924169844L;
private static final int width = 500;
private static final int height = 500;
private MyPanel panel = new MyPanel();
JButton cButton, sButton; // 添加保存按钮
JPanel panel1;
public Tf() {
super();
setTitle("三点作图");
setSize(width, height);
setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
panel1 = new JPanel();
cButton = new JButton("清除");
sButton = new JButton("保存"); // 添加保存按钮
panel1.add(cButton);
panel1.add(sButton); // 添加保存按钮
cButton.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
panel.repaint();
}
});
sButton.addActionListener(new ActionListener() { // 添加保存按钮的事件监听器
@Override
public void actionPerformed(ActionEvent e) {
saveImage();
}
});
getContentPane().add(panel);
getContentPane().add(panel1, "South");
setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - width) / 2,
(int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - height) / 2);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setResizable(true);
}
/**
* 保存图片的方法
*/
private void saveImage() {
BufferedImage image = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
panel.paint(g2d);
g2d.dispose();
try {
ImageIO.write(image, "png", new File("triangle.png"));
JOptionPane.showMessageDialog(panel, "保存成功!", "提示", JOptionPane.INFORMATION_MESSAGE);
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(panel, "保存失败!", "错误", JOptionPane.ERROR_MESSAGE);
}
}
public static void main(String[] args) {
new Tf();
}
class MyPanel extends JPanel {
private static final long serialVersionUID = -701381909057737726L;
private List<Point> pl = new ArrayList<Point>();
Graphics g;
public MyPanel() {
super();
setBackground(Color.WHITE);
try {
addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
pl.add(e.getPoint());
if (pl.size() == 3) {
draw();
pl.clear();
}
}
});
} catch (Exception e) {
System.out.println("捕获异常: " + e);
}
}
private void draw() {
if (g == null)
g = getGraphics();
try {
g.drawLine(pl.get(0).x, pl.get(0).y, pl.get(1).x, pl.get(1).y);
g.drawLine(pl.get(1).x, pl.get(1).y, pl.get(2).x, pl.get(2).y);
g.drawLine(pl.get(0).x, pl.get(0).y, pl.get(2).x, pl.get(2).y);
} catch (Exception e) {
System.out.println("捕获异常:" + e);
}
}
}
}
```
我添加了一个名为 `sButton` 的按钮,并在 `panel1` 面板中添加它。我还为 `sButton` 添加了一个事件监听器,当用户单击该按钮时,它将调用 `saveImage()` 方法来保存画完的三角形。 `saveImage()` 方法使用 `BufferedImage` 和 `Graphics2D` 类来绘制 `MyPanel` 面板的内容,并将其保存为 `triangle.png` 文件。如果保存成功,将显示一个信息对话框,提示用户保存成功。否则,将显示一个错误对话框,提示用户保存失败。
阅读全文