本文介绍了如何将JLabel和JTextField放在图像之上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个游戏,在其中我有一个背景图像和以JLabel
s形式出现的文本.我将如何去做?
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?
我这样做的主要原因是,我可以拥有2个具有不同字体大小的不同文本区域.使用g.drawString()
只能使整个内容使用1个文本大小.
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);
}
}
推荐答案
这是一种方法.
但是,当然,这仅仅是出于您自己的代码/规范而被黑客入侵的版本.要更好地实现相同的想法,请参见此背景面板.
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.
import java.awt.*;
import java.awt.image.BufferedImage;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import java.net.URL;
import javax.imageio.ImageIO;
public class Intro extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
private Thread thread;
private BufferedImage bg;
private String html =
"<html><body style='color: yellow;'>" +
"<h1>Game</h1>" +
"<p>Welcome to the Game!";
public Intro() {
super();
loadImages();
setLayout(new BorderLayout());
setBorder(new EmptyBorder(40,40,40,40));
add(new JLabel(html), BorderLayout.NORTH);
add(new JTextField("..enter name"), BorderLayout.SOUTH);
}
private void loadImages() {
try {
URL url = new URL("http://pscode.org/media/stromlo2.jpg");
bg = ImageIO.read(url);
setPreferredSize(new Dimension(bg.getWidth(), bg.getHeight()));
} catch (Exception e) {
e.printStackTrace();
}
}
public void addNotify() {
super.addNotify();
thread = new Thread(this);
thread.start();
}
public void run() {
while(true) {
repaint();
}
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g.drawImage(bg, 0, 0, this);
}
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, new Intro());
}
}
这篇关于如何将JLabel和JTextField放在图像之上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!