My problem is the following. I am using the standard SWING event dispatch thread to draw an oval with a low alpha value and another smaller oval in the centre to represent its centre. And when I run the simulation, I use a separate thread and also allow the user to add these obstacles while the simulation is running. Now this works fine when the separate thread isn’t running but when the separate simulation thread is running, the bigger oval (Which is supposed to be drawn with a low alpha) isn’t drawn with a low alpha value and therefore creates a solid big red oval. Why is this happening and how can I resolve this?
If i wasn’t too clear in my explanation please let me know.
private void drawObstacleCircle(Obstacle o, Graphics2D g)
{
final double OBSTACLE_CENTER_RADIUS = 2.0;
final double OBSTACLE_RADIUS = o.getRadius(true);
float scaleToUse = Math.max(0.8f, scale);
Vector pos = o.getPosition();
int xPos = (int) (pos.getX() * getBufferedHeightMap().getWidth());
int yPos = (int) (pos.getY() * getBufferedHeightMap().getHeight());
Point positionPoint = convertToPanelPoint(new Point(xPos, yPos));
// Draw the circle where the Obstacle's center is
Graphics2D g1 = (Graphics2D) g.create();
g1.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g1.setColor(Color.RED);
g1.fillOval(positionPoint.x - (int) (OBSTACLE_CENTER_RADIUS * scaleToUse), positionPoint.y - (int) (OBSTACLE_CENTER_RADIUS * scaleToUse), (int) (OBSTACLE_CENTER_RADIUS * 2 * scaleToUse), (int) (OBSTACLE_CENTER_RADIUS * 2 * scaleToUse));
Color c = Color.RED;
g1.setColor(new Color(c.getRed(), c.getGreen(), c.getBlue(), 100));
g1.fillOval(positionPoint.x - (int) (OBSTACLE_RADIUS * scaleToUse), positionPoint.y - (int) (OBSTACLE_RADIUS * scaleToUse), (int) (OBSTACLE_RADIUS * 2 * scaleToUse), (int) (OBSTACLE_RADIUS * 2 * scaleToUse));
g1.dispose();
g2.dispose();
}
It’s hard to tell without the code around it, but it seems to me like you aren’t erasing your canvas before you start redrawing the scene. Could the addition of the new thread cause the
drawObstacleCirclemethod to be called multiple times with the same graphics context?That would mean that the method draw on top of the already drawn scene. So a slightly transparent oval is drawn again and again and again on place of the previously drawn ovals until the area looks solid red.