FOLLOW ME Twitter Facebook Вконтакте LinkedIn RSS Feed
Browsing articles tagged with " java"

2048 game in Java

Mar 28, 2014
Category: Java
Tags: ,

I’m so addicted to 2048 and I decided to write it in Java. I combined all algorithms and approaches I found on Github and the Internet, and I finally have the game in just one java file. You can find sources here: https://github.com/bulenkov/2048


IconLoader library

Mar 5, 2014
Category: Java
Tags: ,

Welcome IconLoader a small and useful library for icons handling in Java


Bug with stringWidth() in JDK 1.7.0_40

Sep 13, 2013
Category: Java

Today I found that JDK 1.7.0_40 has a bug in the strings width calculation for some fonts. The problem easily reproduces with Retina displays. I wrote a test to do this:

import javax.swing.*;
import java.awt.*;

/**
 * @author Konstantin Bulenkov
 */
public class TextWidthTest extends JFrame {
  public TextWidthTest() throws HeadlessException {
    super("Text Width Test in JDK 1.7.0_40");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setSize(280, 40);
    final String str = "nnnnnnnnnnnnnnnnnnnn.txt";
    JComponent customComp = new JComponent() {
      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        FontMetrics fm = getFontMetrics(getFont());
        g.setColor(Color.orange);
        g.fillRect(0, 0, fm.stringWidth(str), fm.getHeight());
        g.setColor(Color.blue);
        g.drawString(str, 0, fm.getHeight());
      }
    };
    Font font = new Font("Lucida Grande", Font.PLAIN, 12);
    customComp.setFont(font);
    getContentPane().add(customComp);
    setVisible(true);
  }

  public static void main(String[] args) {
    new TextWidthTest();
  }
}

Oracle_JDK_Wrong_String_Width
I was playing with the test and found out that FontMetrics returns a wrong width for some combinations of symbols and fonts. I decided to have a break after this:

00000410


Font rendering: Apple JDK 6 vs Oracle JDK 1.7.0_40

Sep 12, 2013
Category: Java

The Oracle JDK version 1.7.0_40 contains significant changes in font rendering comparing to latest builds of Apple JDK 1.6

Apple JDK with Quartz
apple-jdk-font-rendering

Oracle JDK 1.7.0_40
oracle-jdk-font-rendering

First screenshot was made with Apple JDK. Quartz Graphics Framework enabled by passing apple.awt.graphics.UseQuartz=true as a system property to render text and graphics. Anyway, fonts and colours under Apple JDK with Quartz look so different in java applications comparing to native ones. The good news is that fonts look natively with new Oracle JDK. It uses new approaches to render texts. Unfortunately, OpenGL doesn’t support SubPixel anti-aliasing, so you can have some problems with non-Retina displays.

Also Apple JDK with Quartz modifies colours while rendering and makes them more soft and warm. Look at the difference in backgrounds on the pictures. They have the same background colour… sort of…


Retina support in Oracle JDK 1.7.0_40 and above

Jun 23, 2013
Category: Java

Hi everyone!

A few days ago I installed Oracle JDK (version 1.7.0_40) built especially for early access purposes. So, it’s not the final release. The build contains tons of bug fixes and backports from JDK 8 for AWT/Swing and that was the main point why I started to play with it. As you might have heard, Retina support was one of the major issues in all 1.7.* releases. I’d say there was no Retina support before the build 1.7.0_40. I’m going to show you some differences of Retina support in Apple and Oracle JDKs.
 

1. isRetina() method

That’s funny, but there is no public API for it in Oracle JDK 7. Here is how we (IntelliJ IDEA team) do it under Apple JDK:

class IsRetina {
  public static final boolean isRetina = isRetina();

  private static boolean isRetina() {
    try {
      final boolean[] isRetina = new boolean[1];
      new apple.awt.CImage.HiDPIScaledImage(1,1,BufferedImage.TYPE_INT_ARGB) {
        @Override
        public void drawIntoImage(BufferedImage image, float v) {
          isRetina[0] = v > 1;
        }
      };
      return isRetina[0];
    } catch (Throwable e) { 
      e.printStackTrace();
      return false;
    }
  }
}

Continue reading »


Smalltalk Support in IntelliJ IDEA

Great news! I recently uploaded initial version of Redline Smalltalk plugin for IntelliJ IDEA. What is Redline Smalltalk? Redline Smalltalk is a Smalltalk for JVM. This literally means you write Smalltalk code and run it using JRE and Redline library. I’ve made it available from your favorite Java IDE IntelliJ IDEA.

What’s in version 1.0?

  • Create a Redline Smalltalk project using IntelliJ wizard
  • Import a Redline Smalltalk project from an existing project
  • Adding Redline Smalltalk support to an existing module
  • Adding Redline Smalltalk module to an existing project
  • Creating a new Smalltalk file based on two templates: class and message
  • Create a Redline Smalltalk run configuration
  • Ability to compile and run Redline Smalltalk from IntelliJ
  • Run output console
  • Ability to download and install Redline Smalltalk from the Module Wizard
  • Simple code highlighting

Download and have fun!


Running java on Debian via SSH

Jan 13, 2013

A few days ago I had a problem: run YouTrack on my VDS. YouTrack is a bug tracker written in Java. First time I run it as

java -jar youtrack.jar 8080 &

And this worked fine, until I closed my ssh session with the server. When you close a ssh session your terminal send the HUP (hangup) signal to warn all depending processes of logout. To avoid the HUP signal to be sent, run your java programs using hohup:

nohup java -jar youtrack.jar 8080 &

How to add Java sources and docs to your JDK on Mac OS X

Jan 2, 2013
Category: Java
Tags: ,

Every time I setup a new Mac OS X virtual machine with Java on board I can’t find java sources (src.zip or src.jar files). Here is a short instruction how to setup JDK with the sources and documentation on Mac OS X.

  1. Go to https://developer.apple.com/downloads and sing in.
  2. Search for ‘java developer’ or simply click https://developer.apple.com/downloads?q=java%20developer
  3. Choose the latest build labeled Java for OS X 201X-0YZ Developer Package where X, Y, Z may vary.
  4. Add this JDK to your IDE as you usually do. If sources are not attached automatically, attach them manually. Here is the path to src.jar
    /Library/Java/JavaVirtualMachines/1.6.0_XX-XXX-XXX.jdk/Contents/Home/src.jar

    where XX may vary from version to version.


Arrow symbols in Java

Oct 14, 2012
Category: Java

Unicode is a major feature which Java supports perfectly. This means you can use ANY unicode symbols in your code. Arrow symbols are quite useful. Here is so-called “The Unicode Arrows Block” with characters codes. To use arrow symbols in a Java application just write something like this:

String msg = "Press \u2191 button to go up";

or, if your favorite editor supports unicode, copy and paste any symbol from here

String msg = "Press ↑ button to go up";
  0 1 2 3 4 5 6 7 8 9 A B C D E F
\u219x
\u21Ax
\u21Bx
\u21Cx
\u21Dx
\u21Ex
\u21Fx