我能';javaswing中的t-center文本
我正在为我的游戏制作自定义组件,我尝试了在堆栈溢出中找到的所有东西,无论发生什么,我仍然无法将文本放置在矩形的中心。我甚至阅读了使用java文本API的所有文档 有人能给我解释一下在JavaSwing中如何将文本与中心(rect或frame的中心或任何东西)一劳永逸地对齐吗?这不是一个重复的问题,因为在关于堆栈溢出的所有其他问题中,我没有得到一个有效的解决方案 到目前为止,我使用了FontMetrics,并使用stringWidth()方法测量了宽度,使用ascent测量了高度(没有一个是准确的)我能';javaswing中的t-center文本,java,swing,graphics2d,fontmetrics,Java,Swing,Graphics2d,Fontmetrics,我正在为我的游戏制作自定义组件,我尝试了在堆栈溢出中找到的所有东西,无论发生什么,我仍然无法将文本放置在矩形的中心。我甚至阅读了使用java文本API的所有文档 有人能给我解释一下在JavaSwing中如何将文本与中心(rect或frame的中心或任何东西)一劳永逸地对齐吗?这不是一个重复的问题,因为在关于堆栈溢出的所有其他问题中,我没有得到一个有效的解决方案 到目前为止,我使用了FontMetrics,并使用stringWidth()方法测量了宽度,使用ascent测量了高度(没有一个是准确的
包com.isi.ui组件;
导入java.awt.Color;
导入java.awt.Font;
导入java.awt.FontMetrics;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入java.awt.geom.Rectangle2D;
导入com.isi.core.Game;
导入com.isi.states.GameState;
公共类按钮扩展UIComponent{
专用字体;
私有字符串文本;
公共按钮(游戏、游戏状态、整数x、整数y、整数宽度、整数高度、字符串文本){
超级(游戏、状态、x、y、宽度、高度);
字体=新字体(“Arial”,font.BOLD,20);
this.text=文本;
}
公共按钮(游戏、游戏状态、整数x、整数y、整数宽度、整数高度){
超级(游戏、状态、x、y、宽度、高度);
text=null;
}
公共字符串getText(){
返回文本;
}
@凌驾
公共空白勾号(){
}
@凌驾
公众虚空抽签(图2d g){
g、 setColor(fillColor);
g、 fillRect(x,y,宽度,高度);
g、 setColor(boundsColor);
g、 绘制(边界);
如果(文本!=null){
FontMetrics fm=g.getFontMetrics();
int textX=x+(宽度/2)-(fm.stringWidth(文本)/2);
int textY=y+((height-fm.getHeight())/2)+fm.getAscent();
g、 setFont(字体);
g、 setColor(Color.white);
g、 抽绳(text,textX,textY);
}
}
}
// =============================================
包com.isi.ui组件;
导入java.awt.Color;
导入java.awt.Graphics2D;
导入java.awt.Rectangle;
导入com.isi.core.Game;
导入com.isi.states.GameState;
公共抽象类UIComponent{
公共最终静态颜色默认值\u界限\u颜色=Color.white;
公共最终静态颜色默认值\填充\颜色=Color.gray;
受保护的游戏;
受保护的配子状态;
受保护的int x;
保护智力;
保护整数宽度;
保护内部高度;
保护矩形边界;
保护色底色;
保护色;
公共UIComponent(游戏、游戏状态、整数x、整数y、整数宽度、整数高度){
这个游戏=游戏;
this.state=状态;
这个.x=x;
这个。y=y;
这个。宽度=宽度;
高度=高度;
边界=新矩形(x、y、宽度、高度);
boundsColor=默认的\u边界\u颜色;
fillColor=默认的填充颜色;
}
公共int getX(){
返回x;
}
公共无效集合x(整数x){
这个.x=x;
}
公共int getY(){
返回y;
}
公共空间设置(整数y){
这个。y=y;
}
公共int getWidth(){
返回宽度;
}
公共void setWidth(int-width){
这个。宽度=宽度;
}
公共整数getHeight(){
返回高度;
}
公共空间设置高度(内部高度){
高度=高度;
}
公共矩形getBounds(){
返回边界;
}
公共空心收进边界(矩形边界){
this.bounds=bounds;
}
公共颜色getBoundsColor(){
返回边界颜色;
}
公共空白setBoundsColor(颜色边界颜色){
this.boundsColor=boundsColor;
}
公共颜色getFillColor(){
返回填充颜色;
}
公共void setFillColor(颜色fillColor){
this.fillColor=fillColor;
}
公共摘要无效勾号();
公开摘要作废图(图2d g);
}
// =============================================
包com.isi.states;
导入java.awt.Graphics2D;
导入java.awt.image.buffereImage;
导入java.util.ArrayList;
导入com.isi.core.Game;
导入com.isi.tools.ImageLoader;
导入com.isi.uicomponents.Button;
导入com.isi.uicomponents.UIComponent;
公共类MainMenuState扩展游戏状态{
私有静态缓冲区dimage bg=ImageLoader.load(“主菜单Background.jpg”);
//动画背景坐标
私人INTX;
私营企业;
//主菜单组件阵列
私有ArrayList组件;
公共主自定义(游戏){
超级(游戏);
x=0;
y=0;
components=新的ArrayList();
添加(新按钮(game,this,game.getWidth()/2-80/2,game.getHeight()/2-50/2,80,50,“Play”);
}
公共ArrayList getComponents(){
返回组件;
}
公共无效勾号(){
y=y>=game.getHeight()?0:y+2;
}
公共作废图纸(图形2D g){
g、 drawImage(bg,0,-game.getHeight()+y,game.getWidth(),game.getHeight(),null);
g、 drawImage(背景,x,y,game.getWidth(),game.getHeight(),null);
对于(int i=0;i
这很接近,您需要将x的宽度增加一半,然后将字符串宽度减少一半。您还需要在获取字体度量之前设置字体,否则将获取现有图形字体的度量
g.setFont(font);
FontMetrics fm=g.getFontMetrics();
int textX=x+(宽度/2)-(fm.stringWidth(文本)/2);
int textY=
package com.isi.uicomponents;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Rectangle2D;
import com.isi.core.Game;
import com.isi.states.GameState;
public class Button extends UIComponent {
private Font font;
private String text;
public Button(Game game, GameState state, int x, int y, int width, int height, String text) {
super(game, state, x, y, width, height);
font = new Font("Arial", Font.BOLD, 20);
this.text = text;
}
public Button(Game game, GameState state, int x, int y, int width, int height) {
super(game, state, x, y, width, height);
text = null;
}
public String getText() {
return text;
}
@Override
public void tick() {
}
@Override
public void draw(Graphics2D g) {
g.setColor(fillColor);
g.fillRect(x, y, width, height);
g.setColor(boundsColor);
g.draw(bounds);
if (text != null) {
FontMetrics fm = g.getFontMetrics();
int textX = x + (width / 2) - (fm.stringWidth(text) / 2);
int textY = y + ((height - fm.getHeight()) / 2) + fm.getAscent();
g.setFont(font);
g.setColor(Color.white);
g.drawString(text, textX, textY);
}
}
}
// =============================================
package com.isi.uicomponents;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import com.isi.core.Game;
import com.isi.states.GameState;
public abstract class UIComponent {
public final static Color DEFAULT_BOUNDS_COLOR = Color.white;
public final static Color DEFAULT_FILL_COLOR = Color.gray;
protected Game game;
protected GameState state;
protected int x;
protected int y;
protected int width;
protected int height;
protected Rectangle bounds;
protected Color boundsColor;
protected Color fillColor;
public UIComponent(Game game, GameState state, int x, int y, int width, int height) {
this.game = game;
this.state = state;
this.x = x;
this.y = y;
this.width = width;
this.height = height;
bounds = new Rectangle(x, y, width, height);
boundsColor = DEFAULT_BOUNDS_COLOR;
fillColor = DEFAULT_FILL_COLOR;
}
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getWidth() {
return width;
}
public void setWidth(int width) {
this.width = width;
}
public int getHeight() {
return height;
}
public void setHeight(int height) {
this.height = height;
}
public Rectangle getBounds() {
return bounds;
}
public void setBounds(Rectangle bounds) {
this.bounds = bounds;
}
public Color getBoundsColor() {
return boundsColor;
}
public void setBoundsColor(Color boundsColor) {
this.boundsColor = boundsColor;
}
public Color getFillColor() {
return fillColor;
}
public void setFillColor(Color fillColor) {
this.fillColor = fillColor;
}
public abstract void tick();
public abstract void draw(Graphics2D g);
}
// =============================================
package com.isi.states;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.util.ArrayList;
import com.isi.core.Game;
import com.isi.tools.ImageLoader;
import com.isi.uicomponents.Button;
import com.isi.uicomponents.UIComponent;
public class MainMenuState extends GameState {
private static BufferedImage bg = ImageLoader.load("Main Menu Background.jpg");
// Background coordinates for animation
private int x;
private int y;
// MainMenu components array
private ArrayList<UIComponent> components;
public MainMenuState(Game game) {
super(game);
x = 0;
y = 0;
components = new ArrayList<UIComponent>();
components.add(new Button(game, this, game.getWidth() / 2 - 80 / 2, game.getHeight() / 2 - 50 / 2, 80, 50, "Play"));
}
public ArrayList<UIComponent> getComponents() {
return components;
}
public void tick() {
y = y >= game.getHeight() ? 0 : y + 2;
}
public void draw(Graphics2D g) {
g.drawImage(bg, 0, -game.getHeight() + y, game.getWidth(), game.getHeight(), null);
g.drawImage(bg, x, y, game.getWidth(), game.getHeight(), null);
for (int i = 0; i < components.size(); i++) {
components.get(i).draw(g);
}
}
}