0

I am making a Java program which is able to draw a curve on JPanel on mouse events.

Now I want to ask after drawing that curve, how can I edit that curve?

This is my Java program:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.QuadCurve2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import javax.swing.*;

public class MouseTestHovercraft extends JPanel {
    private static final int PREF_W = 600;
    private static final int PREF_H = PREF_W;
    private static final int MAX_CLR = 5;
    private static final Color CURRENT_LIST_COLOR = new Color(190, 190, 255);
    private List<Color> colors = new ArrayList<Color>();
    private boolean tracking = false;
    private List<Point> currentList = null;
    private BufferedImage bufferedImage = new BufferedImage(PREF_W, PREF_H,
            BufferedImage.TYPE_INT_ARGB);
    private Random random = new Random();

This contructor is used to change the color of the curve as soon as mouse is released.

    public MouseTestHovercraft() {
        for (int redIndex = 0; redIndex < MAX_CLR; redIndex++) {
            int r = (redIndex * 256) / (MAX_CLR - 1);
            r = (r == 256) ? 255 : r;
            for (int greenIndex = 0; greenIndex < MAX_CLR; greenIndex++) {
                int g = (greenIndex * 256) / (MAX_CLR - 1);
                g = (g == 256) ? 255 : g;
                for (int blueIndex = 0; blueIndex < MAX_CLR; blueIndex++) {
                    int b = (blueIndex * 256) / (MAX_CLR - 1);
                    b = (b == 256) ? 255 : b;
                    Color c = new Color(r, g, b);
                    colors.add(c);
                }
            }
        }

        add(new JToggleButton(new AbstractAction("TrackMouse") {
            public void actionPerformed(ActionEvent ae) {
                trackMouse(ae);
            }
        }));
        add(new JButton(new AbstractAction("Clear Image") {
            public void actionPerformed(ActionEvent e) {
                bufferedImage = new BufferedImage(getWidth(), getHeight(),
                        BufferedImage.TYPE_INT_ARGB);
                repaint();
            }
        }));

        MyMouseAdapter myMA = new MyMouseAdapter();
        addMouseListener(myMA);
        addMouseMotionListener(myMA);
    }

This is used to listen the event of tracking button.

    private void trackMouse(ActionEvent ae) {
        JToggleButton toggleBtn = (JToggleButton) ae.getSource();
        tracking = toggleBtn.isSelected();
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(PREF_W, PREF_H);
    }

    public void msg(String message) {
        System.out.println(message);
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawImage(bufferedImage, 0, 0, null);
        if (currentList != null) {
            drawList(g, currentList, CURRENT_LIST_COLOR, 1f);
        }
    }

This will draw a curve on jpanel using quadcurve

    private void drawList(Graphics g, List<Point> ptList, Color color,
            float strokeWidth) {
        if (ptList.size() > 1) {
            Graphics2D g2 = (Graphics2D) g.create();
            //        Graphics2D g2 = (Graphics2D) g;
            g2.setColor(color);
            //         g2.setStroke(new BasicStroke(strokeWidth));
            QuadCurve2D selectedCurve = null;
            for (int j = 0; j < ptList.size() - 1; j++) {
                int x1 = ptList.get(j).x;
                int y1 = ptList.get(j).y;
                int x2 = ptList.get(j + 1).x;
                int y2 = ptList.get(j + 1).y;

                selectedCurve =new QuadCurve2D.Float(x1, y1, 5, 5, x2,y2);

                g2.draw(selectedCurve);

            }
            g2.dispose();
        }
    }

On mouse press this code will first add the starting point into currentlist list and after that till mouse released all the points are added into currentlist.

    private class MyMouseAdapter extends MouseAdapter {



        @Override
        public void mousePressed(MouseEvent e) {
            if (tracking && e.getButton() == MouseEvent.BUTTON1) {
                currentList = new ArrayList<Point>();


                currentList.add(e.getPoint());
            }
        }

        @Override
        public void mouseReleased(MouseEvent e) {
            if (tracking && e.getButton() == MouseEvent.BUTTON1) {
                currentList.add(e.getPoint());
                Graphics2D g2 = bufferedImage.createGraphics();
                Color color = colors.get(random.nextInt(colors.size()));
                drawList(g2, currentList, color, 3f);
                currentList = null;
                repaint();
            }
        }

        @Override
        public void mouseDragged(MouseEvent e) {
            if (tracking && currentList != null) {
                currentList.add(e.getPoint());
                repaint();
            }
        }
    }

    private static void createAndShowGui() {
        MouseTestHovercraft mainPanel = new MouseTestHovercraft();

        JFrame frame = new JFrame("MouseMotion Eg");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(mainPanel);
        frame.setResizable(false);
        frame.pack();
        frame.setLocationByPlatform(true);
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGui();
            }
        });
    }
}
4

0 回答 0