如何保存 JPanel 中绘制的线条?

我最初是在寻找一个库来为java制作“鼠标书写”或“手写”签名。我没有找到任何东西,所以我只是想让用户在 JPanel 上的画布上绘制,然后他可以选择保存它、重新绘制它或取消签名。我遇到的问题是,当我尝试将绘制的内容保存在画布中时,我得到一个空的 .jpeg


到目前为止我的代码:


import javax.swing.*;


import java.awt.*;

import java.awt.event.*;

import java.awt.image.BufferedImage;

import java.awt.*;

import java.awt.event.*;


import javax.swing.*;

import javax.swing.colorchooser.*;

import javax.swing.event.*;


import java.awt.geom.Line2D;

import java.io.File;

import java.io.FileInputStream;

import java.io.IOException;

import java.io.InputStream;

import java.sql.Connection;

import java.sql.DriverManager;

import java.sql.PreparedStatement;

import java.util.logging.Logger;


import javax.imageio.ImageIO;



public class AES_Encryption extends JFrame implements ActionListener{




public BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);

//JPanel canvas = new JPanel();


    JPanel buttonPanel = new JPanel();

    Point lastPos = null;

    Point startPos = null;

    Point finishPos = null;

    Graphics g;

    JButton save = new JButton("Save");

    JButton cancel = new JButton("Cancel");

    JButton clear = new JButton("Clear");

    JPanel canvas = new JPanel();

    int changer = 1;

    String path="";


    public AES_Encryption () {



        setLocation(100,100);

        setSize(600,500);

        setTitle("ENCODE SECTION");

        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        canvas.setBackground(Color.WHITE);


        clear.addActionListener(this);

        clear.setActionCommand("clear");


        save.addActionListener(this);

        save.setActionCommand("Save");



        cancel.addActionListener(this);

        cancel.setActionCommand("Cancel");


达令说
浏览 92回答 1
1回答

扬帆大鱼

创建线的坐标时,将它们保存在列表中。&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; List<Point> points = new ArrayList<>(); // instance field.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; canvas.addMouseMotionListener(new MouseMotionListener () {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void mouseDragged (MouseEvent m) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point p = m.getPoint() ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (changer==1){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; points.add(lastPos);// add it here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lastPos = p ;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public void mouseMoved (MouseEvent m) {}&nbsp; &nbsp; &nbsp; &nbsp; });这是一个例子。您需要确定放置points.add()代码的位置。在这里查看绘画示例编辑:这是如何在窗口中绘图的示例。不要在绘画环境之外使用图形上下文,例如paintor paintComponent(例外情况是buffered images,etc 不在 之内绘画EDT)。&nbsp; &nbsp; import java.awt.BasicStroke;&nbsp; &nbsp; import java.awt.Color;&nbsp; &nbsp; import java.awt.Dimension;&nbsp; &nbsp; import java.awt.Graphics;&nbsp; &nbsp; import java.awt.Graphics2D;&nbsp; &nbsp; import java.awt.Point;&nbsp; &nbsp; import java.awt.RenderingHints;&nbsp; &nbsp; import java.util.List;&nbsp; &nbsp; import java.util.Random;&nbsp; &nbsp; import java.util.stream.Collectors;&nbsp; &nbsp; import java.util.stream.IntStream;&nbsp; &nbsp; import javax.swing.JFrame;&nbsp; &nbsp; import javax.swing.JPanel;&nbsp; &nbsp; import javax.swing.SwingUtilities;&nbsp; &nbsp; public class ExampleDrawDemo extends JPanel {&nbsp; &nbsp; &nbsp; &nbsp;int&nbsp; &nbsp; &nbsp; &nbsp; WIDTH&nbsp; = 600;&nbsp; &nbsp; &nbsp; &nbsp;int&nbsp; &nbsp; &nbsp; &nbsp; HEIGHT = 500;&nbsp; &nbsp; &nbsp; &nbsp;JFrame&nbsp; &nbsp; &nbsp;frame&nbsp; = new JFrame();&nbsp; &nbsp; &nbsp; &nbsp;List<Line> lines;&nbsp; &nbsp; &nbsp; &nbsp;public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; SwingUtilities.invokeLater(() -> new ExampleDrawDemo().start());&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public void start() {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Random r = new Random();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color[] color = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.ORANGE,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color.CYAN&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // generate some lines.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; lines = IntStream.range(0, 100).mapToObj(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i -> new Line(r, color[r.nextInt(color.length)])).collect(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Collectors.toList());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; setPreferredSize(new Dimension(600, 500));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.add(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.pack();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setLocationRelativeTo(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; frame.setVisible(true);&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;public void paintComponent(Graphics g) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; super.paintComponent(g);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Graphics2D g2d = (Graphics2D) g.create();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // smooth lines&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;RenderingHints.VALUE_ANTIALIAS_ON);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // line thickness&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.setStroke(new BasicStroke(2));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (Line line : lines) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;g2d.setColor(line.color);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;g2d.drawLine(line.start.x, line.start.y, line.end.x, line.end.y);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; g2d.dispose();&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;class Line {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point start;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Point end;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Color color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; public Line(Random r, Color color) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;this.color = color;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;start = new Point(r.nextInt(WIDTH), r.nextInt(HEIGHT));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end = new Point(r.nextInt(WIDTH), r.nextInt(HEIGHT));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; }
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java