Java 在矩形内创建字符串

Java 在矩形内创建字符串,java,string,swing,canvas,Java,String,Swing,Canvas,您好,我正在尝试在一个矩形内创建一个字符串,以在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); g2d.

您好,我正在尝试在一个矩形内创建一个字符串,以在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);
    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);
}

您遇到的基本问题是使用组件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);
        }
    }

}

例如

已更新…

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

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;

        }

    }
}

public void渲染(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.WHITE);
Font Font=新字体(“Verdana”,Font.PLAIN,20);
g2d.setFont(字体);
int x=0;
int y=0;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics fm=g2d.getFontMetrics();
String option=“这是一个测试”;
而(x
您遇到的基本问题是,您正在使用组件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);
        }
    }

}

例如

已更新…

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

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;

        }

    }
}

public void渲染(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.WHITE);
Font Font=新字体(“Verdana”,Font.PLAIN,20);
g2d.setFont(字体);
int x=0;
int y=0;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics fm=g2d.getFontMetrics();
String option=“这是一个测试”;
而(x
您遇到的基本问题是,您正在使用组件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);
        }
    }

}

例如

已更新…

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

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;

        }

    }
}

public void渲染(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.WHITE);
Font Font=新字体(“Verdana”,Font.PLAIN,20);
g2d.setFont(字体);
int x=0;
int y=0;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics fm=g2d.getFontMetrics();
String option=“这是一个测试”;
而(x
您遇到的基本问题是,您正在使用组件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);
        }
    }

}

例如

已更新…

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

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;

        }

    }
}

public void渲染(图形g){
Graphics2D g2d=(Graphics2D)g;
g2d.setColor(Color.WHITE);
Font Font=新字体(“Verdana”,Font.PLAIN,20);
g2d.setFont(字体);
int x=0;
int y=0;
g2d.setRenderingHint(RenderingHints.KEY_抗锯齿,
RenderingHints.VALUE_ANTIALIAS_ON);
FontMetrics fm=g2d.getFontMetrics();
String option=“这是一个测试”;
而(x