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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:15:43+00:00 2026-06-13T08:15:43+00:00

I am trying to render to a JPanel from a list array. I’ve created

  • 0

I am trying to render to a JPanel from a list array. I’ve created my own 2D renderer, but when I try and add to the list using the RenderAdd function, it either doesn’t add, or the list array doesn’t allow the list to be read…
He’s the code which starts it.

JFrame frame = new JFrame();
    frame.setSize(900, 500);
    frame.setVisible(true);
    Render render = new Render(new RenderDimension(frame.getX(),frame.getY(),frame.getWidth(),frame.getHeight()), frame);
    BufferedImage zombie = new ImageLoader().readImage("zombie");
    BufferedImage player = new ImageLoader().readImage("player");
    render.RenderAdd(new RenderImage(new RenderDimension(100, 100, player.getWidth(), player.getHeight()), player));
    render.RenderAdd(new RenderImage(new RenderDimension(0, 0, zombie.getWidth(), zombie.getHeight()), zombie));
    render.start();
    render.RenderAdd(new RenderImage(new RenderDimension(200, 100, player.getWidth(), player.getHeight()), player));

‘render’ is the main Render class in the rendering part. Then, RenderAdd adds a RenderImage which has RenderDimension which is the x and y of the object, and the image width and height. Then also takes a BufferedImage as a parameter.

Although, every time I try running the program, it comes with a blank screen

Now, in the Render class, there is another class which extends a thread. This is the class which takes the frame as a parameter, deletes the contents and starts painting to the getContentPane(). This next code is inside the paintComponent() function in the renderthread class.
Unfortunately, nothing paints, but is does process because I’ve tried with System.out.print(“h”) which repeatedly prints itself.

for (RenderImage r : render.getList()){
    int x = r.getSize().getX();
    int y = r.getSize().getY();
    int wi = r.getSize().getWidth();
    int hi = r.getSize().getHeight();
    if (x + wi >= -1 && x + wi <= d.getWidth()){
        if (y + hi >= -1 && y + hi <= d.getHeight()){
            g.drawImage(r.getImage(), x, y, null);
        }
    }
}
frame.getContentPane().add(p);
frame.getContentPane().validate();
frame.getContentPane().repaint();

I think the problem is that the list won’t add, so here’s that part.

List<RenderImage> render = new ArrayList<RenderImage>();

public List<RenderImage> getList(){
    return render;
}

public void RenderAdd(RenderImage renders){
    render.add(renders);
}
  • 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-13T08:15:44+00:00Added an answer on June 13, 2026 at 8:15 am

    It’s difficult to know with the example code you’ve given us.

    Scenario #1, overriding JComponent#paintComponent

    If you are doing this inside your paintComponent method

    for (RenderImage r : render.getList()){
        int x = r.getSize().getX();
        int y = r.getSize().getY();
        int wi = r.getSize().getWidth();
        int hi = r.getSize().getHeight();
        if (x + wi >= -1 && x + wi <= d.getWidth()){
            if (y + hi >= -1 && y + hi <= d.getHeight()){
                g.drawImage(r.getImage(), x, y, null);
            }
        }
    }
    frame.getContentPane().add(p);
    frame.getContentPane().validate();
    frame.getContentPane().repaint();
    

    Then DON`T.

    Calling any method that updates the UI in any way from within a paint method will only result in disaster. This is simply triggering another repaint request to be added to the Event Dispatching Thread, which will call you paintComponent method and you can say good by to your CPU and program responsiveness.

    Also, make sure, when updating the UI from a different Thread other then the EDT, make sure you sync the request back to the EDT using SwingUtilities#invokeLater or SwingUtilities.invokeAndWait

    Also, make sure you are calling super.paintComponent

    Scenario #2, using JComponent#getGraphics

    The question that comes to mind is, where does g come from in your example.

    If you’re using JComponent#getGraphics, then don’t. This is simple a snapshot of the graphics between repaints, as soon as the next repaint occurs, it will be erased.

    Create a custom component from something like JPanel and override it’s paintComponent method and update the component with your RenderImage loop (just leave out the code that changes the UI)

    Also, make sure that all repaint requests are made from within the context of EDT

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

Sidebar

Related Questions

I'm trying to render an HTML file using render :file in my view, but
I'm trying to render a cube using an array of 8 vertices and an
I'm trying to render a form that I've just generated from an entity, but
I am trying to render a float value using NSNumberFormatter's SpellOutStyle, but the results
I'm trying to render a pretty simple data structure using RABL, but I can't
I'm trying to render the following HTML code using xslt: <foo:bar href=[MYURL]/> I created
I am trying to render a chart from the following query: Questionnaires created by
I'm trying to render some text using PIL, but the result that comes out
I'm trying render the results of surveys from Surveryor gem as a CSV file,
i´m trying to render PDF with renderpdf grails plugin, but their documentation is very

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.