Reputation: 31
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.graphics.ImageLoader;
import org.eclipse.swt.widgets.Display;
....
display = new Display();
image = new Image(display, chosenImageFilePath);
gc = new GC(image);
gc.drawText("text to be drawn", 0, 0, SWT.DRAW_TRANSPARENT);
gc.dispose();
ImageLoader loader = new ImageLoader();
loader.data = new ImageData[] { image.getImageData() };
loader.save(destinationImageFilePath, SWT.IMAGE_JPEG);
image.dispose();
display.dispose();
I'm drawing some text on the picture and it all works on windows using eclipse. It also works when I export it as a runnable jar, and run it via cmd.
The main intention of making the program was so I could use it in my web app (tomcat, java, jsp, mysql) which is now running on Centos 7 cloud server. I get this error when trying to run it:
I have downloaded swt-3.6.1-gtk-linux-x86_64 and added it into eclipse's project libraries and build path, but it didn't seem to fix it. Any ideas? Can I even run this on a virtual server, where there's no graphics card? PS I'm not showing most of my code (which works and doesn't cause errors) that just calculates where the text will be drawn on the picture.
Upvotes: 1
Views: 835
Reputation: 31
Answering my own question. Alright, so...
1.Imported this .jar into project's build path:
org.eclipse.swt.gtk.linux.x86_64-4.3.jar
Using
yum install xorg-11-server-Xvfb
I've enabled usage of virtual buffer to replace the lack of a graphic environment.
Next step was to activate the virtual display:
Xvfb :1 -ac -screen 0 1024x768x8 & export DISPLAY=:1
And then I was able to run my runnable jar.
PS Perhaps something wasn't explained the best way, but these are the steps that enabled me to use my app.
Upvotes: 2