1


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);
}
3
  • seems like you're not repaint()ing after display? Commented Oct 20, 2012 at 8:05
  • I am, I forgot to add that in. Commented Oct 20, 2012 at 8:09
  • For better help sooner, post an SSCCE. Commented Oct 20, 2012 at 8:10

1 Answer 1

3

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.