如何在JFrame中将图像设置为背景
How to set an image as background in JFrame
我不知道如何解决这个问题。请帮我。谢谢。
代码如下:
package com.gabriel.mvc.app;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import com.gabriel.mvc.shapeimpl.CarRenderer;
import com.gabriel.mvc.shapesfx.MyShape;
import com.gabriel.mvc.shapesfx.Renderer;
public class CarView extends JFrame {
Image image;
MyShape car;
Renderer renderer = new CarRenderer();
void init(MyShape car) {
this.car = car;
}
void initUI() {
setVisible(true);
image = new ImageIcon("2284910.jpg").getImage();
setBounds(300,100,500,500);
setSize(500,500);
}
public void paint(Graphics g)
{
g.clearRect(0,0,600,600);
g.drawImage(image, 0, 0, null);
renderer.draw(g, car);
}
}
这是输出,只有纯色。图像未设置为背景。 :(
这是我的预期输出,我在 Photoshop 上对其进行了编辑。
您可以参考此代码,只需替换您的图像源并在 Graphics 的 drawImage 方法之后添加您的 CarRenderer。
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import java.awt.Image;
import java.awt.Graphics;
public class SampleImageBg extends JFrame{
private JDesktopPane panel;
public SampleImageBg() {
panel = new javax.swing.JDesktopPane() {
private Image image;
{
try {
image = ImageIO.read(getClass().getResource("bg.jpeg"));// source of your background image
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.clearRect(50,0,300,300); // add your renderer here instead
}
};
add(panel);
setVisible(true);
setSize(400, 600);
}
public static void main(String[] args) {
new SampleImageBg();
}
}
我不知道如何解决这个问题。请帮我。谢谢。
代码如下:
package com.gabriel.mvc.app;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import com.gabriel.mvc.shapeimpl.CarRenderer;
import com.gabriel.mvc.shapesfx.MyShape;
import com.gabriel.mvc.shapesfx.Renderer;
public class CarView extends JFrame {
Image image;
MyShape car;
Renderer renderer = new CarRenderer();
void init(MyShape car) {
this.car = car;
}
void initUI() {
setVisible(true);
image = new ImageIcon("2284910.jpg").getImage();
setBounds(300,100,500,500);
setSize(500,500);
}
public void paint(Graphics g)
{
g.clearRect(0,0,600,600);
g.drawImage(image, 0, 0, null);
renderer.draw(g, car);
}
}
这是输出,只有纯色。图像未设置为背景。 :(
这是我的预期输出,我在 Photoshop 上对其进行了编辑。
您可以参考此代码,只需替换您的图像源并在 Graphics 的 drawImage 方法之后添加您的 CarRenderer。
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import java.awt.Image;
import java.awt.Graphics;
public class SampleImageBg extends JFrame{
private JDesktopPane panel;
public SampleImageBg() {
panel = new javax.swing.JDesktopPane() {
private Image image;
{
try {
image = ImageIO.read(getClass().getResource("bg.jpeg"));// source of your background image
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
g.clearRect(50,0,300,300); // add your renderer here instead
}
};
add(panel);
setVisible(true);
setSize(400, 600);
}
public static void main(String[] args) {
new SampleImageBg();
}
}