Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: Scaling text question

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Scaling text question

    What I am creating is a clock. That goes round n round using "getcalendar".
    It's realtime, but the clock has a constant resolution of 800x800.
    I have carefully placed numbers from 1-12 on the clock using that resolution placing them pixel by pixel.
    Now if I resize the window, the clock resizes but the text, it fades away out of the screen because the resolution is too small.

    This is how I draw them.

    g2.drawString(six, 380, 750);
    g2.drawString(twelve, 375, 70);
    g2.drawString(nine, 30, 415);
    g2.drawString(three, 725, 415);
    g2.drawString(one, 570, 120);
    g2.drawString(two, 690, 250);
    g2.drawString(four, 685, 575);
    g2.drawString(five, 570, 700);
    g2.drawString(seven, 200, 710);
    g2.drawString(eight, 80, 585);
    g2.drawString(ten, 80, 235);
    g2.drawString(eleven, 205, 115);

    I understand why this is happening, obviously because the strings are in a certain place just can't understand how do I make them scaleable?
    How were I to solve the problem? Is there an universal formula? Or do I have to draw every string relevant to some mathematical equation regarding to the screen size. For example : g2.drawString(twelve, diameter / 2, screenheight / 3);
    It might be hard to understand without pictures, what you guys are afraid to click on for some reason.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scaling text question

    I may not understand what you're asking, but I'll take a stab at it: The drawString() method prints the specified String using the currently set font at the currently set font size. If you are changing the size of the Frame, you may have to proportionally adjust the font size. I don't know of a formula or rule of thumb for this adjustment, but I'm sure you can come up with something acceptable with a little experimentation.

    If I've completely misunderstood, please explain what you mean by "fades away."

  3. #3
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scaling text question

    Fades away being just that the drawstring() character moves out of it's position.
    Which is logical. I mean when I set the string to be at 550 x 540.
    But then change window size to 200x200 ofcourse it is gone. But my goal would be to make so the text remains in the same position and the font size resizes when making the window smaller or bigger.

    Thanks for your reply.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scaling text question

    Setting the x/y location for the start of the string's location in the frame is simple enough. If you've chosen locations you like for a 550 x 540 frame, then scale those as the frame size changes. For a change from 550 x 540 to 200 x 200, the x/y starting string position would scale to

    xNew = xOld * 200 / 550 (new width / old width ),

    yNew = yOld * 200 / 540 (new height / old height )

    As mentioned before, you should experiment with scaling the font size using corresponding factors. Since font size affects both height and width, you might scale the font based on an average of the x/y changes or choose the most significant scaling factor of the two. You should strive to obtain a scaling algorithm that results in the same x/y and font size for a specific frame size. E.g., if x/y and font size are scaled from 500 x 1000 to 150 x 200 and then back to 500 x 1000, the x/y and font sizes for 500 x 1000 should be the same at the start and at the end.

    Let us know what you come up with.

  5. #5
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Java Custom font

    Hello people,
    After some 2 failed thread's I've made <--- my fault obviously because I couldn't make it clear what I wanted.

    Lets start off with what I'm doing: I'm developing a clock thats set on a 800x800 window.
    I have drawn roman numbers on the clock as such.

    Font numberfont = new Font("SansSerif", Font.PLAIN, 50);
            g2.setFont(numberfont);
     
            g2.drawString(six, 380, 750);        
            g2.drawString(twelve, 375, 70);
            g2.drawString(nine, 30, 415);
            g2.drawString(three, 725, 415);
            g2.drawString(one, 570, 120);
            g2.drawString(two, 690, 250);
            g2.drawString(four, 685, 575);
            g2.drawString(five, 570, 700);
            g2.drawString(seven, 200, 710);
            g2.drawString(eight, 80, 585);
            g2.drawString(ten, 80, 235);
            g2.drawString(eleven, 205, 115);

    Everything works perfect on 800x800 but if I were to resize the window everything will be thrown out of place.

    So what I would need to do is to set the coordinates of the "DrawString" relative to the screen? Simple enough I tought.

    I experimented with the number "Six".
    380 / 800 * 100 = 47.5
    750 / 800 * 100 = 93.75
     
    g2.drawString(six, (_clockImage.getWidth() / 100) * 47, (_clockImage.getHeight() / 100) * 94);

    Upon testing It somewhat worked but It would still be out of place if X is longer than Y and if their not correspondingly resized.

    Also upon resizing the window smaller the testing number "Six" was still too big, the original FONT had not changed so what I need secondly is a CUSTOM font that resizes according to the window size. No luck with figuring out this one. So I looked around an article, some guy was trying to scale font to fit a rectangle.

    public Font scaleFont(String text, Rectangle rect, Graphics g, Font pFont) {
        float fontSize = 20.0f;
        Font font = pFont;
     
        font = g.getFont().deriveFont(fontSize);
        int width = g.getFontMetrics(font).stringWidth(text);
        fontSize = (rect.width / width ) * fontSize;
        return g.getFont().deriveFont(fontSize);
    }

    I would need a custom font to scale relatively to the window size of my clock, this is hard to explain by writing, also I'm not trying to fit a rectangle here this is just something it would look like. I'll GIF the whole process so you'll see what is wrong.

    Here is the GIF: Gyazo - d58b05e220ce0a51eebf785caf95664d.gif
    The clock scales as it should but the text is not.

    Thanks in advance,
    Trolltrain
    Last edited by Trolltrain; July 14th, 2014 at 06:00 AM.

  6. #6
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Scaling text question

    Threads merged.

    Please don't post duplicate threads on the same topic. You may think the previous threads were failed attempts, but they contain the efforts of others to help based on their understanding of the problem at that time. If your understanding and explanation of the problem evolves, then that evolution is important to those trying to help and those following along who may try to help. The history allows us to move forward rather than starting from ground zero each time you decide to start a new thread.
    this is hard to explain by writing
    A way to explain to programmers is to post some code with the instructions needed to run and demonstrate the problem. You were also given advice in earlier posts on how to attack this problem, so provide some feedback on your efforts to incorporate that advice or provide additional info to explain why those suggestions misunderstood the question and are invalid or not helpful.

  7. #7
    Junior Member
    Join Date
    May 2014
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Scaling text question

    Okay I have figured out on how to make the numbers scale.

       Rectangle r = ClockStart.window.getBounds();
            int h = r.height;
            int w = r.width;

     g2.drawString(six,(w / 100) * 47 , (h / 100) * 94);
            g2.drawString(twelve, (w / 100) * 46, (h / 100) * 10);
            g2.drawString(nine, (w / 100) * 4, (h / 100) * 51);
            g2.drawString(three, (w / 100) * 90, (h / 100) * 52);
            g2.drawString(one, (w / 100) * 71, (h / 100) * 15);
            g2.drawString(two, (w / 100) * 86, (h / 100) * 31);
            g2.drawString(four, (w / 100) * 85, (h / 100) * 72);
            g2.drawString(five, (w / 100) * 71, (h / 100) * 87);
            g2.drawString(seven, (w / 100) * 25, (h / 100) * 89);
            g2.drawString(eight, (w / 100) * 10, (h / 100) * 73);
            g2.drawString(ten, (w / 100) * 10, (h / 100) * 29);
            g2.drawString(eleven, (w / 100) * 25, (h / 100) * 14);

    Some pretty calculations, it's a bit sloppy tho still but it's a moderate success.
    So what I did was I calculated the precentage of their position under normal circumstances.
    If the starting coordinates were 375 and 700 in an 800 by 800 window.
    375 / 800 * 100 = almost 47%
    Making the x coordinate at (w(idth) / 100) * 47 making the x coordinate relative to the screen at all times.

    It's a bit unaccurate because I am unable to add doubles to a drawString.

    I am still facing a problem as the font remains unchanged. If anyone would know how to make a custom font relative to the window size could you please give me some leads?

    If the default font size is 50 it would go down if the window gets smaller and go up if the window gets bigger.
    Any help is appreciated.

    Best Regards,

    Trolltrain

Similar Threads

  1. Replies: 1
    Last Post: April 10th, 2014, 06:07 AM
  2. Newbie Question...GUI Text input Areas
    By steme in forum Java Theory & Questions
    Replies: 3
    Last Post: April 1st, 2013, 08:45 PM
  3. Scaling a graph in console
    By mysticCHEEZE in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 8th, 2011, 03:31 PM
  4. Help with Polynomial Graph Plotter and its scaling
    By sojharo in forum What's Wrong With My Code?
    Replies: 0
    Last Post: January 23rd, 2011, 06:04 PM
  5. image scaling on window resize
    By chopficaro in forum Java Theory & Questions
    Replies: 2
    Last Post: August 27th, 2010, 03:09 PM