我读了几个关于我的题目的答案,但我没有找到答案。我想要一个我的java代码的背景。我在这里所指的只是把图像的代码,但它不工作。
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class background extends JFrame {
private Container c;
private JPanel imagePanel;
public background() {
initialize();
}
private void initialize() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
c = getContentPane();
imagePanel = new JPanel() {
public void paint(Graphics g) {
try {
BufferedImage image = ImageIO.read(new File("http://www.signe-zodiaque.com/images/signes/balance.jpg"));
g.drawImage(image, 1000, 2000, null);
} catch (IOException e) {
e.printStackTrace();
}
}
};
imagePanel.setPreferredSize(new Dimension(640, 480));
c.add(imagePanel);
}发布于 2012-03-23 21:35:39
你在哪里找到密码的?如果从一个教程,请放弃它,因为它是教你非常坏的习惯。比如说..。
paint(...)或paintComponent(...)方法中读取图像文件(或任何文件)。首先,为什么每次重新绘制程序时都让程序在一个文件中重新读取,而您可以读取它一次并完成它。但是更重要的是,您希望您的油漆/油漆组件方法是精益的、平均的和尽可能快的,因为如果没有,并且您的绘图是缓慢的,那么用户会认为您的程序是慢的和快的。paintComponent(...)方法中进行绘图,而不是使用它的paint(...)方法。当你画画时,你会失去所有双缓冲的秋千提供的免费和你的动画将是不爽。paintComponent(...)方法。例如..。
public class ZodiacImage extends JPanel {
private static final String IMG_PATH = "http://www.signe-zodiaque.com/" +
"images/signes/balance.jpg";
private BufferedImage image;
public ZodiacImage() {
// either read in your image here using a ImageIO.read(URL)
// and place it into the image variable, or else
// create a constructor that accepts an Image parameter.
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
if (image != null) {
// draw your image here.
}
}
@Override //if you want the size to match the images
public Dimension getPreferredSize() {
if (image != null) {
return new Dimension(image.getWidth(), image.getHeight());
}
return super.getPreferredSize();
}
}https://stackoverflow.com/questions/9846732
复制相似问题