图像不显示在简单的小程序中
我第一次使用 java 图像,在小程序加载时查看它们时遇到问题。我在下面发布的代码是我实际使用的代码的大幅精简版本,希望能够弄清楚为什么我看不到带有此代码的图像,而我必须调整窗口大小才能看到使用此代码的图像。非常感谢所有帮助,并提前致谢:)
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
}
}
I'm working with java images for the first time and having a problem viewing them when the applet loads. The code I've posted below is a dramatically pared-down version of the code I'm actually working with, hopefully figuring out why I can't see an image with this code will show me while I have to resize the window to see images with this code. All help is greatly appreciated and thanks are extended in advance :)
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
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这不是一个简单的例子,代码中仍然有很多垃圾。例如,所有图像过滤与显示图像有什么关系?所有线程代码与显示图像有什么关系?
昨天我和你花了一个小时教你绘画的基础知识之后,你却没有听我说的话。
我教了你所有关于重写paintComponent()的知识。我向您指出了 Swing 教程,其中有一个使用图像的工作示例。您的小程序的结构与教程中的示例完全不同。您的示例将非常简单,因为您不必担心动画。
你昨天不仅浪费了我的时间,而且知道你正试图浪费别人的时间。
从教程中学习并发布正确的 SSCCE。
This is NOT a simple example there is still way to much junk in the code. For example, what does all the image filtering have to do with display an image? What does all the Thread code have to do with displaying an image?
After I spent an hour with you yesterday teaching you the basics of painting you haven't listened to a thing I said.
I taught you all about overriding paintComponent(). I pointed you to the Swing tuturial which has a working example of using an image. The structure of your applet looks nothing like the example in the tutorial. Your example will be much simple since you don't have to worry about the animation.
Not only did you waste my time yesterday, but know you are attempting to waste other peoples time.
Learn from the tutorial and post a proper SSCCE.
答案是更改将 JPanel 扩展为paintComponent() 的类中的draw() 方法,并将drawImage() 调用中的最后一个参数切换为“this”而不是“null”。立即完美地工作了!
The answer is changing the draw() method in the class that extends JPanel to paintComponent() and switching the last parameter in the call to drawImage() to 'this' instead of 'null'. Worked instantly and perfectly!