Issue
// Eine einfache grafische Benutzeroberfläche mit Swing
//von der Zeile 3 bis zur Zeile 5; Impotieren von Bibliothek
import java.awt.*;
import javax.swing.*;
import java.awt.Graphics;
public class HelloGUI extends JFrame { // public class HelloGui
public HelloGUI (String title) {
super(title);
getContentPane().add("North",new JButton("Hello World"));
setSize(400,400);
setVisible(true);
setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}
public void paint (Graphics g) {
/* Das Verwenden von pain Methode
* Graphics ist ein Parameter
* in g ist das Parameter Graphics gespeichert
*/
String insideRect = "Hello World!";
//this String should be displayed inside of the rectangel
int x = 100;
int y = 100;
g.drawRect(100,100,200,200);
g.setColor(Color.RED);
g.drawString(insideRect, x,y);
}
public static void main(String args[]) {
new HelloGUI("Hello World ");
}
}
0 Antworten
I want to write a simpl java gui App, which display the Strin "Hello world", but it is very important to me that "Hello world" must be inside a rectangle.
so used the method g.drawRect()
to draw a rectangle and the method g.drawString()
to display the String "Hello World!" or any other message.
The Code work , but Hello World is not displayed iside the reactangle.
Can anybody help me to display the string "Hello Wolrd!"inside the rectangle... I try it but it didn't work. that is my code! tnx
so here is a screenshot of my result
[enter image description here][1] [1]: https://i.stack.imgur.com/daWM5.jpg
Solution
Your String
is actually inside the rectangle, in the upper left corner. However it doesn't take the font size into consideration. The code below should put the text within the borders of the rectangle:
public void paint (Graphics g) {
/* Das Verwenden von pain Methode
* Graphics ist ein Parameter
* in g ist das Parameter Graphics gespeichert
*/
String insideRect = "Hello World!";
//this String should be displayed inside of the rectangel
int x = 100;
int y = 100;
g.drawRect(100,100,200,200);
g.setColor(Color.RED);
// add an offset to y coordinate
Font font = g.getFont();
g.drawString(insideRect, x ,y + font.getSize());
}
Edit: To center the text you should play with the height and width of you rectangle & text, changing the texts x
and y
coordinates. Below I created widthOffset
and heightOffset
based on the width/height of the rectangle & text:
public void paint (Graphics g) {
/* Das Verwenden von pain Methode
* Graphics ist ein Parameter
* in g ist das Parameter Graphics gespeichert
*/
String insideRect = "Hello World!";
//this String should be displayed inside of the rectangel
int x = 100;
int y = 100;
g.drawRect(100,100,200,200);
g.setColor(Color.RED);
// center the text inside the rectangle
Font font = g.getFont();
FontRenderContext frc = new FontRenderContext(new AffineTransform(), true, true);
Rectangle2D textRectangle = font.getStringBounds(insideRect, frc);
int textWidth = (int) textRectangle.getWidth();
int textHeight = (int) textRectangle.getHeight();
int widthOffset = 100 - textWidth/2; // 100 = rectangle width / 2
int heightOffset = 100 + textHeight/2; // 100 = rectangle height / 2
g.drawString(insideRect, x + widthOffset ,y + heightOffset);
}
To run the snippet above you should also import:
import java.awt.font.FontRenderContext;
import java.awt.geom.AffineTransform;
import java.awt.geom.Rectangle2D;
Answered By - happy songs
Answer Checked By - Mary Flores (JavaFixing Volunteer)