Getting the width of a String, or centering it within the Applet.

Hello,
I want to get a String within the center of my 2D Applet, so I'm wondering if it's possible to get the width (and height) of the String in numbers, or even if it's possible to center like Microsof Word does. If one of the two is possible, please tell it me :)
Thanks
PS: I use fonts and a size for the font, could this create problems?
Edited by: The_Pointer on Mar 27, 2009 3:34 PM
Edited by: The_Pointer on Mar 27, 2009 3:34 PM

Can someone explain the
Graphics.getFontMetrics()
x = (Component.getWidth() - Fontmetrics.stringWidth()) / 2 a little bit more please?
Since I get the following errors
C:\Documents and Settings\Robert van der Pijl\Bureaublad\Clock\clockApplet.java:66: non-static method getFontMetrics() cannot be referenced from a static context
          Graphics.getFontMetrics();
                  ^
C:\Documents and Settings\Robert van der Pijl\Bureaublad\Clock\clockApplet.java:67: x is not public in java.awt.Component; cannot be accessed from outside package
          x = (Component.getWidth() - Fontmetrics.stringWidth()) / 2;
          ^
C:\Documents and Settings\Robert van der Pijl\Bureaublad\Clock\clockApplet.java:67: non-static method getWidth() cannot be referenced from a static context
          x = (Component.getWidth() - Fontmetrics.stringWidth()) / 2;
                        ^
C:\Documents and Settings\Robert van der Pijl\Bureaublad\Clock\clockApplet.java:67: cannot find symbol
symbol  : variable Fontmetrics
location: class clockApplet
          x = (Component.getWidth() - Fontmetrics.stringWidth()) / 2;
                                      ^
4 errors

