drawString rechtsbündig?

System.exit(0)

Aktives Mitglied
Hallo,

ich würde gerne Zahlen rechtsbündig ausgeben.
Dazu berechne ich die X-Koordinate mit Hilfe der stringWidth()-Methode der FontMetrics.

Leider ergibt dieser Code in Zeile 10 keine rechtsbündige Ausgabe. Die Ausgabe ist eher zentriert um den ursprünglichen Wert von tempX + 300.

Woran könnte das liegen?

Danke

System.exit(0)

Java:
        String shortText = "";
        String price = "";
        int index = 0, itemY = 0;
        for (ShopItem SI : myShopItems)
        {
            shortText = SI.getshortText();
            price = Integer.toString(SI.getPreis());
            itemY = tempY + 60 + 30 * index;
            G.drawString(shortText, tempX + 30, itemY);
            G.drawString(price, tempX + 300 - FM.stringWidth(price), itemY);
            index ++;
        }
 

Ebenius

Top Contributor
Wie holst Du denn die FontMetrics-Instanz? Wieso benutzt Du nicht die SwingUtilities-Methode zum Ermitteln der Stringbreite; das tun zumindest die üblichen UI-Delegates?

Allgemein: Java Naming Conventions in Kurzform wegen Groß-/Kleinschreibung:
  • [c]KlassenName[/c]
  • [c]variablenName[/c]
  • [c]methodenName[/c]
  • [c]COMPILE_TIME_KONSTANTEN_NAME[/c]
  • [c]packagename[/c]
Ebenius
 

System.exit(0)

Aktives Mitglied
Hallo,

weil ich diese Utilities bisher nicht kannte.
Im netz wurde hatl die stringWidth-Methode der Fontmetrics als Lösung vorgeschlagen. Aber sie funktioniert hier nicht.

Wie sieht der code für die Utilities aus?

Gruß

System.exit(0)
 

Ebenius

Top Contributor
Die Frage wie Du die FontMetrics holst ist trotzdem interessant. Wie Du die SwingUtilities-Methode (hab nachgesehen; sie heißt computStringWidth) benutzt, verrät Dir die API-Doc der Klasse javas.swing.SwingUtilities. Oder dieses Beispiel:
Java:
/* $Id$ */

/* Copyright 2010 Sebastian Haufe

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       [url]http://www.apache.org/licenses/LICENSE-2.0[/url]

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License. */

package com.ebenius;

import java.awt.BorderLayout;
import java.awt.FontMetrics;
import java.awt.Graphics;

import javax.swing.*;

public class GlyphAlignmentTestGui {

  public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {

      public void run() {
        createAndShowGui();
      }
    });
  }

  private static void createAndShowGui() {
    final JPanel contentPane = new JPanel(new BorderLayout(6, 6));
    contentPane.add(new JComponent() {

      @Override
      protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        final String text = "Huhu";
        final FontMetrics fm = g.getFontMetrics();
        g.drawString(text, getWidth()
              - getInsets().right
              - SwingUtilities.computeStringWidth(fm, text), 20);
      }
    });

    final JFrame f = new JFrame("Test Frame: GlyphAlignmentTestGui"); //$NON-NLS-1$
    f.setContentPane(contentPane);
    f.pack();
    f.setLocationRelativeTo(null);
    f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    f.setVisible(true);
  }
}
Ebenius
 

System.exit(0)

Aktives Mitglied
Hallo,

danke für die Tipps.

Das Problem war, dass ich die FontMetrics von Graphics geholt habe und danach erst den Font im Graphics-Objekt geändert habe.

GRuß

System.exit(0)
 

System.exit(0)

Aktives Mitglied
Hallo Ebenius,

schon klar.

Präziser wäre aber: "Wann holst du die FontMetrics."

Denn richtig geholt hatte ich sie ja, nur leider war sie veraltet.

Gruß

System.exit(0)
 

Ähnliche Java Themen

Neue Themen


Oben