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

  • Home
  • SEARCH
  • 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 9318407
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 19, 20262026-06-19T03:07:15+00:00 2026-06-19T03:07:15+00:00

I am trying to display an array graphically but I am having problems. It

  • 0

I am trying to display an array graphically but I am having problems.

It shoes “null” before I fill up the array which is fine, but after I fill up the array it draws over the “null” which makes it hard to read.

How can I make it so the canvas clears up and redraws after I fill up the array.

Here is my code so far:

public class wordManager extends JFrame
{
String[] array = new String[15];

private BufferedImage buffered;
public wordManager()
{
    super("Word Managery");


    setSize(300,600);
    setVisible(true);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

 public void paint(Graphics window)
 {
     if(buffered==null)
            buffered = (BufferedImage)(createImage(getWidth(),getHeight()));

     Graphics windowTemp = buffered.createGraphics();

     int y = 50;
     for(int i = 0; i<array.length; i++)
     {
        windowTemp.drawString(array[i] + "", 10,y);
        y+=10;

     }

     window.drawImage(buffered, 0, 0, null);
 }


public void read(String filename) throws IOException
{

    String word;
    int i = 0;
    Scanner file = new Scanner(new File(filename+".txt"));
    while(file.hasNext())
    {
        word = file.next();
        array[i] = word;

        i++;
    }
    repaint();
}


public void scramble()
{
    for(int i=0;i<array.length;i++)
    {
        int a = (int) (Math.random()*array.length);
        String b = array[i];
        array[i] = array[a];
        array[a] = b;
    }
    repaint();
}

public void sort() 
{
    for (int i = 1; i < array.length; i++)
    {
        int s = i-1;
        for (int j = i; j < array.length; j++)
        {
            if (array[j].compareTo(array[s]) < 0)
            {
                 s = j;
            }
        }
         String temp = array[i-1];
         array[i-1] = array[s];
         array[s] = temp;
    }
    repaint();
}


public void write() throws IOException
{
    PrintWriter fileOut = new PrintWriter(new FileWriter("out.txt"));

    for(int i = 0; i<array.length; i++)
    {
        fileOut.println(array[i]);  
    }

    fileOut.close();

}

public void printArray()
{
    for(String term : array)
    {
        System.out.println(term);
    }
}

}

public class runner
{
public static void main(String args[]) throws IOException 
{
    wordManager run = new wordManager();

    Scanner keyboard = new Scanner(System.in);
    System.out.println("In put file name");
    String filename = keyboard.next();

    run.read(filename);

    System.out.println("");
    run.printArray();
    System.out.println("");

    System.out.println("Enter 1 if you want to sort\n");
    System.out.println("Enter 2 if you want to scramble");


    int selection = keyboard.nextInt();


        if(selection == 1)
        {
            run.sort();
        }

        if(selection == 2)
        {
            run.scramble();
        }

    run.printArray();
    System.out.println("");

    run.write();
}
}
  • 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-19T03:07:17+00:00Added an answer on June 19, 2026 at 3:07 am

    You’ve failed to honor the paint chain.

    Paint does a lot of important work, most notably for you, it prepares the graphics for rendering.

    Graphics is a shared resource, it can get past to all the components that the repaint manager needs to repaint. Part of the paint process is to clear the graphics, typically by calling super.paint.

    Now, having said that. You should, very rarely, need to override the paint method of top level container, if for nothing else, top level containers aren’t double buffered.

    Instead, you should be creating a custom component, from something like JPanel, and overriding it’s paintComponent method.

    Text in a graphic environment have metrics, based on the current font. Simply using a magic number to calculate the next line that the text should appear on is going to produce lots of nasty results when the font changes (and it will).

    enter image description here

    Instead, you should be do something like…

    FontMetrics fm = windowTemp.getFontMetrics();
    int y = 50;
    for(int i = 0; i<array.length; i++)
    {
        windowTemp.drawString(array[i] + "", 10,y + fm.getAscent());
        y+=fm.getHeight();
    }
    

    Take a look at Working with Text APIs

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

Sidebar

Related Questions

I am trying to display the list of songs using array adapters. But the
Hello everyone I'm trying to display my array's values in my View but there
just created an UITableView, trying to display some custom data from an array, but
I am trying to display a large array into the command window but it
I am trying to display an array which by itself is no issue. However,
I'm trying to display only the duplicate value in an array which just holds
I have an array of strings which I'm trying to display (one by one)
I was trying to display an array in php to an HTML table but
I am just trying to display a list from an array that I have
I'm trying to create an array to display the last 5 products a customer

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.