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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:00:17+00:00 2026-05-31T19:00:17+00:00

I have to sort an array of 10 numbers using insertion sort and display

  • 0

I have to sort an array of 10 numbers using insertion sort and display them using rectangles (i.e. a bar graph). Each time the user hits “next” it will sort the next position in the array. The console confirms that my algorithm is working, and the rectangles will become bigger/smaller but will only overwrite themselves on top of another rectangle; the panel will not erase before drawing new rectangles. How can I fix this? Here are the important parts of my code:

 public class graphTest extends JFrame {

    int[] numbers = {31, 19, 76, 24, 94, 99, 21, 74, 40, 73};
    private JButton action = new JButton("Next");

    public graphTest(){
        final ImagePanel p1 = new ImagePanel();
        add(action, BorderLayout.SOUTH);
        p1.add(new ImagePanel(), BorderLayout.CENTER);
        add(p1,BorderLayout.CENTER);
        action.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //Code for insertion sort
                p1.repaint();
                //System.out.println testing array
            }
        });
    }

    class ImagePanel extends JPanel{
        public void paintComponent (Graphics g){
            super.paintComponents(g);
            g.setColor(Color.black);
            for(int i = 0; i < 10; i++)
                g.drawRect(10*i+10,200-numbers[i],7,numbers[i]);
        }
    }

    public static void main(String[] args) {
        JFrame frame = new graphTest();
        frame.setTitle("Hi");
        frame.setSize(300, 300);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
        frame.setVisible(true);
        }

}

I’ve made a quick workaround by just drawing a large fillRect before drawing the regular rectangles, but I would still like to learn how to properly redraw a JPanel.

  • 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-31T19:00:18+00:00Added an answer on May 31, 2026 at 7:00 pm

    In ImagePanel.paintComponent( Graphics g ):

    you call super.paintComponents(g) when what you really want is super.paintComponent(g)

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

Sidebar

Related Questions

I have an array that I would like to sort using a date field
I want to sort array of random numbers using MPI library. The idea is
I'm using ruby to sort an array where each element in the array is
I have an array of structures and I want to sort them. I have
I have a number of very long arrays. No run-time sort is possible. It
I have an array of filenames and need to sort that array by the
I have an array of pointers to Objective-C objects. These objects have a sort
I'm struggling with trying to sort an array of dictionaries. My dictionaries have a
I have an array which i need to sort its elements by occurrence then
I have an array that I want to sort based on the value of

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.