为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

达令说 2023-06-21 16:38:01
我最初是在寻找一个库来为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");
查看完整描述

1 回答

?
扬帆大鱼

TA贡献1799条经验 获得超9个赞

创建线的坐标时,将它们保存在列表中。



            List<Point> points = new ArrayList<>(); // instance field.


            canvas.addMouseMotionListener(new MouseMotionListener () {                      

            public void mouseDragged (MouseEvent m) {

                Point p = m.getPoint() ;

                if (changer==1){

                g.drawLine(lastPos.x, lastPos.y, p.x, p.y) ;

                points.add(lastPos);// add it here.

                }           

                lastPos = p ;


            }   

            public void mouseMoved (MouseEvent m) {}

        });


这是一个例子。您需要确定放置points.add()代码的位置。在这里查看绘画示例


编辑:


这是如何在窗口中绘图的示例。不要在绘画环境之外使用图形上下文,例如paintor paintComponent(例外情况是buffered images,etc 不在 之内绘画EDT)。



    import java.awt.BasicStroke;

    import java.awt.Color;

    import java.awt.Dimension;

    import java.awt.Graphics;

    import java.awt.Graphics2D;

    import java.awt.Point;

    import java.awt.RenderingHints;

    import java.util.List;

    import java.util.Random;

    import java.util.stream.Collectors;

    import java.util.stream.IntStream;


    import javax.swing.JFrame;

    import javax.swing.JPanel;

    import javax.swing.SwingUtilities;


    public class ExampleDrawDemo extends JPanel {


       int        WIDTH  = 600;

       int        HEIGHT = 500;

       JFrame     frame  = new JFrame();

       List<Line> lines;


       public static void main(String[] args) {

          SwingUtilities.invokeLater(() -> new ExampleDrawDemo().start());

       }

       public void start() {

          Random r = new Random();

          Color[] color = {

                Color.RED, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.ORANGE,

                Color.CYAN

          };


          // generate some lines.

          lines = IntStream.range(0, 100).mapToObj(

                i -> new Line(r, color[r.nextInt(color.length)])).collect(

                      Collectors.toList());


          setPreferredSize(new Dimension(600, 500));

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

          frame.add(this);

          frame.pack();

          frame.setLocationRelativeTo(null);

          frame.setVisible(true);

       }


       public void paintComponent(Graphics g) {

          super.paintComponent(g);

          Graphics2D g2d = (Graphics2D) g.create();

          // smooth lines

          g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

                   RenderingHints.VALUE_ANTIALIAS_ON);

          // line thickness

          g2d.setStroke(new BasicStroke(2));


          for (Line line : lines) {

             g2d.setColor(line.color);

             g2d.drawLine(line.start.x, line.start.y, line.end.x, line.end.y);

          }

          g2d.dispose();

       }


       class Line {

          Point start;

          Point end;

          Color color;


          public Line(Random r, Color color) {

             this.color = color;

             start = new Point(r.nextInt(WIDTH), r.nextInt(HEIGHT));

             end = new Point(r.nextInt(WIDTH), r.nextInt(HEIGHT));

          }

       }

    }



查看完整回答
反对 回复 2023-06-21
  • 1 回答
  • 0 关注
  • 91 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信