gpt4 book ai didi

java - 根据字符串的宽度绘制动态按钮

转载 作者:行者123 更新时间:2023-11-29 05:56:45 25 4
gpt4 key购买 nike

因此,我对 Minecraft 进行了一段时间的 retrofit ,并且看到了它是如何生成按钮的。它获取一个非常宽的按钮图像,取左边的一部分,取右边的一部分,然后将两者放回一起形成一个较小的图像。

对不起,但我无法用语言很好地解释这一点,所以让我用糟糕的 Paint.Net 技能向您展示:

explanation

但是,我无法让它工作,结果是这样的: turnout

这是我的部分代码:

public class ComponentToolbarButton extends JComponent implements MouseListener {

private static final int HEIGHT = 40;

// ... other methods here

private BufferedImage getImageBasedOnWidth(Graphics g) {
BufferedImage finalImage = null;
BufferedImage rawImage = null;


// Try/catch block to initialize rawImage

// Setting font and things here

int compWidth = determineComponentWidth(g); // Returns the (should-be) width of the component

if (compWidth != getWidth()) {
setPreferredSize(new Dimension(compWidth, HEIGHT)); // Just making sure :)
}

finalImage = new BufferedImage(compWidth, HEIGHT, BufferedImage.TYPE_INT_RGB);
Graphics finalImageGraphics = finalImage.getGraphics();


// Draw left side:
finalImageGraphics.drawImage(rawImage, 0, 0, 0, compWidth / 2, 0, 0, compWidth, HEIGHT, null);

// Draw right side:
finalImageGraphics.drawImage(rawImage, compWidth / 2, 0, compWidth, HEIGHT, rawImage.getWidth() / 2 - compWidth,
0, rawImage.getWidth(), rawImage.getHeight(), null);


return finalImage;
}
}

提前致谢! (顺便说一句,我尽量缩短了代码)

最佳答案

好吧,经过一番思考,我想出了这个小技巧来演示我认为它应该如何工作:P

public class TestMenu extends JComponent {

protected static final int MAX_HEIGHT = 40;
protected static final int LEFT_MARGIN = 5;
protected static final int RIGHT_MARGIN = 5;

protected static final ImageIcon BACKGROUND = new ImageIcon(TestMenu.class.getResource("/Menu.png"));

private String text;

public TestMenu() {

setOpaque(false);
text = "This is some text";
setFont(UIManager.getFont("Label.font"));

}

@Override
public Dimension getPreferredSize() {

FontMetrics fm = getFontMetrics(getFont());
int width = fm.stringWidth(text) + LEFT_MARGIN + RIGHT_MARGIN;

Dimension size = new Dimension(width, MAX_HEIGHT);

return size;

}

@Override
protected void paintComponent(Graphics g) {

super.paintComponent(g);

int width = getWidth() - 1;
int height = getHeight() - 1;

BufferedImage imgLeft = new BufferedImage(LEFT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
BufferedImage imgRight = new BufferedImage(RIGHT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);
BufferedImage imgBody = new BufferedImage(width - LEFT_MARGIN - RIGHT_MARGIN, MAX_HEIGHT, BufferedImage.TYPE_INT_ARGB);

Graphics2D g2d = imgLeft.createGraphics();
g2d.drawImage(BACKGROUND.getImage(), 0, 0, this);
g2d.dispose();

g2d = imgRight.createGraphics();
g2d.drawImage(BACKGROUND.getImage(), RIGHT_MARGIN - BACKGROUND.getIconWidth(), 0, this);
g2d.dispose();

g2d = imgBody.createGraphics();
g2d.drawImage(BACKGROUND.getImage(), -LEFT_MARGIN, 0, this);
g2d.dispose();

g2d = (Graphics2D) g;

g2d.drawImage(imgLeft, 0, 0, this);
g2d.drawImage(imgBody, LEFT_MARGIN, 0, this);
g2d.drawImage(imgRight, width - RIGHT_MARGIN, 0, this);

FontMetrics fm = g2d.getFontMetrics();

int x = (width - fm.stringWidth(text)) / 2;
int y = ((height - fm.getHeight()) / 2) + fm.getAscent();

g2d.setColor(Color.WHITE);
g2d.drawString(text, x, y);


}

}

首先,永远不要尝试在 paint 方法中构建所有图像片段,它会减慢绘画程序并消耗大量内存,但对于这个例子,我只是想演示这个想法。请改用后备缓冲区(我认为这正是您尝试做的)。

这将产生这个:

enter image description here

关于java - 根据字符串的宽度绘制动态按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11811959/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com