I drew some shapes using OpenGL, here is the code:
The drawing methods:
private void drawRectangle(GL2 gl, double x1, double y1, double x2, double y2, double x3, double y3, double x4, double y4) {
gl.glBegin(GL2.GL_LINE_LOOP);
gl.glVertex2d(x1, y1);
gl.glVertex2d(x2, y2);
gl.glVertex2d(x3, y3);
gl.glVertex2d(x4, y4);
gl.glEnd();
}
private void drawCircle(GL2 gl, double centerX, double centerY, double radius) {
gl.glBegin(GL2.GL_LINE_STRIP);
for (double i = 0; i < 360; i++) {
double radian = i * Math.PI / 180;
double x = radius * Math.cos(radian) + centerX;
double y = radius * Math.sin(radian) + centerY;
gl.glVertex2d(x, y);
}
gl.glEnd();
}
I drew some circles and rectangles, here is the display method:
public void display(GLAutoDrawable drawable) {
final GL2 gl = drawable.getGL().getGL2();
gl.glColor3f(1f, 1f, 1f);
drawCircle(gl, 0.7, 0.2, 0.1);
drawCircle(gl, 0.3, 0.1, 0.3);
drawRectangle(gl, -0.5, -0.5, 0, -0.5, 0, 0, -0.5, 0);
}
I want to fill the shape with a color when I click on it. (static color for all clicks, red for example)
class Color {
public double c1, c2, c3;
public Color(double c1, double c2, double c3) {
super();
this.c1 = c1;
this.c2 = c2;
this.c3 = c3;
}
}
So I want to know 2 things:
1- how can I fill the shape with a color?
2- how to fill the shape when I click on it?
I'm using javax.swing.JFrame to show the canvas, but there is no problem if there is a way using anything else such as JavaFX or AWT or something else.
The main method:
public static void main(String[] args) {
final GLProfile profile = GLProfile.get(GLProfile.GL2);
GLCapabilities capabilities = new GLCapabilities(profile);
final GLCanvas glcanvas = new GLCanvas(capabilities);
Driver driver = new Driver();
glcanvas.addGLEventListener(driver);
glcanvas.setSize(400, 400);
final JFrame frame = new JFrame();
frame.getContentPane().add(glcanvas);
frame.setSize(frame.getContentPane().getPreferredSize());
frame.setVisible(true);
}

If you want to fill the shape with a color, you must use one of the Triangle primitives instead of
GL2.GL_LINE_LOOP. e.g.:GL2.GL_TRIANGLE_FAN: