Java: Graphics in Linux

Java: Graphics in Linux

Other answerers appear to assume that "full screen graphics in Java" necessarily means "a working implementation of AWT". This is, of course, not necessarily true, as it is perfectly possible (some would even say desirable) to use Java without AWT.

Cairo is a 2D graphics rendering library that can be used from Java, and can also be used without X11. It looks at first glance as though it should be possible to configure it for this scenario. You'll need to configure it to use OpenGL rendering, and provide a suitable non-X11 OpenGL implementation (e.g. MesaGL with the 'fbdev' device driver).

SDLJava is a Java port of the popular C SDL game development library. This also should be able to do what you ask for, although it doesn't seem to have been updated since 2005 so if you have any problems with it support may not be forthcoming.

As an alternative, you could always use some fairly simple C code to open and configure the framebuffer, and then use JNI to return the memory-mapped framebuffer as a direct-mode ByteBuffer, so you can draw to it directly.

How do I do graphics easily in a modern linux?


#include<X11/Xlib.h>
#include<stdlib.h>

/* gcc -std=gnu99 -o circle circle.c -lX11 */

int main (int argc, char *argv[])
{

/* connect to the X server and make a window */
Display *dpy = XOpenDisplay (getenv ("DISPLAY"));
Window w = XCreateSimpleWindow (dpy, DefaultRootWindow (dpy),
100, 100, 640, 480, 1,
BlackPixel (dpy, DefaultScreen (dpy)),
WhitePixel (dpy, DefaultScreen (dpy)));

/* raise it and wait */
XSelectInput (dpy, w, StructureNotifyMask);
XMapRaised (dpy, w);
for(XEvent e; ( e.type != MapNotify );
XWindowEvent (dpy, w, StructureNotifyMask, &e));

/* create a graphics context for drawing in the window */
GC g = XCreateGC (dpy, w, 0, NULL);

/* draw a circle */
XDrawArc(dpy,w,g,200,100,150,150,0,360*64);
XFlush(dpy);

/*wait for key press*/
XSelectInput (dpy, w, KeyReleaseMask);
for(XEvent e; ( e.type != KeyRelease );
XWindowEvent (dpy, w, KeyReleaseMask, &e));

/*clean up*/
XDestroyWindow( dpy, w );
XCloseDisplay (dpy);
}

Java 8 graphics glitch when stroking sub-pixel coordinates on Linux

From bug report discussion:

It is an xrender bug -Dsun.java2d.xrender=false cures it.

I haven't checked this solution myself as I didn't received any notifications from bug reporting system besides that it was reviewed. And as it was Linux-only problem it was decided to wait for official fixes as there was not so many Linux users among our customers.

Animation on Transparent-background JFrame Linux

Basically this issue is OS-related. What works for Windows will not work for Linux and vice versa.

For some reason, Linux only allows animated per-pixel-transparency when setting up a BufferStrategy. However, this solution fails on Windows. As a result I have come up with the following code which picks the correct algorithm based on the OS:

static int a = 0;

public static void main(String[] args) {
JFrame f = new JFrame();
JPanel p = new JPanel() {
@Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
g2d.setBackground(new Color(255, 255, 255, 0));
g2d.clearRect(0, 0, f.getWidth(), f.getHeight());
g2d.drawRect(a, a++, 2, 2);
}
};
f.add(p);
f.setUndecorated(true);
f.setBackground(new Color(255, 255, 255, 0));
f.setSize(512, 512);
f.setVisible(true);
f.createBufferStrategy(2);

BufferStrategy bs = f.getBufferStrategy();
while (true) {
try {
Thread.sleep(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (System.getProperty("os.name").contains("indows ")) {
p.repaint();
} else {
Graphics g = null;
do {
try {
g = bs.getDrawGraphics();
p.update(g);
} finally {
g.dispose();
}
bs.show();
} while (bs.contentsLost());
Toolkit.getDefaultToolkit().sync();
}
}
}

This code works for my Windows 7 x64 and my Lubuntu 15.04 x64. Please try out this code out yourself and see if it works for you. I myself don't own a Mac so if someone would please test it for me I would be very grateful. If it does not work for anyone, please let me know.

This is what you're supposed to see:

Sample Image



Related Topics



Leave a reply



Submit