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);
}
It’s difficult to know with the example code you’ve given us.
Scenario #1, overriding
JComponent#paintComponentIf you are doing this inside your
paintComponentmethodThen DON`T.
Calling any method that updates the UI in any way from within a
paintmethod will only result in disaster. This is simply triggering another repaint request to be added to the Event Dispatching Thread, which will call youpaintComponentmethod and you can say good by to your CPU and program responsiveness.Also, make sure, when updating the UI from a different
Threadother then the EDT, make sure you sync the request back to the EDT usingSwingUtilities#invokeLaterorSwingUtilities.invokeAndWaitAlso, make sure you are calling
super.paintComponentScenario #2, using
JComponent#getGraphicsThe question that comes to mind is, where does
gcome 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
JPaneland override it’spaintComponentmethod and update the component with yourRenderImageloop (just leave out the code that changes the UI)Also, make sure that all repaint requests are made from within the context of EDT