Evan Hirtenfeld
Evan Hirtenfeld

Reputation: 43

Making font size smaller than 1 Java GUI

I want to label my hashmarks in my grid for my graph, however when I use even font size 1 it is way to big! Is there a way to make a font size smaller than 1? Am I missing something with how I'm coding it?

Here's the code which generates the grid and attempts to put a label on the hash.

     for (double k = myStart1; k <= myEnd1; k = k + (myEnd1 - myStart1) / 8) {
    g2.setColor(Color.BLACK);
        g2.draw(new Line2D.Double(k, (max - min) / 60, k, -(max - min) / 60));
        String labelx=String.valueOf(k);
        Float xCo=Float.parseFloat(Double.toString(k));
        g2.setFont(new Font("SansSerif",Font.PLAIN,1));
        g2.drawString(labelx, xCo, 0);
    }

Here's a screenshot of the graph produced by x^2.enter image description here

Upvotes: 0

Views: 1195

Answers (1)

username tbd
username tbd

Reputation: 9382

As I'm sure you've already noted, the Font constructor takes an int for the size parameter- effectively rendering impossible the construction of a font (using this method, at least) which has a size between 0 and 1.

I did, however, find the deriveFont method of the Font class particularly interesting:

public Font deriveFont(float size)

Creates a new Font object by replicating the current Font object and applying a new size to it.

Parameters: size - the size for the new Font.

The deriveFont method, which claims to construct a new Font with the given size, takes a float as the parameter- therefore, it might be possible to do something like this:

Font theFont = new Font("SansSerif",Font.PLAIN,1);
theFont = theFont.deriveFont(0.5);
g2.setFont(theFont);

Resulting in a font with a size of 0.5.

Now, I haven't tested this myself- setting up a Graphics program takes time, so you're in a much better position to try it out than me. But just throwing it out there as a possibility.

Upvotes: 5

Related Questions