1

My application performs data visualization using gray-scale "heat map". Above it I need to paint time axis in yellow color. It looks good on black background, but becomes invisible on white background (see attached image). How to make it visible regardless of background?

Here is how I paint the timestamps:

g.setColor(Color.yellow);
g.drawString("12:43:15", x, y);

where g is java.awt.Graphics object

enter image description here

Serg
  • 13,470
  • 8
  • 36
  • 47

4 Answers4

6

What about XOR'ing your Color. For e.g.,

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;

import javax.swing.*;

@SuppressWarnings("serial")
public class XorEg extends JPanel {
   private static final int PREF_W = 800;
   private static final int PREF_H = PREF_W / 4;
   private static final float SIZE = 24f;
   private String text = "Hello world, how's it going? ";

   public XorEg() {
      setFont(getFont().deriveFont(SIZE));
      for (int i = 0; i < 2; i++) {
         text += text;
      }
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(Color.black);
      int x = 0;
      int y = 0;
      int width = getWidth() / 2;
      int height = getHeight();
      g.fillRect(x, y, width, height);

      g.setColor(Color.white);
      x = width;
      g.fillRect(x, y, width, height);

      g.setXORMode(Color.blue);

      g.drawString(text, 10, PREF_H / 2);
   }

   @Override
   public Dimension getPreferredSize() {
      return new Dimension(PREF_W, PREF_H);
   }

   private static void createAndShowGui() {
      XorEg mainPanel = new XorEg();

      JFrame frame = new JFrame("XorEg");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

which shows:

xor color

Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
4

Put a rectangle behind the text, and paint it a dark translucent color.

E.G. as seen in this answer (OK that is 'dark on light' as opposed to 'light on dark' but ..Batteries Not Included).

Here is another example that uses the same 'outline' approach as mentioned by @MadProgrammer.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
4

You could "outline" the text (in black for example)

For example...

Outline

As demonstrated in Assigning a image to a String

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
2

Use another color that works good with black and white or detect lower color and change text color based on that, which is a better approach

Issam Zoli
  • 2,724
  • 1
  • 21
  • 35