如何将 JLabel 和 JTextField 放在图像之上?
我正在尝试创建一个游戏,其中有背景图像和 JLabel 形式的文本。我该怎么做呢?
我这样做的主要原因是我可以有两个不同字体大小的不同文本区域。使用 g.drawString()
只会让您对整个内容使用 1 个文本大小。
到目前为止,这是我的代码:
package com.cgp.buildtown;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Intro extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private BufferedImage bg;
private Font font;
public Intro() {
super();
loadImages();
setFont(loadFont(50f));
}
private Font loadFont(Float f) {
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("res/komikatext.ttf"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
return font.deriveFont(f);
}
private void loadImages() {
try {
bg = ImageIO.read(new File("res/introbg.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
repaint();
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(bg, 0, 0, null);
}
}
I am trying to create a game where I have a background image and text on top of that in the form of JLabel
s. How would I go about doing that?
The main reason I'm doing this is so that I can have 2 different text areas with different font sizes. Using g.drawString()
will only let you use 1 text size for the whole thing.
Here is my code so far:
package com.cgp.buildtown;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
public class Intro extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private BufferedImage bg;
private Font font;
public Intro() {
super();
loadImages();
setFont(loadFont(50f));
}
private Font loadFont(Float f) {
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("res/komikatext.ttf"));
GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
ge.registerFont(font);
} catch (FontFormatException | IOException e) {
e.printStackTrace();
}
return font.deriveFont(f);
}
private void loadImages() {
try {
bg = ImageIO.read(new File("res/introbg.png"));
} catch (IOException e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
repaint();
}
}
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(bg, 0, 0, null);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种方法。
但当然,这只是根据您自己的代码/规范破解的版本。要更好地实现相同的想法,请参阅此后台面板。
Here is one way.
But of course that is merely a version hacked out of your own code/ specs. For a much better implementation of the same idea, see this Background Panel.
首先加载背景图像,然后使其属性不透明,然后在其上添加其他组件。
first load the background image then opaque its property then add the other components on it.