Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8828165
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:32:40+00:00 2026-06-14T07:32:40+00:00

I am trying to draw lines with the mouse in a JFrame window, and

  • 0

I am trying to draw lines with the mouse in a JFrame window, and when I try to do so, it doesn’t work! Please ignore the menu, I am trying to do things with that later! What did I miss! I would be grateful if you could give me some hints!

public class newGUI extends JFrame implements ActionListener, MouseMotionListener, MouseListener {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 300;
public static final int HEIGHT = 200;
Point point1;
Point point2;
Line2D line2d;

public static void main(String[] args){
    newGUI gui = new newGUI();
    gui.setVisible(true);
}

public newGUI()
{
    super("Menu Demonstration");
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JMenu colorMenu = new JMenu("Choose Colors");
    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);
    setJMenuBar(bar);
    addMouseListener(this);
    addMouseMotionListener(this);
}
@Override
public void actionPerformed(ActionEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseClicked(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseEntered(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseExited(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mousePressed(MouseEvent arg0) {
    // TODO Auto-generated method stub
    point1=arg0.getPoint();
}

@Override
public void mouseReleased(MouseEvent arg0) {
    // TODO Auto-generated method stub

}

@Override
public void mouseDragged(MouseEvent arg0) {
    // TODO Auto-generated method stub
    point2=arg0.getPoint();
    line2d=new Line2D.Double(point1, point2);
    repaint();

}

@Override
public void mouseMoved(MouseEvent arg0) {
    // TODO Auto-generated method stub

}
public void paintComponent(Graphics g){
    Graphics2D g2d = (Graphics2D) g;
    if(point1!=null && point2!=null){
        g2d.setPaint(Color.RED);
        g2d.setStroke(new BasicStroke(1.5f));
        g2d.draw(line2d);
        repaint();
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T07:32:41+00:00Added an answer on June 14, 2026 at 7:32 am
    • Always start class names with a capital letter i.e NewGui
    • Use Event Dispatch Thread for creating and changing of UI components
    • Do not extend JFrame class uncecessarily
    • Do not call setSize(..) rather call JFrame#pack() on JFrame instance
    • JFrame does not have a paintComponent(..), rather add custom JPanel and override its paintComponent(..) and getPreferredSize(..)
    • Dont forget to call super.paintComponent(..) in your custom JPanel
    • Do not call repaint() from within paintComponent(..) (this will cause an infinite loop of repainting more than needed)
    • Also where possible (if it is not used by other classes) use anonymous MouseListeners/MouseMotionListeners and rather use MouseAdapter instead of MouseListener/MouseMotionListeners etc.

    Here is an example I made (basically your code with fixes mentioned):

    enter image description here

    import java.awt.BasicStroke;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Line2D;
    import javax.swing.JFrame;
    import javax.swing.JMenu;
    import javax.swing.JMenuBar;
    import javax.swing.JMenuItem;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class NewGui implements ActionListener {
    
        private static final long serialVersionUID = 1L;
        public static final int WIDTH = 300;
        public static final int HEIGHT = 200;
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
                @Override
                public void run() {
                    NewGui gui = new NewGui();
                }
            });
        }
    
        public NewGui() {
            initComponents();
        }
    
        @Override
        public void actionPerformed(ActionEvent arg0) {
            // TODO Auto-generated method stub
        }
    
        private void initComponents() {
            JFrame frame = new JFrame("Menu Demonstration");
    
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            JMenu colorMenu = new JMenu("Choose Colors");
            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);
            frame.setJMenuBar(bar);
    
            frame.add(new MyPanel());
    
            frame.pack();
            frame.setVisible(true);
        }
    }
    
    class MyPanel extends JPanel {
    
        Point point1;
        Point point2;
        Line2D line2d;
    
        public MyPanel() {
            addMouseListener(new MouseAdapter() {
                @Override
                public void mousePressed(MouseEvent me) {
                    super.mousePressed(me);
                    point1 = me.getPoint();
                }
            });
            addMouseMotionListener(new MouseAdapter() {
                @Override
                public void mouseDragged(MouseEvent me) {
                    super.mouseDragged(me);
                    // TODO Auto-generated method stub
                    point2 = me.getPoint();
                    line2d = new Line2D.Double(point1, point2);
                    repaint();
                }
            });
        }
    
        //so our panel will be visible
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(400, 200);
        }
    
        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g;
    
            //Set  anti-alias!
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON); 
    
            if (point1 != null && point2 != null) {
                g2d.setPaint(Color.RED);
                g2d.setStroke(new BasicStroke(1.5f));
                g2d.draw(line2d);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to build a ScrolledWindow that you can draw on using the mouse,
I am trying to make a something that will draw lines on a pixel
I'm trying to make a program that will draw lines over a picturebox using
I'm trying to add a whiteboard, so that people can draw lines on it.
Hello I am trying to draw diagonal lines in Java and this won't work
so I am trying to draw some grid lines that in landscape go all
I am trying to draw a line along with mouse move on a paper.
I'm trying to draw some simples lines with the iPhone/Touch SDK. I'd like to
I'am trying to draw a line above mapkit. If I try to set the
Using core graphics , I am trying to draw a line, in that when

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.