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

Recommended Answers

All 6 Replies

Your paint method needs to be in the GUI class MyMouse, not in the listener class MyMouseAdapter. It's the GUI that gets painted, not the listener.

Please include all import statements with the posted code so we don't have to add them ourselves. I see no call to this function:

public void actionButtons() {
            for (int i = 0; i < buttonsName.length; ++i) {
                paintButtons[i].addActionListener(this);
            }

No Action Listeners are ever added to the buttons. I put a breakpoint at the top of this function:

public void actionPerformed(ActionEvent e) {
            Graphics g = null;
            if (e.getSource() == buttonsName[0]) {
                drawLine(g);
            }
        }

and it looks to me like this function is never executed.

hi again,
Sorry for lating.

I try to adhust the problem but i can't . This is the finel code i did
but not working also
please :tell me the correct

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

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);
        mouse.actionButtons();
    }

    @Override
    public void paint(Graphics g) {
        mouse.drawLine(g);
    }

    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 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

Its no good just saying "it doesn't work". What exactly goes wrong?

The line not appear when i draw in the panel with mouse me
why ???

please help

So put System.out.println(...) statements in your code (eg in the listener methods, in drawLine etc) so you can see what is being executed and what is not.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.