I try to draw line with this code
but it never appear say to me the correct way for doing this .
This is my code:
public class MyMouse extends JFrame {
private JPanel mainPenl;
private JPanel paintPanel, componentsPaintPanel, areaPanel, areaPaintPanel;
String[] buttonsName = {"Line", "Oval", "Rectangle"};
JButton[] paintButtons;
MyMouseAdapter mouse = new MyMouseAdapter();
public MyMouse() {
intializePanel();
intializeButtons();
areaPaintPanel.setBackground(Color.WHITE);
areaPaintPanel.setPreferredSize(new Dimension(1500, 1000));
areaPanel.add(areaPaintPanel);
paintPanel.setLayout(new GridLayout(3, 1));
componentsPaintPanel.setPreferredSize(new Dimension(100, 50));
componentsPaintPanel.setBackground(Color.BLUE);
componentsPaintPanel.add(paintPanel);
mainPenl.add(componentsPaintPanel, BorderLayout.WEST);
mainPenl.add(areaPanel, BorderLayout.CENTER);
getContentPane().add(mainPenl);
addMouseListener(mouse);
addMouseMotionListener(mouse);
}
public static void main(String[] args) {
MyMouse mouse = new MyMouse();
mouse.setDefaultCloseOperation(MyMouse.EXIT_ON_CLOSE);
mouse.setSize(new Dimension(600, 300));
mouse.setVisible(true);
mouse.setLocationRelativeTo(null);
}
private void intializePanel() {
mainPenl = new JPanel();
mainPenl.setLayout(new BorderLayout());
paintPanel = new JPanel();
componentsPaintPanel = new JPanel();
areaPanel = new JPanel();
areaPaintPanel = new JPanel();
}
private void intializeButtons() {
paintButtons = new JButton[buttonsName.length];
for (int i = 0; i < buttonsName.length; ++i) {
paintButtons[i] = new JButton(buttonsName[i]);
paintPanel.add(paintButtons[i]);
}
}
class MyMouseAdapter extends MouseAdapter implements ActionListener {
Point p1, p2;
public void actionButtons() {
for (int i = 0; i < buttonsName.length; ++i) {
paintButtons[i].addActionListener(this);
}
}
@Override
public void mousePressed(MouseEvent e) {
p1 = e.getPoint();
}
@Override
public void mouseReleased(MouseEvent e) {
p2 = e.getPoint();
}
public void paint(Graphics g) {
drawLine(g);
}
public void actionPerformed(ActionEvent e) {
Graphics g = null;
if (e.getSource() == buttonsName[0]) {
drawLine(g);
}
}
private void drawLine(Graphics g) {
g.setColor(Color.BLACK);
g.drawLine(p1.x, p1.y, p2.x, p2.y);
areaPaintPanel.print(g);
}
}
}
thanks in advance
beshoy