Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6598493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T18:21:21+00:00 2026-05-25T18:21:21+00:00

I’d like to implement a simple bitmap font drawing in Java AWT-based application. Application

  • 0

I’d like to implement a simple bitmap font drawing in Java AWT-based application. Application draws on a Graphics object, where I’d like to implement a simple algorithm:

1) Load a file (probably using ImageIO.read(new File(fileName))), which is 1-bit PNG that looks something like that:

8*8 bitmap font

I.e. it’s 16*16 (or 16*many, if I’d like to support Unicode) matrix of 8*8 characters. Black corresponds to background color, white corresponds to foreground.

2) Draw strings character-by-character, blitting relevant parts of this bitmap to target Graphics. So far I’ve only succeeded with something like that:

    int posX = ch % 16;
    int posY = ch / 16;

    int fontX = posX * CHAR_WIDTH;
    int fontY = posY * CHAR_HEIGHT;

    g.drawImage(
            font,
            dx, dy, dx + CHAR_WIDTH, dy + CHAR_HEIGHT,
            fontX, fontY, fontX + CHAR_WIDTH, fontY + CHAR_HEIGHT,
            null
    );

It works, but, alas, it blits the text as is, i.e. I can’t substitute black and white with desired foreground and background colors, and I can’t even make background transparent.

So, the question is: is there a simple (and fast!) way in Java to blit part of one 1-bit bitmap to another, colorizing it in process of blitting (i.e. replacing all 0 pixels with one given color and all 1 pixels with another)?

I’ve researched into a couple of solutions, all of them look suboptimal to me:

  • Using a custom colorizing BufferedImageOp, as outlined in this solution – it should work, but it seems that it would be very inefficient to recolorize a bitmap before every blit operation.
  • Using multiple 32-bit RGBA PNG, with alpha channel set to 0 for black pixels and to maximum for foreground. Every desired foreground color should get its own pre-rendered bitmap. This way I can make background transparent and draw it as a rectangle separately before blitting and then select one bitmap with my font, pre-colorized with desired color and draw a portion of it over that rectangle. Seems like a huge overkill to me – and what makes this option even worse – it limits number of foreground colors to a relatively small amount (i.e. I can realistically load up and hold like hundreds or thousands of bitmaps, not millions)
  • Bundling and loading a custom font, as outlined in this solution could work, but as far as I see in Font#createFont documentation, AWT’s Font seems to work only with vector-based fonts, not with bitmap-based.

May be there’s already any libraries that implement such functionality? Or it’s time for me to switch to some sort of more advanced graphics library, something like lwjgl?

Benchmarking results

I’ve tested a couple of algorithms in a simple test: I have 2 strings, 71 characters each, and draw them continuously one after another, right on the same place:

    for (int i = 0; i < N; i++) {
        cv.putString(5, 5, STR, Color.RED, Color.BLUE);
        cv.putString(5, 5, STR2, Color.RED, Color.BLUE);
    }

Then I measure time taken and calculate speed: string per second and characters per second. So far, various implementation I’ve tested yield the following results:

  • bitmap font, 16*16 characters bitmap: 10991 strings / sec, 780391 chars / sec
  • bitmap font, pre-split images: 11048 strings / sec, 784443 chars / sec
  • g.drawString(): 8952 strings / sec, 635631 chars / sec
  • colored bitmap font, colorized using LookupOp and ByteLookupTable: 404 strings / sec, 28741 chars / sec
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T18:21:21+00:00Added an answer on May 25, 2026 at 6:21 pm

    Okay, looks like I’ve found the best solution. The key to success was accessing raw pixel arrays in underlying AWT structures. Initialization goes something like that:

    public class ConsoleCanvas extends Canvas {
        protected BufferedImage buffer;
        protected int w;
        protected int h;
        protected int[] data;
    
        public ConsoleCanvas(int w, int h) {
            super();
            this.w = w;
            this.h = h;
        }
    
        public void initialize() {
            data = new int[h * w];
    
            // Fill data array with pure solid black
            Arrays.fill(data, 0xff000000);
    
            // Java's endless black magic to get it working
            DataBufferInt db = new DataBufferInt(data, h * w);
            ColorModel cm = ColorModel.getRGBdefault();
            SampleModel sm = cm.createCompatibleSampleModel(w, h);
            WritableRaster wr = Raster.createWritableRaster(sm, db, null);
            buffer = new BufferedImage(cm, wr, false, null);
        }
    
        @Override
        public void paint(Graphics g) {
            update(g);
        }
    
        @Override
        public void update(Graphics g) {
            g.drawImage(buffer, 0, 0, null);
        }
    }
    

    After this one, you’ve got both a buffer that you can blit on canvas updates and underlying array of ARGB 4-byte ints – data.

    Single character can be drawn like that:

    private void putChar(int dx, int dy, char ch, int fore, int back) {
        int charIdx = 0;
        int canvasIdx = dy * canvas.w + dx;
        for (int i = 0; i < CHAR_HEIGHT; i++) {
            for (int j = 0; j < CHAR_WIDTH; j++) {
                canvas.data[canvasIdx] = font[ch][charIdx] ? fore : back;
                charIdx++;
                canvasIdx++;
            }
            canvasIdx += canvas.w - CHAR_WIDTH;
        }
    }
    

    This one uses a simple boolean[][] array, where first index chooses character and second index iterates over raw 1-bit character pixel data (true => foreground, false => background).

    I’ll try to publish a complete solution as a part of my Java terminal emulation class set soon.

    This solution benchmarks for impressive 26007 strings / sec or 1846553 chars / sec – that’s 2.3x times faster than previous best non-colorized drawImage().

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For some reason, after submitting a string like this Jack’s Spindle from a text
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
this is what i have right now Drawing an RSS feed into the php,
Seemingly simple, but I cannot find anything relevant on the web. What is the
I have some data like this: 1 2 3 4 5 9 2 6
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm making a simple page using Google Maps API 3. My first. One marker
i got an object with contents of html markup in it, for example: string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.