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 7780575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:56:33+00:00 2026-06-01T18:56:33+00:00

I have the following piece of code that I would like help with… public

  • 0

I have the following piece of code that I would like help with…

public class Nodes extends JPanel implements ActionListener, MouseListener, MouseMotionListener {

    private static JPanel p1;
    private static JPanel p2;
    private JButton linkButton, nodeButton;
    private Vector nodeVector, linkVector;
    private Vector<Integer> data;
    private String s1,s2;
    public int x;
    int pt1;
    public int pt2;
    private Rectangle Node;
    private boolean f1, f2 = false;

    Nodes () {
        JFrame F = new JFrame ("Add Node / Add Link");
        nodeVector = new Vector ();
        linkVector = new Vector ();
        data = new Vector <Integer> ();
        F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        F.setVisible (true);
        F.setSize (500, 500);
        p1 = new JPanel ();
        p2 = new JPanel ();
        linkButton = new JButton ("Add Link");
        nodeButton = new JButton ("Add Node");
        linkButton.addActionListener (this);
        nodeButton.addActionListener (this);
        p2.addMouseListener (this);
        p2.addMouseMotionListener (this);
        p1.add (linkButton);
        p1.add (nodeButton);
        F.add (p1,BorderLayout.SOUTH);
        F.add (p2,BorderLayout.CENTER);
        p2.setBackground (Color.WHITE);
    }

    public void paintComponent (Graphics g) {
        super.paintComponent (g);
        Graphics2D g2d = (Graphics2D)(g);
        BufferedImage buffImage = new BufferedImage (25,25,BufferedImage.TYPE_INT_RGB);
        Graphics2D gg = buffImage.createGraphics ();
        gg.setColor (Color.GRAY);
        gg.fillRect (0, 0, 25, 25);
        gg.setStroke (new BasicStroke (12.0f));
        gg.setColor (Color.BLUE);
        gg.drawString ("" + data, 10, 10);
        g2d.setPaint (new TexturePaint (buffImage, new Rectangle (25,25)));
        g.drawRect (pt1, pt2, 10, 10);
        repaint ();
    }

    public void actionPerformed(ActionEvent e){                   
        if(e.getSource()==linkButton){
        s1 = JOptionPane.showInputDialog (null, "Insert the first Node Number");
            s2 = JOptionPane.showInputDialog (null, "Insert the second Node Number");
        }
        if (e.getSource () == nodeButton){
            x = Integer.parseInt ((JOptionPane.showInputDialog (null, "Insert the Node Number")));
            for (int i = 0 ; i < data.size (); i++){ 
                if (x == (int) data.get (i))
                    x = Integer.parseInt (JOptionPane.showInputDialog (null, "Used before insert another number"));
            }
            data.add (x);
            f1 = true; 
        }
    }

    public void mouseClicked (MouseEvent e){
           f2 = true;
           if (f1){ 
               JOptionPane.showMessageDialog (null, "you clicked at" + e.getPoint ());
               pt1 = e.getX ();
               pt2 = e.getY ();
               f1 = !f1;
               repaint ();
           }
    }

    public static void main (String[] args) {
        new Nodes();
    }   
}

I am trying to update the display by calling repaint(), however the paintComponent() method doesn’t work, and nothing actually changes. In the correct behaviour, when I press the “Add Node” button then click anywhere on the panel, a rectangle with the number the user entered should be drawn.

Could anyone please help explain what is wrong with my code.

  • 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-01T18:56:34+00:00Added an answer on June 1, 2026 at 6:56 pm

    Your main class is a JPanel, but it is not added to the JFrame, and its Panels/Buttons/whatnot.

    Try it this way:

    private Vector<Integer> data;
    public int x;
    int pt1;
    public int pt2;
    private Rectangle Node;
    private boolean f1, f2 = false;
    private JButton linkButton, nodeButton;
    
    Nodes ()
    {
        JFrame F = new JFrame ("Add Node / Add Link");
        Vector nodeVector, linkVector;
        nodeVector = new Vector ();
        linkVector = new Vector ();
        data = new Vector <Integer> ();
        F.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        F.setVisible (true);
        F.setSize (500, 500);
    
        JPanel p1 = new JPanel ();
        linkButton = new JButton ("Add Link");
        nodeButton = new JButton ("Add Node");
        linkButton.addActionListener (this);
        nodeButton.addActionListener (this);
        addMouseListener (this);
        addMouseMotionListener (this);
        p1.add (linkButton);
        p1.add (nodeButton);
        F.add (p1, BorderLayout.SOUTH);
        F.add (this, BorderLayout.CENTER);
        setBackground (Color.WHITE);
    }
    

    Many attributes can be local variables. JPanel p2 is removed and replaced by the JPanel which is this, which allows to use the paintComponent method to be used.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following piece of Java code that I would like to convert
I have the following code piece for a console application that I would like
I have the following piece of code: NameX.functionA(functionB(Dictionary[___])) Instead of _ I would like
I have the following piece of code for a graph: barplot(as.vector(t(mat1[1,3:ncol(mat1)])),las=2) that I would
I have the following piece of code that takes in some words, stores them
I have the following piece of code that generate a jquerymobile-style button <a href=#
I have the following piece of code that finds all elements in the document
I have the following piece of Java code that reads strings from CSV file.
I have the following piece of code from a function that takes the host
I have the following piece of code in my MainViewController. Is that the best

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.