我正在首次使用java图像,并且在小程序加载时看到它们时出现问题。我在下面发布的代码是一个大幅缩减版本的代码,我实际上正在处理,希望能够弄清楚为什么我无法看到具有此代码的图像会显示给我,而我必须调整窗口大小以查看图像与this code。所有帮助是极大的赞赏和感谢事先扩展:)图像无法在简单的小程序中显示
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;
import java.awt.Graphics;
public class example extends JApplet implements Runnable
{
boolean updating;
Thread thread;
private int width, height;
TestImageDraw aTable; //used to create and store values
private AudioClip[] sounds = new AudioClip[4]; //array to hold audio clips
private int counter = 0; //counter for audio clip array
private Image GameImage;
private Graphics GameGraphics;
public example() //set up applet gui
{
this.resize(new Dimension(600, 500));
//setup table
//aTable = new Table(50, 50, 50, 50, 16, 16, getImage("images/FLY.gif", Color.white),
//getImage("images/FlySwatter.gif", Color.white)); //Table must be square or flyswatter wont move straight
aTable = new TestImageDraw(getImage("images/FLY.gif", Color.white));
//this.add(aTable);
super.resize(800, 600);
repaint();
}
public void init()
{
width = getSize().width;
height = getSize().height;
GameImage = createImage(width, height);
GameGraphics = GameImage.getGraphics();
// Automatic in some systems, not in others
GameGraphics.setColor(Color.black);
repaint();
validate();
}
public void start()
{
thread = new Thread(this);
thread.start();
}
public void stop()
{
updating = false;
}
public void run()
{
while(updating)
{
//aTable.update();
}
aTable.revalidate();
}
//returns a transparent image.
//color is made transparent
private Image getImage(String imgPath, final Color color)
{
Image img = Toolkit.getDefaultToolkit().getImage(imgPath);
ImageFilter filter = new RGBImageFilter() {
// the color we are looking for... Alpha bits are set to opaque
public int markerRGB = color.getRGB() | 0xFFFFFF;
public final int filterRGB(int x, int y, int rgb) {
if ((rgb | 0xFF000000) == markerRGB) {
// Mark the alpha bits as zero - transparent
return 0x00FFFFFF & rgb;
}
else {
// nothing to do
return rgb;
}
}
};
ImageProducer ip = new FilteredImageSource(img.getSource(), filter);
img = Toolkit.getDefaultToolkit().createImage(ip);
return img;
}
}
TestImageDraw.java
import java.awt.*;
import java.util.Random;
import javax.swing.*;
public class TestImageDraw extends JPanel
{
Image itemImg; // stores the item image
public TestImageDraw(Image itemImg)
{
this.itemImg = itemImg;
}
/** Description of draw(Graphics g)
*
* Function draws the lines used in the table
* @param g object used to draw the table
* @return none
*/
public void draw(Graphics g)
{
Graphics2D g2=(Graphics2D)g;
//draw flyswatter
drawValues(g2); //draw values
}
private void drawValues(Graphics g)
{
g.drawImage(itemImg,20,140,30,40, null);
g.setColor(Color.black); // set color of table to black
}
}
显然你没有教我所有关于重写paintComponent(),因为我还在问问题。我不明白什么可以从代码中删除,所以我留下了我认为可能很重要的功能。在我拿出你在我们上次谈话中推荐的所有东西之后,该程序没有绘制任何东西,所以我不知道发生了什么。我已经阅读了swing教程,当我尝试编译示例代码时,我得到了“使用或覆盖已弃用的API”错误,因此在那里没有多大帮助。我感谢你试图帮助我,但没有人强迫你“浪费”你的时间。 – danwoods 2009-12-07 06:01:57
我的印象是,像这样的代码恰好就是当你说“创建一个简单的自定义JPanel,它具有简单的drawstring(...)并将其添加到JApplet并让它首先工作时”。 (除了我需要画一个图像,而不是一个字符串,我不能得到它的工作,这就是为什么我问问题)... – danwoods 2009-12-07 06:04:13
你了解了paintComponent你的帖子:http:// stackoverflow。 COM /问题/ 1830603 /绘图文本中之JPanel的。我还第二次向您介绍了定制绘画的Swing教程。同样,这里的代码看起来与“如何制作小程序”教程中的示例代码完全不同。你声称这个代码的问题是它不会显示图像,它甚至不会使用drawString(),所以你也忽略了这个建议。 – camickr 2009-12-07 06:18:10