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 6173753
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T23:37:48+00:00 2026-05-23T23:37:48+00:00

Some code needs to be refactored in approx 10 classes, of these classes there

  • 0

Some code needs to be refactored in approx 10 classes, of these
classes there are conditionals scattered throughout the classes painting
text onto the screen where appropriate. I want to add all this code conditional/painting
code into one centralised location.
Is there a design pattern appropriate for this ?

if (fontWidth == 100) {
    fontSize = large;
} else {
    fontSize = small;
}

if (fontWidth == 100) {
    graphics.drawText("100", xVal, yVal);
} else {
    xText = Utils.calculateXText(param1, param2);
    graphics.drawText("200", xVal, yVal);
}

For delegation does it mean I need to write defferent wrapper objects, one for setting the font size and one for drawing the text based on above code ?

public void paint(Graphics graphics){

    super.paint(graphics);
    Font font;


    if(ScreenDimension.getFontWidth() == 320){
        font = Font.getDefault().derive(Font.PLAIN,10,Ui.UNITS_pt);
    }
    else {
        font = Font.getDefault().derive(Font.PLAIN,12,Ui.UNITS_pt);
    }

String strScore = String.valueOf(score);
    graphics.setFont(font); 
    int pixelWidth = font.getAdvance(strScore);


    if(ScreenDimension.getFontWidth() == 320){
        graphics.drawText(strScore, (int) (this.backgroundBitmap.getWidth() * 0.50) - (pixelWidth/2), 10);
    }
    else {
        graphics.drawText(strScore, (int) (this.backgroundBitmap.getWidth() * 0.50) - (pixelWidth/2), 20);
    }

    if(ScreenDimension.getFontWidth() == 320){
        font = Font.getDefault().derive(Font.PLAIN,6,Ui.UNITS_pt);
    }
    else {
        font = Font.getDefault().derive(Font.PLAIN,8,Ui.UNITS_pt);
    }

    int namePixelWidth = font.getAdvance(judgeName);
    graphics.setFont(font);

if(ScreenDimension.getFontWidth() == 320){
    graphics.drawText(judgeName, (int) (this.backgroundBitmap.getWidth() * 0.50) - (namePixelWidth/2), this.backgroundBitmap.getHeight() - font.getHeight() - 2);
}
    else {
        graphics.drawText(judgeName, (int) (this.backgroundBitmap.getWidth() * 0.50) - (namePixelWidth/2), this.backgroundBitmap.getHeight() - font.getHeight() - 15);
    }
}
  • 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-23T23:37:49+00:00Added an answer on May 23, 2026 at 11:37 pm

    You possibly want to use a Strategy pattern, defining something like

    /**
     * Strategy to render some text, of two size variants, centered horizontally
     * at a specified position.
     */
    public interface TextRenderer {
        public enum FontSize { LARGE, SMALL };
        public void render(String text, int x, int y, FontSize size);
    }
    
    /**
     * TextRenderer Factory that uses the ScreenDimension to determine sizes of LARGE
     * and SMALL fonts to use.
     */
    public class ScreenDimensionAwareTextRendererFactory {
        public static TextRenderer getTextRenderer(Graphics gc) {
            Font baseFont = Font.getDefault();
            int fontWidth = ScreenDimension.getFontWidth();
            return fontWidth == 320
                    ? new DefaultTextRenderer(gc, baseFont.derive(Font.PLAIN,10,Ui.UNITS_pt), baseFont.derive(Font.PLAIN,6,Ui.UNITS_pt))
                    : new DefaultTextRenderer(gc, baseFont.derive(Font.PLAIN,12,Ui.UNITS_pt), baseFont.derive(Font.PLAIN,8,Ui.UNITS_pt));
        }
    }
    
    public class DefaultTextRenderer implements TextRenderer {
        protected Map<FontSize,Font> fontSizeMap = new HashMap<FontSize,Font>();
        protected Graphics gc;
        public DefaultTextRenderer(Graphics gc, Font largeFont, Font smallFont) {
            this.gc = gc;
            fontSizeMap.put(LARGE, largeFont);
            fontSizeMap.put(SMALL, smallFont);        
        }
        public void render(String text, int x, int y, FontSize size) {
            Font font = fontSizeMap.get(size)
            int pixelWidth = font.getAdvance(text);
            gc.setFont(font);
    
            // TODO: note here I'm not dealing with the vertical offsets you're using
            // which are dependent upon the font size. It would be possible, but
            // I suspect what you really ought to be doing is consistently rendering
            // text on a baseline.
            // The way you could adjust this to closer match what you appear to be doing
            // would be to have arguments to render() which indicate vertical alignment.
            // something like 
            // TextRenderer.VerticalAlignment = enum { BASELINE, TOP, BOTTOM }
            // and based on something like that you could compute here the y offset
            // based on the selected font.
            // I can't do that now because you're "magic numbers" hard coded don't explain
            // what they're trying to do
            gc.drawText(text, Math.round(x - (pixelWidth / 2)), y);
        }
    }
    
    // your example
    public void paint(Graphics graphics) {
        super.paint(graphics);
    
        String strScore = String.valueOf(score);
        TextRenderer textRenderer = ScreenDimensionAwareTextRendererFactory.getTextRenderer(graphics);
        int middleX = Math.round(this.backgroundBitmap.getWidth() / 2);
        textRenderer.render(strScore, middleX, 10, TextRenderer.TextSize.LARGE);
        textRenderer.render(judgeName, middleX, this.backgroundBitmap.getHeight(), TextRenderer.TextSize.SMALL);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which needs to ensure some data is in a mysql
I am writing some code that needs to be backwards compatible with EXISTING remoting
We have some legacy code that needs to identify in the Page_Load which event
I have some VBA code that needs to talk to a running c# application.
I have some code in a javascript file that needs to send queries back
My index.html page for my project needs some Ruby code so I need to
I have to refactor some sharepoint 2010 code from my collegue. Everytime he needs
I recently refactored some code and now have a static utility class with a
I've got some code that needs to scan through a hierarchical database schema, looking
I'm testing some code that needs to use FileInfo and DirectoryInfo object and, instead

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.