0

Hy!

I want to draw free lines in a JFrame. I implemented it in a class which extends JPanel. Its definition looks like this:

public class Draw extends JPanel implements MouseMotionListener, MouseListener

I added to the class Draw a void method where I also generated the JFrame and added than the Draw object. My problem now is: I want to be able to select the color of the line from a menu:

JMenu colorMenu = new JMenu();
JMenuItem greenChoice = new JMenuItem("GREEN");
greenChoice.addActionListener(this);
colorMenu.add(greenChoice);
JMenuItem redChoice = new JMenuItem("RED");
colorMenu.add(redChoice);
JMenuBar bar = new JMenuBar();
bar.add(colorMenu);

I don't know where to implement this code! Should I make 2 classes, one for the drawing and one for the JFrame together with its Menu? How should I tell the Drawing class to interact with my JMenu, e.g. with the color I choose from the Menu? It is here where i generate my line in the class Drwa, with the default color BLUE :(

public void paintComponent(Graphics g){
Graphics2D g2d = (Graphics2D) g;
if(point1!=null && point2!=null){
g2d.setPaint(Color.BLUE);
g2d.setStroke(new BasicStroke(1.5f));
g2d.draw(line2d);
  }
  }

thank you!

1 Answers1

0

Here's a free hand drawing class that I did to illustrate Swing graphics. Modify it as you wish.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.event.MouseInputAdapter;

public class DrawingTest implements Runnable {

    private JFrame frame;

    private MyDrawPanel drawPanel;

    private List<Point> points;

    public DrawingTest() {
        points = new ArrayList<Point>();
    }

    @Override
    public void run() {
        frame = new JFrame("Bouncing Vertices");

        drawPanel = new MyDrawPanel(this);
        MyListener alpha = new MyListener(this);
        drawPanel.addMouseMotionListener(alpha);
        drawPanel.addMouseListener(alpha);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().add(drawPanel);
        frame.setSize(800, 600);
        frame.setVisible(true);
    }

    public JPanel getDrawingPanel() {
        return drawPanel;
    }

    public List<Point> getPoints() {
        return points;
    }

    public void setPoint(int x, int y) {
        points.add(new Point(x, y));
    }

    public void resetPoints() {
        points.clear();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new DrawingTest());
    }

    private class MyListener extends MouseInputAdapter {

        private DrawingTest drawingTest;

        public MyListener(DrawingTest drawingTest) {
            this.drawingTest = drawingTest;
        }

        @Override
        public void mouseDragged(MouseEvent event) {
            drawingTest.setPoint(event.getX(), event.getY());
            drawingTest.getDrawingPanel().repaint();
        }

        @Override
        public void mouseReleased(MouseEvent event) {
            drawingTest.resetPoints();
        }

    }

    private class MyDrawPanel extends JPanel {

        private static final long serialVersionUID = 1L;
        private DrawingTest drawingTest;

        public MyDrawPanel(DrawingTest drawingTest) {
            this.drawingTest = drawingTest;
        }

        @Override
        public void paintComponent(Graphics g) {
            g.setColor(Color.WHITE);
            g.fillRect(0, 0, this.getWidth(), this.getHeight());
            g.setColor(Color.BLUE);
            for (int i = 1; i < drawingTest.getPoints().size(); i++) {
                Point p1 = drawingTest.getPoints().get(i - 1);
                Point p2 = drawingTest.getPoints().get(i);
                g.drawLine(p1.x, p1.y, p2.x, p2.y);
            }
        }
    }

}
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
  • this looks and works indeed very nice, but the code is a bit complicated for me. I tried to work on my version, but I didn't manage to pimp it up to draw at least normal lines. Can you have a look on it, please? – madalina c. Nov 02 '12 at 18:23