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!