0

Here i try to draw line by mouse pn panel
but it is not appear

here is my code:

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

public class MainPainter {

    private static final Dimension App_Dimension = new Dimension(600, 300);
    private JPanel mainPanel, mainComponentPanel, componentPanel, drawPanel;
    private String[] buttonNames = {"line", "rect", "oval", "polygon", "eraser"};
    private JButton[] buttons;
 //   private String[] imageNames = {"line.png", "rect.png", "oval.png", "connected.png", "erase.png"};
    private ImageIcon[] imageIcons;

    public MainPainter() {
        intializePanels();
        intializeButtons();
        //intializationIcons();
        ShapetTest shapetTest = new ShapetTest();
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        frame.getContentPane().add(new MainPainter().getComponent());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(App_Dimension);
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    }

    private void intializePanels() {
        mainPanel = new JPanel(new BorderLayout());
        mainComponentPanel = new JPanel();
        mainComponentPanel.setBackground(Color.BLUE);
        mainComponentPanel.setPreferredSize(new Dimension(200, 100));
        componentPanel = new JPanel(new GridLayout(3, 2, 5, 5));
        componentPanel.setBackground(Color.GREEN);
        //drawPanel = new JPanel() ;

    }

//    private void intializationIcons() {
//        imageIcons = new ImageIcon[imageNames.length];
//        for (int i = 0; i < imageNames.length; ++i) {
//            imageIcons[i] = new ImageIcon(getClass().getResource(imageNames[i]));
//        }
//    }

    private void intializeButtons() {
       // intializationIcons();
        MyActionListener myActionListener = new MyActionListener();
        buttons = new JButton[buttonNames.length];
        for (int i = 0; i < buttonNames.length; ++i) {
            buttons[i] = new JButton(buttonNames[i]);
            buttons[i].setToolTipText(buttonNames[i]);
            buttons[i].addActionListener(myActionListener);
            componentPanel.add(buttons[i]);
            mainComponentPanel.add(componentPanel);
        }
        mainPanel.add(mainComponentPanel, BorderLayout.WEST);
    }

    public JComponent getComponent() {
        return mainPanel;
    }

    class ShapetTest {

        private int xStart, yStart, xEnd, yEnd;

        public ShapetTest() {
            MyMouse mouse = new MyMouse();
            drawLine();
            drawPanel.addMouseListener(mouse);
            drawPanel.addMouseMotionListener(mouse);
        }

        public void drawLine() {
          drawPanel = new JPanel() {

                @Override
                public void paintComponent(Graphics g) {
                    super.paintComponent(g);
                    g.setColor(Color.BLACK);
                    g.drawLine(xStart, yStart, xEnd, yEnd);
                }
            };
            drawPanel.setBackground(Color.WHITE);
            mainPanel.add(drawPanel, BorderLayout.CENTER);

        }

        class MyMouse extends MouseAdapter {

            @Override
            public void mousePressed(MouseEvent e) {
                xStart = e.getX();
                yStart = e.getY();
            }

            @Override
            public void mouseReleased(MouseEvent e) {
                xEnd = e.getX();
                yEnd = e.getY();
                new ShapetTest().drawLine();
            }
        }
    }

    class MyActionListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            if (e.getSource() == buttons[0]) {
                new ShapetTest().drawLine();
            }
        }
    }
}

please say to me the correct way
thanks

Beshoy Atef

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by JamesCherrill
0

Use your IDE, or put print statements into your code so you can find out which methods are / are not being called when you expect.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.