0

I want to draw a line on a JFrame by clicking on pushButton.

I wrote this code bellow, but it doesn't work. I would appreciate very much if anyone could help me solve this problem.

Rubin

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class testDrawing {

      int x = 0;
      int y = 0;
      int x1 = 0;
      int y1 = 0;

      JFrame frame=new JFrame();
      DrawPanel draw=new DrawPanel();

      public  testDrawing() {

          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.setSize(800,600);
          frame.setVisible(true);

          JButton btntest = new JButton("Draw a line");
          btntest.setBounds(380, 100, 100, 20);
      frame.add(btntest);

      btntest.addActionListener(new ActionListener() {
             public void actionPerformed(ActionEvent e) {

                 x =  100;
                 y =  100; 
                 x1 = 150;
                 y1 = 130;

                 executeAction(); 

             }  
        });

       }  

      public void executeAction(){

          frame.getContentPane().add(draw);

          x =  100;
                  y =  100; 
                  x1 = 150;
                  y1 = 170;

          draw.repaint();  

                  try{
                  Thread.sleep(30);
                  }catch(Exception e)
                  {}
      }

    class DrawPanel extends JPanel{

        public void paintComponent(Graphics g)
           { 

            g.drawLine(x, y, x1, y1);

           }
    }


     public static void main(String[] args) {

             SwingUtilities.invokeLater(new Runnable() {
             @Override
             public void run() {

                 testDrawing test = new testDrawing();

            //   test.executeAction(); with this line uncommented the drawing is performed,
            //                         but the pushBotton event doesn't work

             }
          });

     }
}
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720

3 Answers3

0

Your button is covering the whole JFrame. If you want to draw lines on the button, you would have to create an implementation of javax.swing.Icon, use an instance of that icon as the buttons icon, and then edit the icon as you paint the lines.

However, I see no reason to use button here. Just paint on the JPanel. See this thread

Community
  • 1
  • 1
ArneHugo
  • 6,051
  • 1
  • 26
  • 47
0

You should add an appropriate layout manager and arrange your items upfront (docs.oracle.com/javase/tutorial/uiswing/layout/box.html)

chris.tian
  • 770
  • 5
  • 13
0

Just add a flag boolean draw = false. Use it in the paintComponent method if(draw) and update it in the actionPerfomed

And Don't use Thread.sleep, you will block the EDT

Also not you need to be calling super.paintComponent from the paintComponent method.


Here's an example of what I'm talking about

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;


public class Example {

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JOptionPane.showMessageDialog(null, new DrawPanel());
            }
        });
    }

    private static class DrawPanel extends JPanel {
        private boolean draw = false;
        int x1, y1, x2, y2;

        public DrawPanel() {
            JButton button = new JButton("Draw");
            button.addActionListener(new ActionListener(){
                @Override
                public void actionPerformed(ActionEvent e) {
                    x1 = 50; y1 = 50; x1 = 200; y2 = 300;
                    draw = true;
                    repaint();
                }
            });
            add(button);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.fillRect(0, 0, getWidth(), getHeight());
            if (draw) {
                g.setColor(Color.GREEN);
                g.drawLine(x1, y1, x2, y2);
            }  
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(300, 300);
        }
    }
}

Side Note

  • Use Java naming convention. Class named begin with capital letters.
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720