Hm so I tried it, doesn’t seem to work but perhaps something is wrong with my code :
Main.java
public class Main {
public static void main(String[] args) {
@SuppressWarnings("unused")
Frame frame = new Frame(1280, 720);
}
}
Frame.java
import java.awt.IllegalComponentStateException;
import java.awt.MouseInfo;
import javax.swing.JFrame;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLCapabilities;
import com.jogamp.opengl.GLProfile;
import com.jogamp.opengl.awt.GLJPanel;
public class Frame extends JFrame{
public static int mouseX = 0;
public static int mouseY = 0;
public static GLProfile PROFILE;
public static GLCapabilities CAPABILITIES;
public static GLJPanel panel;
public static PanelListener listener;
public static boolean stop = false;
//SERIALIZATION ID
private static final long serialVersionUID = 1L;
private boolean glLoaded = false;
public Frame(int width, int height) {
super();
PROFILE = GLProfile.get(GLProfile.GL2);
CAPABILITIES = new GLCapabilities(PROFILE);
CAPABILITIES.setSampleBuffers(true);
CAPABILITIES.setNumSamples(4);
CAPABILITIES.setStencilBits(8);
panel = new GLJPanel(CAPABILITIES);
listener = new PanelListener(panel);
panel.addGLEventListener(listener);
this.setContentPane(panel);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(width, height);
run();
}
private void updateGraphics() {
if(panel.getGL() != null) {
if(panel.getGL().getGL2() != null) {
GL2 gl = panel.getGL().getGL2();
glLoaded = true;
gl.glClearColor(
0.95f,
0.95f,
0.95f,
1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_STENCIL_BUFFER_BIT);
}
}
}
private void waitForGlLoad() {
while(!glLoaded) {
updateGraphics();
try {Thread.sleep(1);}
catch (InterruptedException e) {e.printStackTrace();}
}
}
public void run() {
while(!stop) {
waitForGlLoad();
try {
mouseX = MouseInfo.getPointerInfo().getLocation().x - panel.getLocationOnScreen().x;
mouseY = MouseInfo.getPointerInfo().getLocation().y - panel.getLocationOnScreen().y;
}
catch(IllegalComponentStateException e) {
System.out.println("Component data isn't available : window was closed");
break;
}
panel.repaint();
try {Thread.sleep(1);}
catch (InterruptedException e) {e.printStackTrace();}
}
}
}
PanelListener.java
import java.awt.Point;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBufferInt;
import java.awt.image.Raster;
import java.io.File;
import java.io.IOException;
import java.nio.IntBuffer;
import javax.imageio.ImageIO;
import com.jogamp.opengl.GL;
import com.jogamp.opengl.GL2;
import com.jogamp.opengl.GLAutoDrawable;
import com.jogamp.opengl.GLEventListener;
import com.jogamp.opengl.awt.GLJPanel;
public class PanelListener implements GLEventListener{
GLJPanel panel;
GL2 gl;
public PanelListener(GLJPanel panel) {
super();
this.panel = panel;
}
@Override
public void display(GLAutoDrawable displayable) {
gl.glDrawBuffer(GL.GL_BACK);
gl.glClearColor(
1f,
1f,
1f,
1.0f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL2.GL_DEPTH_BUFFER_BIT | GL2.GL_STENCIL_BUFFER_BIT);
gl.glColor3f(1.0f, 0.0f, 0.0f);
gl.glBegin(GL2.GL_QUADS);
gl.glVertex2f(-0.4f, -0.4f);
gl.glVertex2f( 0.4f, -0.4f);
gl.glVertex2f( 0.4f, 0.4f);
gl.glVertex2f(-0.4f, 0.4f);
gl.glEnd();
gl.glReadBuffer(GL.GL_BACK);
IntBuffer pixels = IntBuffer.allocate(panel.getWidth() * panel.getHeight());
gl.glReadPixels(0, 0, panel.getWidth(), panel.getHeight(), GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, pixels);
saveImage(pixels);
gl.glColor3f(0.0f, 0.0f, 1.0f);
gl.glBegin(GL2.GL_TRIANGLES);
gl.glVertex2f( 0.45f, 0.0f);
gl.glVertex2f(-0.5f , 0.5f);
gl.glVertex2f(-0.5f , -0.5f);
gl.glEnd();
}
public void saveImage(IntBuffer pixels) {
//GENERATING THE RASTER
DataBufferInt bufferedPixels = new DataBufferInt(panel.getWidth() * panel.getHeight());
int[] data = pixels.array();
for(int x = 0; x < panel.getWidth(); x++) {
for(int y = 0; y < panel.getHeight(); y++) {
bufferedPixels.setElem(x + y * panel.getWidth(), data[x + (panel.getHeight() - 1 - y) * panel.getWidth()]);
}
}
pixels.clear();
Raster r = Raster.createRaster(ColorModel.getRGBdefault().createCompatibleSampleModel(panel.getWidth(), panel.getHeight()), bufferedPixels, new Point(0, 0));
//CREATING THE IMAGE
BufferedImage img = new BufferedImage(panel.getWidth(), panel.getHeight(), BufferedImage.TYPE_INT_ARGB);
img.setData(r);
img.flush();
try {
ImageIO.write(img, "png", new File("text_image.png"));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void dispose(GLAutoDrawable displayable) {
}
@Override
public void init(GLAutoDrawable displayable) {
gl = displayable.getGL().getGL2();
gl.glEnable(GL2.GL_LINE_SMOOTH);
gl.glEnable(GL2.GL_POINT_SMOOTH);
gl.glEnable(GL2.GL_SMOOTH);
gl.glHint(GL2.GL_LINE_SMOOTH_HINT, GL2.GL_FASTEST);
gl.glHint(GL2.GL_POINT_SMOOTH_HINT, GL2.GL_NICEST);
gl.glShadeModel(GL2.GL_SMOOTH);
gl.glEnable(GL2.GL_BLEND);
gl.glBlendFuncSeparate(GL2.GL_SRC_ALPHA, GL2.GL_ONE_MINUS_SRC_ALPHA, GL2.GL_ONE, GL2.GL_ONE);
gl.glClearColor(0.3f, 0.3f, 0.3f, 1f);
gl.glClear(GL2.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT | GL.GL_STENCIL_BUFFER_BIT);
}
@Override
public void reshape(GLAutoDrawable displayable, int x, int y, int w, int h) {
panel.repaint();
}
}
And here is the saved image :