我想在Java中创建一个简单的Paint

42fyovps  于 2023-11-15  发布在  Java
关注(0)|答案(1)|浏览(91)

我想修改程序,用一条直线连接一系列的点,组成一个字母的一个笔划,这样就可以画出单词,但我真的不知道如何做到这一点,所以我想一些参考。我试图画他们使用drawLine,但它不是我真正想要的。


的数据
这是我的代码:

package tool;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import java.util.Vector;

import javax.swing.JFrame;
import javax.swing.JPanel;

class Point {
    int x, y;

    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }

}

public class MyPaintApp extends JFrame {
    int x, y;
    Vector<Point> list = new Vector<>();

    class MyPanel extends JPanel {
        public MyPanel() {
            addMouseMotionListener(new MouseMotionAdapter() {
                public void mouseDragged(MouseEvent event) {
                    x = event.getX();
                    y = event.getY();
                    list.add(new Point(x, y));
                    repaint();
                }
            });
        }

        public void paintComponent(Graphics g) {

            super.paintComponent(g);
            for (Point p : list)
                g.fillOval(p.x, p.y, 4, 4);
        }
    }

    public MyPaintApp() {
        setSize(600, 450);
        setLocation(400, 0);
        setTitle("My Paint");
        add(new MyPanel());
        setVisible(true);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

个字符

t3irkdon

t3irkdon1#

import java.util.Vector;
import javax.swing.*;

import java.awt.*;

import java.awt.event.*;

public class MyPaintApp extends JFrame {
   Vector<Point> list = new Vector<>();
   Vector<Vector<Point>> list1 = new Vector<>();

   class MyPanel extends JPanel {
      public MyPanel() {
      
         final MouseAdapter mouseAdapter =
            new MouseAdapter() {
               public void mouseDragged(MouseEvent event) {
                  repaint();
                  list.add(event.getPoint());
               }
               public void mouseMoved(MouseEvent event) {
                  repaint();
               }
               public void mouseReleased(MouseEvent event) {
                  if (list.isEmpty()){
                     return;
                  }
               
                  list1.add(list);
                  list = new Vector<>();
                  repaint();
               }
            }
            ;
      
         this.addMouseMotionListener(mouseAdapter);
         this.addMouseListener(mouseAdapter);
      }
   
      public void paintComponent(Graphics g) {
         super.paintComponent(g);
         for (final Vector<Point> listA : list1) {
            for (int i = 0; i < listA.size() - 1; i++) {
               Point p1 = listA.get(i);
               Point p2 = listA.get(i + 1);
               g.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
         }
      }
   }

   @SuppressWarnings("this-escape")
   public MyPaintApp() {
      setSize(600, 450);
      setLocation(400, 0);
      setTitle("My Paint");
      add(new MyPanel());
      setVisible(true);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
   }

   public static void main(String[] args)
   {
   
      new MyPaintApp();
   
   }

}

字符串
在这种情况下,我们甚至没有创建任何你所做的点。我们所做的就是使用MouseEvent.getPoint()从事件中获取点,然后将其添加到我们的Vector。我还将您的MouseMotionAdapter更改为普通的MouseAdapter。这样做给了我们mouseMoved,我只是用来重置Vector并启动一个新的。必须运行,这样我回来后就能回答问题了

相关问题