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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:47:00+00:00 2026-05-14T15:47:00+00:00

I’m making a visualization for a BST implementation (I posted another question about it

  • 0

I’m making a visualization for a BST implementation (I posted another question about it the other day). I’ve created a GUI which displays the viewing area and buttons. I’ve added code to the BST implementation to recursively traverse the tree, the function takes in coordinates along with the Graphics object which are initially passed in by the main GUI class. My idea was that I’d just have this function re-draw the tree after every update (add, delete, etc…), drawing a rectangle over everything first to “refresh” the viewing area. This also means I could alter the BST implementation (i.e by adding a balance operation) and it wouldn’t affect the visualization.

The issue I’m having is that the draw function only works the first time it is called, after that it doesn’t display anything. I guess I don’t fully understand how the Graphics object works since it doesn’t behave the way I’d expect it to when getting passed/called from different functions. I know the getGraphics function has something to do with it.

Relevant code:

    private void draw(){
    Graphics g = vPanel.getGraphics();      
    tree.drawTree(g,ORIGIN,ORIGIN);
}

vPanel is what I’m drawing on

private void drawTree(Graphics g, BinaryNode<AnyType> n, int x, int y){
    if( n != null ){
        drawTree(g, n.left, x-10,y+10 );
            if(n.selected){
                g.setColor(Color.blue);
            }
            else{
                g.setColor(Color.gray);
            }        
            g.fillOval(x,y,20,20);
            g.setColor(Color.black);
            g.drawString(n.element.toString(),x,y);
        drawTree(g,n.right, x+10,y+10);
    }
}

It is passed the root node when it is called by the public function. Do I have to have:

Graphics g = vPanel.getGraphics();

…within the drawTree function? This doesn’t make sense!!

Thanks for your help.

  • 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-05-14T15:47:00+00:00Added an answer on May 14, 2026 at 3:47 pm

    This is not the right way of doing it. If you want a component that displays the tree, you should make your own JComponent and override the paintComponent-method.

    Whenever the model (the tree / current node etc) changes, you invoke redraw() which will trigger paintComponent.

    I actually don’t think you are allowed to fetch the Graphics object from anywhere else than the argument of the paintComponent method.

    Try out the following program

    import java.awt.Graphics;
    
    public class FrameTest {
    
        public static void main(String[] args) {
            final JFrame f = new JFrame("Frame Test");
            f.setContentPane(new MyTreeComponent());
            f.setSize(400, 400);
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.setVisible(true);
    
            new Thread() {
                public void run() {
                    for (int i = 0; i < 10; i++) {
                        try {
                            sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        f.repaint();
                    }
                }
            }.start();
        }
    }
    
    class MyTreeComponent extends JComponent {
        public void paintComponent(Graphics g) {
            // Draw your tree. (Using random here to visualize the updates.)
            g.drawLine(30, 30, 50, 30 + new Random().nextInt(20));
            g.drawLine(30, 30, 50, 30 - new Random().nextInt(20));
        }
    }
    

    The best starting point is probably http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html

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

Sidebar

Ask A Question

Stats

  • Questions 374k
  • Answers 374k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It doesn't work quite like Javascript. You'll have to do… May 14, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer (Full disclosure: I work for Security Innovation and worked for… May 14, 2026 at 7:58 pm
  • Editorial Team
    Editorial Team added an answer barbuttonitems aren't supposed to resize... That's something you will have… May 14, 2026 at 7:58 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.