Similar Messages

  • How can I adjust the width of email display so text remains within the width of display

    Most emails are narrow enough to fit within the width of my display, occasionally I receive an email that spills of the edge of my screen and I need to move focus of my display back & firth with every line, is there a way to display in word wrap.?

    You can set the width of the word wrap in about:config. Get to about:config from Options > Advanced > General > Config Editor. Search for "wrap" and set mailnews.wraplength to whatever works for you. You should also set mail.wrap_long_lines to true.

  • What is the advantages and disadvantages of having 2012 DHCP within the Domain or its better to keep it stand alone

    What is the advantages and disadvantages of having 2012 DHCP within the Domain or its better to keep it stand alone
    I am Trying to upgrade Current DHCP 2003 Server to 2012 to get benefit from the latest fail over plan and I Want to be sure that it doesn’t affect the current setup

    Hi,
    For DHCP migration, there won’t be any problem. You can follow the guide below:
    Step-By-Step: Migration of DHCP from Windows Server 2003 to Windows Server 2012
    http://blogs.technet.com/b/canitpro/archive/2013/04/29/step-by-step-migration-of-dhcp-from-windows-server-2003-to-windows-server-2012.aspx
    If you have any other role installed on windows server 2003, please read the relevant article:
    Migrate Roles and Features to Windows Server 2012
    http://technet.microsoft.com/en-us/library/dn486809.aspx
    Hope this helps.

  • HT1349 What if I Tunes can't find the album artwork for some of my CDs' within the stoThere should be a way for it tore ?  download the album artwork anyway wheb downloading a disc ,should'nt there? My friend does it with all his CDs'. I cant' ask him tho

    What if I Tunes can't find the album artwork for some of my CDs' within the i-tunes store? There should be a way for it to download the Album Artwork automatically anyway ,shoild'nt there?My friens do it with all there CDs' but they moved.They do it with CDs' that i-tunes do'nt even know exist .I just can't figure it out .Any advice or help out there ?

    "If the content doesn't exist in iTunes then you can'd load artwork."
    If not in the iTunes Store, iTunes uses the Graceland database for artwork and tag info.

  • Using getter/setter for returing a string variable to display on an Applet

    have two classes called, class A and class testA.
    class A contains an instance variable called title and one getter & setter method. class A as follow.
    public class A extends Applet implements Runnable, KeyListener
         //Use setter and getter of the instance variable
         private String title;
         public void init()
              ASpriteFactory spriteFactory = ASpriteFactory.getSingleton();
              // Find the size of the screen .
              Dimension theDimension = getSize();
              width = theDimension.width;
              height = theDimension.height;
              //Create new ship
              ship = spriteFactory.createNewShip();
              fwdThruster = spriteFactory.createForwardThruster();
              revThruster = spriteFactory.createReverseThruster();
              ufo = spriteFactory.createUfo();
              missile = spriteFactory.createMissile();
              generateStars();
              generatePhotons();
              generateAsteroids();
              generateExplosions();
              initializeFonts();
              initializeGameData();
              //Example from Instructor
              //setMyControlPanel( new MyControlPanel(this) );
              // new for JDK 1.2.2
              addKeyListener(this);
              requestFocus();
         public void update(Graphics theGraphics)
              // Create the offscreen graphics context, if no good one exists.
              if (offGraphics == null || width != offDimension.width || height != offDimension.height)
                   // This better be the same as when the game was started
                   offDimension = getSize();
                   offImage = createImage(offDimension.width, offDimension.height);
                   offGraphics = offImage.getGraphics();
                   offGraphics.setFont(font);
              displayStars();
              displayPhotons();
              displayMissile();
              displayAsteroids();
              displayUfo();
              //displayShip();
              //Load the game with different color of the space ship          
              displayNewShip();
              displayExplosions();
              displayStatus();
              displayInfoScreen();
              // Copy the off screen buffer to the screen.
              theGraphics.drawImage(offImage, 0, 0, this);
         private void displayInfoScreen()
              String message;
              if (!playing)
                   offGraphics.setColor(Color.white);
                   offGraphics.drawString("\'A\' to Change Font Attribute", 25, 35);
                   offGraphics.drawString(getTitle(), (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             - fontHeight);
                   message = "The Training Mission";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2);
                   message = "Name of Author";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             + fontHeight);
                   message = "Original Copyright 1998-1999 by Mike Hall";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 2
                             + (fontHeight * 2));
                   if (!loaded)
                        message = "Loading sounds...";
                        int barWidth = 4 * fontWidth + fontMetrics.stringWidth(message);
                        int barHeight = fontHeight;
                        int startX = (width - barWidth) / 2;
                        int startY = 3 * height / 4 - fontMetrics.getMaxAscent();
                        offGraphics.setColor(Color.black);
                        offGraphics.fillRect(startX, startY, barWidth, barHeight);
                        offGraphics.setColor(Color.gray);
                        if (clipTotal > 0)
                             offGraphics.fillRect(startX, startY, (barWidth * clipsLoaded / clipTotal), barHeight);
                        offGraphics.setColor(Color.white);
                        offGraphics.drawRect(startX, startY, barWidth, barHeight);
                        offGraphics
                                  .drawString(message, startX + 2 * fontWidth, startY + fontMetrics.getMaxAscent());
                   else
                        message = "Game Over";
                        offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4);
                        message = "'S' to Start";
                        offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4
                                  + fontHeight);
              else if (paused)
                   offGraphics.setColor(Color.white);
                   message = "Game Paused";
                   offGraphics.drawString(message, (width - fontMetrics.stringWidth(message)) / 2, height / 4);
         public String getTitle() {
              System.out.print(title);
              return title;
         public void setTitle(String title) {
              this.title = title;
    }displayInfoScreen method in class A calls out for getTitle( ) to be displayed on an applet as an initial display string for the application.
    The instance variable title is set by setTitle method which is called out in class testA as follow,
    public class testA extends TestCase
          * testASprite constructor comment.
          * @param name
          *          java.lang.String
         public testA(String name)
              super(name);
          * Insert the method's description here.
          * @param args
          *          java.lang.String[]
         public static void main(String[] args)
              junit.textui.TestRunner.run(suite());
              // need to figure out how to get rid of the frame in this test
              System.exit(0);
         public static Test suite()
              return new TestSuite(testA.class);
          * Basic create and simple checks
         public void testCreate()
              A theGame = new A();
              assertNotNull("game was null!", theGame);
          * Basic create and simple checks
         public void testInit()
              A theGame = new A();
              Frame gameFrame = new Frame("THE GAME");
              gameFrame.add(theGame);
              int width = 640;
              int height = 480;
              gameFrame.setSize(width, height);
              // must pack to get graphics peer
              gameFrame.pack();
              theGame.resize(width, height);
              theGame.setTitle("TEST THE GAME");
              theGame.init();
              assertEquals("ASprite width not set", A.width, width);
              gameFrame.dispose();
              gameFrame.remove(theGame);
    }Basically, class testA invokes the init( ) method in class A and start the applet application. However, it displays a white blank display. If I change the getTitle( ) in the displayInfoScreen method to a fixed string, it works fine. Did I forget anything as far as using getter & setter method? Do I have to specify some type of handle to sync between setter and getter between two classes? Any feedback will be greatly appreciated.
    Thanks.

    Your class A extends runnable which leads me to believe that this is a multi-threaded application. In that case, title may or may not be a shared variable. Who knows? It's impossible to tell from what you posted.
    Anyway, what is happening is that your applet is being painted by the JFrame before setTitle is called. After that, who knows what's happening. It's a complicated application. I suspect that if you called setTitle before you added the applet to the frame, it would work.

  • I get a Runtime error when i try to search within the iTunes store

    Please help! When in the iTunes store, I get a runtime error every time I try to use the search feature. I get a dialog box stating that Windows is searching for a solution, but then iTunes shuts down completely.

    I have the same error, but I think we will get no solution for this problem.
    You've get no answer here and the Apple support also doesn't send an answer how to solve the problem.
    BTW I have also Windows 7 and the newest version of iTunes 64-bit.
    Greetings Mike

  • Lion, Mail, and Outlook. I can't get preferences in Mail to accept Outlook as the default mail program. Outlook works fine within the Office world but not with Preview or Lightroom (to choose two that I've tried today). Any ideas?

    As per the heading, I can't get Lion to accept Outlook 2011 as the default mail program. When I go through the Preferences in Mail to set the detault mail program, I can see Outlook, I can select it, but it doesn't "stick." As soon as I reopen Mail, Mail is the defailt mail program again. It also doesn't work though the Outloook Preferences menu. I'm a recent transfer to Macs, and could use some help. Any ideas?
    Thanks!

    There is a forum specifically for MS Office for Macs, just click Office for Mac Product Forums and post there, they should know.
    If for some reason you don't want to do that you have 90 days of free AppleCare telephone support, this might be a good time to use it. You can find the number in your owners manual.

  • What is the legal way do distribute iPad corporate application within the company employees and avoid it downloading by a million of other users?

    Hello,
    My company has about 1500 employees. The purpose is to distribute our corporate application within their iPads. What are the possible legal ways to distribute?

    You can try these links, but some may require an active account to access:
    Enterprise Guide:
    iPhone OS - Enterprise Deployment Guide
    https://developer.apple.com/library/ios/#featuredarticles/FA_Wireless_Enterprise _App_Distribution/Introduction/Introduction.html
    Distributing Enterprise Apps for iOS 4 Devices

  • How do I boost the volume of a track, and a section within the track?

    Thanks for any help.
    I'm fairly new to logic and I'm trying to figure out how to boost the volume of a track. I know how to adjust it in the mix, but I have some imported audio files that still aren't loud enough overall. Is there a way to "boost" the volume of the track, so that it adds dB levels to the track overall? Not just turning the level of the track up, but actually boosting the volume of what's in the track? I turn it all the way up to 6.0, and it still isn't loud enough... I'd rather not have to double the track.
    Also, is there a way to boost the volume of certain sections of the track, besides using automation?
    Any help is greatly appreciated!!
    Chris

    First, welcome to the Apple Discussion Forums!
    Try this. Double click on the track and the Sample Editor will come up. Select the section you want to boost and select Functions -> Change Gain. But keep in mind, boosting the signal will also boost the noise.
    Hope this helps.

  • Mac Mail signatures work but appears at the bottom of a string of emails not after the email just composed. Is there a fix?

    Signatures in email work - however they appear at the bottom of the page after all of the email that appear on the page. How can I get the signatures to appear after the email msg that I just composed?

    Carolyn: Thank you for the reply, however neither deselecting nor selecting that option has any effect on the location of the signature. In both cases it will appear at the end of the string of emails.
    This is a time consuming annoyance - it would seem to be an easy thing to program that the signature lands wherever at the location of the cursor???

  • Images in the full screen lightbox are not centered horizontally on the screen. Is there a way to center the images?

    I have tried it in different screen sizes but teh result is all same. Horizontally images are slightly on the right. How can I center the images?

    Hi,
    Thanks for your answer. It is ok when using the lightbox without full screen option. When I use the fullscreen option, the container of the image is centered but the image in the container is not. I do center everything but when it comes to full scren there is nothing like centering.
    Here is what happens when I view in the browser:

  • Fim Error - The type f 'PictureURL' ( String) is not compatible with the type of 'SPS_MV_OctetString_PictureURL' (Binary)

    Hello,
    In my farm, Sycnhronization has been broken - however FIMSycnServices are started and running.
    I am getting application error with event ID 6801 and MS has suggested to recreate and application proxy in one of the support forum as below :
    https://support.microsoft.com/kb/2520394?wa=wsignin1.0
    However , I was checking the FIM tool and came across  Error by executing following steps:
    1) Open MIIS Client
    2) Click Managment Agents
    3) Ricght click Extensible Connectivity and select Properties
    When I click on Configure Attribute flow - its throwing following error message as mentioned in screen !
    Would you please let me know why this error and what is resolution ?
    Your help will be highly appreciated.
    Thanks and Regards,
    Dipti Chhatrapati

    Hi,
    Thank you for your post.
    I'm trying to involve someone familiar with this topic to further look at this issue. There might be some time delay. Appreciate your patience.
    Best Regards,
    Lisa Chen
    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact 
    [email protected]
    Lisa Chen
    TechNet Community Support

  • I keep getting a popup that says there was an error within the app store please try again later. (100) whenever i try to download logic pro from the mac app store, how can i fix this?

    Any possible solutions on how to fix this issue?

    Sometimes there are just server problems with the App Store. Only thing that I can offer is to wait a day or so and try again and, if still no luck, contact the App Store support folks - http://www.apple.com/support/mac/app-store/.
    Good luck,
    Clinton

  • Why is my macbook making the cd startup sound every few hours or within the

    first hour it's been on?

    Is there a disc in the optical drive?
    Either way, the OS will spin things up occasionally - it will 'poll' or check all drives from time to time to see if anything changed.

  • Please would you describe the mathematical routines and algorithms that are employed within the �pulse parameters� sub-routine of Labview?

    A Quick response would be much appreciated.

    Kuo,
    The method used in pulse parameters can be found on the help page for this VI. You can find this page by opening help and navigating to VI and Function Reference>>Analyze VIs>>Signal Processing VIs>>Time Domain VIs>>Pulse Parameters. Click the Details link on the top of the page for an explanation of what this VI is accomplishing.
    Regards,
    Matt F
    National Instruments
    Keep up to date on the latest PXI news at twitter.com/pxi

Maybe you are looking for