在Rectangle中创建一个字符串

您好我正在尝试在矩形内部做一个字符串来在java中制作自定义菜单我正在使用canvas并执行以下方法,但我似乎无法正确使用它!

public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); Font font = new Font("Verdana", Font.PLAIN, 20); g2d.setFont(font); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = root.getFontMetrics(font); g2d.drawString(option, (int)getX() - fm.stringWidth(option)/2, (int) getY() + fm.getHeight()); g2d.drawRect((int)getX() - fm.stringWidth(option)/2 - 20, (int) getY() - fm.getHeight() - 10, (int)getX() - fm.stringWidth(option)/2 + 40 , (int) getY() - fm.getHeight() + 10); } 

您遇到的基本问题是您正在使用已转换Graphics上下文的组件x / y位置,以便0x0是组件的左上角。

中心文字

 import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class DrawText { public static void main(String[] args) { new DrawText(); } public DrawText() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { public TestPane() { setBackground(Color.BLACK); } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g.create(); g2d.setColor(Color.RED); g2d.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2); g2d.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight()); render(g); g2d.dispose(); } public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); Font font = new Font("Verdana", Font.PLAIN, 20); g2d.setFont(font); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = g2d.getFontMetrics(); String option = "This is a test"; int x = (getWidth() - fm.stringWidth(option)) / 2; int y = ((getHeight() - fm.getHeight()) / 2); g2d.drawString(option, x, y + fm.getAscent()); g2d.drawRect( (int)x - 20, (int)y - 10, (int)fm.stringWidth(option) + 40, (int)fm.getHeight() + 20); } } } 

例如…

  • 在Panel中居中字符串
  • 矩形中的Java中心文本

更新…

如果每个菜单项都打印在一个组件中,那么上面提供的概念应该有效。 如果要在单个组件中打印多个项目,可以使用类似……

在此处输入图像描述

 public void render(Graphics g) { Graphics2D g2d = (Graphics2D) g; g2d.setColor(Color.WHITE); Font font = new Font("Verdana", Font.PLAIN, 20); g2d.setFont(font); int x = 0; int y = 0; g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); FontMetrics fm = g2d.getFontMetrics(); String option = "This is a test"; while (x < getWidth()) { while (y < getHeight()) { int width = fm.stringWidth(option); int height= fm.getHeight(); g2d.drawString(option, x + 20, y + fm.getAscent() + 10); width += 40; height += 20; g2d.drawRect( (int) x, (int) y, (int) width, (int) height); x += width; y += height; } } }