Swing and bitmaps on retina displays - java

I've got a Java desktop app that works, amongst other, on OS X.
Now the new MacBook Pro has a retina display and I'm concerned: how is it going to work regarding Swing?
What about when a Java app uses both Swing components and some bitmap graphics (like custom icons / ImageIcon)?
Shall all desktop Java apps be automatically resized (for example by quadrupling every pixel) or am I going to need to create two versions of my icons set (for example one with 24x24 icons and the other with 96x96 icons) and somehow determine that the app is running on a retina display?

Use IconLoader library. It supports HiDPI images http://bulenkov.com/iconloader/ It also provides a way to work with HiDPI images (drawing, etc)

On Apple's Java 6 you can provide multiple versions of the same image. Depending on the screen (retina or not), one or the other image is picked and drawn.
However, those images have to loaded in a special way:
Toolkit.getDefaultToolkit().getImage("NSImage://your_image_name_without_extension");
For example, if your (regular resolution) image is called: "scissor.png", you have to create a high resolution version "scissor#2x.png" (following the Apple naming conventions) and place both images in the Resources directory of your app bundle (yes, you need to bundle your app).
Then call:
Image img = Toolkit.getDefaultToolkit().getImage("NSImage://scissor");
You can use the resulting image in your buttons and it will be drawn with the right resolution magically.
There are two other "tricks" you can use:
Using an AffineTransform of (0.5, 0.5) on your Graphics2D object before drawing an Image. Also see this java-dev message
Creating a high dpi version of your image programmatically using this hack
The first "trick" (0.5 scaling) by now also works on Oracle's Java 7/8.
I.e. if you draw an image with 0.5 scaling directly to the component's Graphics object, it will be rendered in high resolution on Retina displays (and also with half its original size).
Update
Starting with Java 9, there is better built-in support for images with different resolutions via the MultiResolutionImage interface. For more details, please see this answer.

I can confirm that the scaling your images works with on Oracle Java 1.8. I cannot get the NSImage hack to work on java 1.7 or 1.8. I think this only works with Java 6 from Mac...
Unless someone else has a better solution, what I do is the following:
Create two sets of icons.
If you have a 48pixel width icon create one 48px #normal DPI and another at 96px with 2x DPI. Rename the 2xDPI image as #2x.png to conform with apple naming standards.
Subclass ImageIcon and call it RetinaIcon or whatever.
You can test for a Retina display as follows:
public static boolean isRetina() {
boolean isRetina = false;
GraphicsDevice graphicsDevice = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
try {
Field field = graphicsDevice.getClass().getDeclaredField("scale");
if (field != null) {
field.setAccessible(true);
Object scale = field.get(graphicsDevice);
if(scale instanceof Integer && ((Integer) scale).intValue() == 2) {
isRetina = true;
}
}
}
catch (Exception e) {
e.printStackTrace();
}
return isRetina;
}
Make sure to #Override the width and height of the new ImageIcon class as follows:
#Override
public int getIconWidth()
{
if(isRetina())
{
return super.getIconWidth()/2;
}
return super.getIconWidth();
}
#Override
public int getIconHeight()
{
if(isRetina())
{
return super.getIconHeight()/2;
}
return super.getIconHeight();
}
Once you have a test for the retina screen and your custom width/height methods overridden you can customise the painIcon method as follows:
#Override
public synchronized void paintIcon(Component c, Graphics g, int x, int y)
{
ImageObserver observer = getImageObserver();
if (observer == null)
{
observer = c;
}
Image image = getImage();
int width = image.getWidth(observer);
int height = image.getHeight(observer);
final Graphics2D g2d = (Graphics2D)g.create(x, y, width, height);
if(isRetina())
{
g2d.scale(0.5, 0.5);
}
else
{
}
g2d.drawImage(image, 0, 0, observer);
g2d.scale(1, 1);
g2d.dispose();
}
I do not know how this will work with multiple screens though- is there anyone else that can help out with that???
Hope this code helps out anyway!
Jason Barraclough.
Here is an example of using the scaling as mentioned above:
RetinaIcon is on the left. ImageIcon is on the right

