在java小程序中查看图像需要调整大小

在java小程序中查看图像需要调整大小,java,image,icons,Java,Image,Icons,我第一次使用java图像,在加载小程序时查看它们时遇到问题。如果我调整窗口的大小,它们会显示得很好。我觉得这是一个常见的第一次出错。还有其他人遇到过这种情况吗?你知道怎么解决吗?下面列出了我认为与代码相关的部分。谢谢你在这方面的帮助 import java.applet.*; import java.awt.*; import java.awt.event.*; import java.awt.image.*; import javax.swing.*; import java.util.*;

我第一次使用java图像,在加载小程序时查看它们时遇到问题。如果我调整窗口的大小,它们会显示得很好。我觉得这是一个常见的第一次出错。还有其他人遇到过这种情况吗?你知道怎么解决吗?下面列出了我认为与代码相关的部分。谢谢你在这方面的帮助

import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import java.util.*;


public class example extends JApplet implements Runnable
{


boolean updating;
Thread thread;
private int width, height;

Table 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
    //add cordTxtFlds to bottom of screen
        //this.add(cordTxtFlds, BorderLayout.SOUTH);
        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();

      //show the greeting
      ImageIcon icon = new ImageIcon("images/FLY.gif",
                             "a fly");

      repaint();
      validate();
}

/** Description of paint(Graphics g)
 *
 * Function draws table and sets the table color
 * @param g graphics object used to draw table
 * @return  void
 */
public void paint(Graphics g)
{
    GameGraphics.clearRect(0, 0, getWidth(), getHeight());

    aTable.draw(GameGraphics);
    g.drawImage(GameImage, 0, 0, this);

}

public void update(Graphics g)
{
    paint(g);
    validate();
}


public void start()
{
    thread = new Thread(this);
    thread.start();
}

public void stop()
{
    updating = false;
}

public void run()
{
    while(updating)
    {
        aTable.update();
}

}

//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;
}


}
以及处理图形的类(在drawValues()中)

import java.awt.*;
导入java.util.Random;
公共类表扩展面板
{
private char[][]values=new char[10][10];//可能最好使用整数值数组(0或1)
专用点[]coordLoc;//=新点[100];//存储网格上点的x&y坐标
私有布尔值[]itemMarker;//存储项的真值
//位于cordLoc阵列中的共同响应点
私有整数[][]坐标;//=新整数[100][2];
Image itemImg;//存储项目图像
private int Rows;//存储行数
private int Columns;//存储列数
private int-BoxWidth;//存储框的宽度
private int-BoxHeight;//存储长方体的高度
公共点Pos=new Point();//创建要从中绘制的新点
private int tableHeight;//存储表的高度
private int tableWidth;//存储表的宽度
私有国际货币基金组织;
/**公共表的说明(x,y,宽度,高度,列,行,x,O)
*
*构造函数
*@param x包含表格的x坐标
*@param y包含表格的y坐标
*@param width包含表中框的宽度
*@param height包含表中框的高度
*@param col包含表中的列数
*@param rows包含表中的行数
*@param itemImg包含“目标”图像,即:蚂蚁、苍蝇、独角兽
*@returnnone
*/
公共表(整数x、整数y、整数宽度、整数高度、整数列、整数行、图像项img、图像样本)
{
/*设定值*/
numOfGridLocs=(列-1)*(行-1);
//初始化数组
coordLoc=新点[numofridlocs];
对于(int i=0;i(0-((行)/2));greaterIndex--){
对于(int minorIndex=0;minorIndex<(行-1);minorIndex++){
索引=x_轴开始索引+最小索引;
coords[指数][1]=更大的指数;
}
x_axisBeginingIndex+=(行-1);
}
//打印出x坐标和y坐标
对于(int i=0;ix=“+coords[i][0]+”y=“+coords[i][1]);
}
}
公共布尔thereIsAnItemAt(int索引){
返回itemMarker[索引];
}
公共布尔bugsLeft(){
布尔therearbugsleft=false;
对于(int i=0;i
这件事已经困扰了好几个星期了。再次感谢

我认为是恰当的 下面列出了部分代码

根据定义,当您遇到问题时,您不知道代码的哪一部分是(或不是)相关的。这就是为什么你需要发布一个演示问题的帖子,这样我们就可以看到你在做什么

事实上,调整大小后的工作意味着问题不在于绘画。问题可能是没有加载图像,在这种情况下,您应该使用:

drawImage(...., this);
当图像完全加载时,“this”而不是“null”通知面板重新绘制图像

或者,您在框架可见后将面板添加到框架中,但忘记使用

panel.revalidate(). 
通过调整帧大小,可以强制重新验证


关键是我们在猜测。因此,请节省我们的时间,并在下次发布SSCCE。

对不起,代码太多了

你需要读一读这篇文章。您的代码是两者的混合体

基本上,正如您在上一篇文章中所说的,定制绘制是通过重写JPanel的paintComponent(…)方法来完成的。因此,您可以进行自定义绘制,然后将JPanel添加到JApplet。在上一篇文章中,我给了你一个Swing教程的链接,其中还包含一个关于如何编写小程序的部分

您应该扩展JPanel,而不是Panel

另外,您不应该重写JApplet的paint()和upated()方法,这是旧的AWT代码,不应该与Swing一起使用


阅读本文并首先解决问题。

答案是更改类中的draw()方法,该类将JPanel扩展为paintComponent(),并切换最后一个参数
panel.revalidate().