Here is a solution, that works also when the icons are used in the apple menu. There the icon is automatically greyed. So I have implemented a class DenseIcon which paints densely:
public synchronized void paintIcon(Component c, Graphics g, int x, int y) {
if(getImageObserver() == null) {
g.drawImage(getImage0(), x, y, getIconWidth(), getIconHeight(), c);
} else {
g.drawImage(getImage0(), x, y, getIconWidth(), getIconHeight(), getImageObserver());
}
}
How to hook into the greying I have not yet figured out. So as a kludge we return a low res image so that the menu can do its modifications:
public Image getImage() {
Image image = getImage0().getScaledInstance(
getIconWidth(),
getIconHeight(),
Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(image, getDescription());
return icon.getImage();
}
You find the code of the full class here on gist. You need to instantiate the icon class with an URL to an image that is twice the size. Works for 2K displays.

This how icons look like on my retina macbook '12:
On the left side icons in IntelliJ IDEA 11 (swing app) and on the right side IDEA 12 which is claimed to be retinized. As you can see automatically resized icons (on the left) looks pretty ugly.
As far as I know, they, just like the guys from Chrome team, made it by providing double sized icons.

Related

How to take screenshot of any java native application without bringing it to foreground in Java?

I want to take the screenshot of a java native application ( any framework AWT, Swing, JavaFx ) without bringing it to the foreground. Are there any framework-specific methods available for this?
I have tried using Robot class to get the screenshot
private static void capture(int x, int y , int width , int height, String setName) {
Robot robot = new Robot();
Rectangle area = new Rectangle(x, y, width, height);
BufferedImage image = robot.createScreenCapture(area);
ImageIO.write(image, "png", new File(System.getProperty("user.dir") + "\\images\\" + setName +".png"));
}
Now robot class with just take the area coordinates and capture the image, whether the target application is on the top or not, to get the application on the top I am using JNA to bring it to focus
private static void bringToFocus() {
for (DesktopWindow desktopWindow : WindowUtils.getAllWindows(true)) {
if (desktopWindow.getTitle().contains("notepad")) {
HWND hwnd = User32.INSTANCE.FindWindow(null, desktopWindow.getTitle());
User32.INSTANCE.SetForegroundWindow(hwnd);
break;
}
}
}
But this is an example where we need to capture only one application, if we need to capture 10 applications screenshots we need to one by one bring them to the front and capture and bring next.
Is there any framework specific method availabe which can take the application screenshot without bringing it to the front.
If your screen shot only needs to be of the Java GUI, you can paint to a BufferedImage
public static Image screenShot(Component c) {
BufferedImage im = new BufferedImage(c.getWidth(), c.getHeight(), BufferedImage.TYPE_INT_ARGB);
Graphics g = im.getGraphics();
c.paint(g); // Paint is the proper entry point to painting a (J)Component, rather than paintComponent
g.dispose(); // You should dispose of your graphics object after you've finished
return im;
}
If your requirement is to paint the Java GUI component along with the rest of the screen, but like your java (J)Frame is in front, you can do that by painting the screen using the robot first, then doing what I've posted above, but with the BufferedImage (which has already been drawn on) being passed in as a parameter rather than being created in the method.

How to render java component to high resolution image?

I'm trying to extract an images of the application window's components in java.
I've created a class that overrides java.awt.GraphicsConfiguration and looks very similar to sun.awt.Win32GraphicsConfig. The problem is that when I call the createAcceleratedImage function with component (for example application window) and 2x larger dimensions than the real one, the java renders it small in the image on the top left corner and the rest of the image is empty space.
My createAcceleratedImage function is same as in sun.awt.Win32GraphicsConfig:
public Image createAcceleratedImage(Component target, int w, int h) {
ColorModel localColorModel = getColorModel(Transparency.OPAQUE);
WritableRaster localWritableRaster = localColorModel.createCompatibleWritableRaster(w, h);
return new OffScreenImage(target, localColorModel, localWritableRaster, localColorModel.isAlphaPremultiplied());
}
I apologize if I forgot to mention more information, If you need more information please ask in comments.
Thank you for help.
I used VolatileImage instead of OffScreenImage inside my CustomComponentPeer.
public VolatileImage createVolatileImage(int width, int height) {
GraphicsConfiguration graphicsConfig = getGraphicsConfiguration();
return graphicsConfig.createCompatibleVolatileImage(width, height);
}
I found that drawImage function in Graphics2D support high resolution rendering only for MultiResolutionImage and VolatileImage. Rendering in high DPI using OffScreenImage is not supported.

Using JLabel for text on custom drawn component causes painting bug

My little project involves a diagram editing tool, in which different sized boxes can be linked together. The drawing requirement for this is complex to say the least. You see the idea is that one can zoom in/out as far as they like and create ever smaller/larger boxes. This is sort of like a Mandelbrot, but as a diagram tool. This is shown in the image below.
These boxes have text in them and my overall aim was for this text to be centered in the absolute center of the box. The only simple way I know for text to both center and wrap is by using JLabels. But when I tried this I came across a strange bug. The labels with multiple lines were prone to "disalign" vertically every other time they were repainted. (Sometimes this happened, other times it didn't. It has some consistency in that it keeps its behaviour when I replicate the same movement and positioning). This bug is shown in the image below.
Depending on the position, it would effectively alternate between the two images above. I have isolated the drawing of each box and ensured there was no escaping graphics transformations, and it seems this problem is down to the label painting.
I am aware that the practices I've used may not be preferable (e.g. painting JLabel directly), however I struggle to find any decent alternatives. If you guys can come up with a good alternative to using JLabels for centering and word wrap I'm happy to try it. Any ideas are welcome really.
Below is the drawing code for a Box. The colors are temporary.
public void draw(Graphics2D g, Map map, int fillAlpha, int lineAlpha, double zoom)
{
if(this == map.selectedBox)
{
if(this == map.highlightedBox)
g.setColor(new Color(200,235,235,fillAlpha));
else
g.setColor(new Color(200,225,225,fillAlpha));
}
else
{
if(this == map.highlightedBox)
g.setColor(new Color(235,235,200,fillAlpha));
else
g.setColor(new Color(225,225,200,fillAlpha));
}
g.fill(rect);
if(this == map.selectedBox)
g.setColor(new Color(0,0,255,lineAlpha));
else
g.setColor(new Color(0,0,0,lineAlpha));
g.draw(rect);
g.translate(rect.x, rect.y);
label.setForeground(g.getColor());
label.paint(g);
g.translate(-rect.x, -rect.y);
}
Below is the drawing code for the whole diagram.
public void draw(Graphics2D g, int layer, Color aboveColor)
{
for(Link link : linksObj(layer))
{
g.setColor(Color.black);
link.draw(g, this, aboveColor);
}
AffineTransform at = g.getTransform();
g.scale(getZoom(layer), getZoom(layer));
double ratio = getRatio(layer);
int fill = getAnimAlpha(255, 0, ratio);
int line = getAnimAlpha(255, 32, ratio);
for(Box box : boxesObj(layer))
box.draw(g, this, fill, line, getZoom(layer));
g.setTransform(at);
}
The label uses the following to center the text.
public String convertForLabel(String in)
{
return "<html><div style=\"text-align: center;\">"+in.replaceAll("\n", "<br>")+"</html>";
}
Edit: I have created a test project for you to see the problem yourself. It is only one class so I just uploaded that. You can find it here: JLabel Bug Class

Why does VolatileImage have no set/getPixel() method

I am a relative newbie in game programming. I know how to draw pixels to a BufferedImage using setPixel(). It is horribly slow on larger formats so I moved on and found VolatileImage (took me a week or so). It is fairly easy to draw lines, strings, rects, etc but I can't draw individual pixels. I already tried using drawLine(x,y,x,y) but I get 3-4 FPS on an 800x600 image.
The fact that java didn't include setPixel() or setRGB() in the VolatileImage makes me pretty angry and confused.
I have 4 questions:
Is there a way to draw individual pixels on a VolatileImage? (on 1440x900 formats with FPS > 40)
Can I draw pixels in a BufferedImage with a faster method? (same 1440x900, FPS > 40)
Is there any other way to draw pixels fast enough for 3D games?
Can I make my BufferedImage hardware accelerated( tried using setAccelerationPriority(1F) but it doesn't work)
Please if you have any idea tell me. I can't continue making my game wihout this information. I already made 3D rendering algorithms but i need to be able to draw fast pixels. I have got a good feeling about this game.
Here's the code if it can help you help me:
public static void drawImageRendered (int x, int y, int w, int h) { // This is just a method to test the performance
int a[] = new int[3]; // The array containing R, G and B value for each pixel
bImg = Launcher.contObj.getGraphicsConfiguration().createCompatibleImage(800, 600); // Creates a compatible image for the JPanel object i am working with (800x600)
bImg.setAccelerationPriority(1F); // I am trying to get this image accelerated
WritableRaster wr = bImg.getRaster(); // The image's writable raster
for (int i = 0; i < bImg.getWidth(); i++) {
for (int j = 0; j < bImg.getHeight(); j++) {
a[0] = i % 256;
a[2] = j % 256;
a[1] = (j * i) % 256;
wr.setPixel(i, j, a); // Sets the pixels (You get a nice pattern)
}
}
g.drawImage(bImg, x, y, w, h, null);
}
I would much prefer not using OpenGL or any other external libraries, just plain Java.
Well you're basically drawing one pixel after the other using the CPU. There's no way that this can be accelerated, thus such a method does simply not make any sense for a VolatileImage. The low FPS you get suggest that this even causes a significant overhead, as each pixel drawing operation is sent to the graphics card (with information such as location & colour), which takes longer than to modify 3 or 4 bytes of RAM.
I suggest to either stop drawing each pixel separately or to figure out a way to make your drawing algorithm run directly on the graphics card (which most likely requires another language than Java).
It's been over 4 years since this post got an answer. I was looking for an answer to this question as well and stumbled on this post. After some more searching, I got it to work. Below I'll post the source to rendering pixels with a VolatileImage.
It seems Java hides our ability to plot pixels directly to a VolatileImage, but we can draw buffered images to it. For good reason. Using the software to plot a pixel doesn't really help with acceleration(in Java it seems). If you can plot pixels to a BufferedImage, and then render it on a VolatileImage, you may get a speed bonus since it's hardware accelerated from that point.
The source down below is a self-contained example. You can copy-pasta practically all of it to your project and run it.
https://github.com/Miekpeeps/JavaSnippets-repo/blob/master/src/graphics_rendering/pixels_03/PlottingVolatile.java
In the constructor I save the Graphics environment of the app/game.
private GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
private GraphicsConfiguration gc = ge.getDefaultScreenDevice().getDefaultConfiguration();
Then, when I call a method to enable hardware we create a buffer. I set the transparency to Opaque. In my little engine, I deal with transparency/alpha blending on another thread in the pipeline.
public void setHardwareAcceleration(Boolean hw)
{
useHW = hw;
if (hw)
{
vbuffer = gc.createCompatibleVolatileImage(width, height, Transparency.OPAQUE);
System.setProperty("sun.java2d.opengl", hw.toString()); // may not be needed.
}
}
For each frame I update, I get the Graphics from the VolatileImage and render my buffer there. Nothing gets rendered if I dont flush().
#Override
public void paintComponent(Graphics g)
{
if(useHW)
{
g = vbuffer.getGraphics();
g.drawImage(buffer, 0, 0, null);
vbuffer.flush();
}
else
{
g.drawImage(buffer, 0, 0, null);
buffer.flush();
}
}
There is still a little bit of overhead when calling to plot a pixel on the BufferedImage writable raster. But when we update the screen, we get a speed boost when using the Volatile image instead of using the Buffered image.
Hope this helps some folks out. Cheers.

how to create and print form using java

maybe someone can give hand of help and tell how to create and print form
like this:
using java.
Also, it should be filled with needed information.
java.awt.print - Java 2D printing, since JDK 1.2
javax.print - aka the Java Print Service (JPS) API, since JDK 1.4
from http://java.sun.com/javase/technologies/desktop/printing/
I think you need a bit of googling - it looks like a very trivial task.
If you are using Swing, the follow the procedure below:
For A4 setting:
Use a JFrame of approx. 750 px. X 960 px.
In the Window use JLabels, JTextFields and JTextAreas to Design the template.
Also do add a print button anywhere on the window (to initiate the print command).
Now when all designing is complete, in the code window of the button action event, simply
add:
<Button Name>.setVisible(false);
<PanelName>.print();
First one will hide the Button, second will actually present you with a print dialog.
Additionally, use Netbeans IDE to save time in designing. It is a great time saver in the designing, compiling and testing grounds.
Please revert back for any doubts, Hope the information is helpful.
If you need to do it in a web application, the printing should be done in javascript. But you may render the page using Java. http://shyarmal.blogspot.com/2011/08/printing-example-with-java-ee.html
If you are doing it using swing: http://shyarmal.blogspot.com/2011/08/printing-with-jeditorpane.html
A little late, but I'll leave this here for reference:
//pertinent code only
import java.awt.print
public void FilePrintClicked(){
PrinterJob job = PrinterJob.getPrinterJob();
PageFormat format = job.defaultPage();
format.setOrientation(PageFormat.LANDSCAPE);
job.setPrintable(this, format);
try{
if(job.printDialog()) job.print();
}
catch(Exception e){e.printStackTrace();}
}
public int print(Graphics g, PageFormat format, int pagenum) {
if (pagenum > 0){
return Printable.NO_SUCH_PAGE;
}
g.translate((int)format.getImageableX(), (int)format.getImageableY());
float pageWidth = (float)format.getImageableWidth();
float pageHeight = (float)format.getImageableHeight();
float imageHeight = (float)this.getHeight();
float imageWidth = (float)this.getWidth();
float scaleFactor = Math.min((float)pageWidth/(float)imageWidth, (float)pageHeight/(float)imageHeight);
int scaledWidth = (int)(((float)imageWidth)*scaleFactor);
int scaledHeight = (int)(((float)imageHeight)*scaleFactor);
BufferedImage canvas = new BufferedImage( this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D gg = canvas.createGraphics();
this.paint( gg );
Image img = canvas ;
g.drawImage(img, 0, 0, scaledWidth, scaledHeight, null );
return Printable.PAGE_EXISTS;
}
Note: Your class needs to implement Printable
It's a little dirty, but it's rather old code from when I was learning Java and I didn't double-check it as I posted it here, but it's working in my application so.....

Categories