2009-12-05 22 views
0

我正在第一次使用java图像,并且在applet加载时查看它们时遇到问题。如果我调整窗口大小,它们显示正常。我觉得这是一个常见的第一次定时器错误。有人遇到过这种情况么?任何想法是什么修复可能是?下面列出了我认为是代码的相关部分。感谢您的任何和这一切的帮助...在java applet中查看图像所需的调整大小

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.*; 
import java.util.Random; 



public class Table extends Panel 
{ 


private char[][]values = new char[10][10]; //probably better to use array of integer values(0 or 1) 
private Point[]coordLoc;// = new Point[100]; //stores the x & y coordinates of points on the grid 
private boolean[]itemMarker; //stores the truth value of wether or not an item 
          // is located at the coresponding point in cordLoc array 
    private int [][]coords;// = new int [100][2]; 
Image itemImg; // stores the item image 
private int Rows; // stores number of rows 
private int Columns; // stores number of columns 
private int BoxWidth ; // stores the width of a box 
private int BoxHeight; // stores the height of a box 
public Point Pos = new Point(); // creates a new point to draw from 

private int tableHeight; // stores the height of the table 
private int tableWidth;  // stores the width of the table 

private int numOfGridLocs; 


/** Description of public Table(x, y, width, height, col, rows, X, O) 
* 
* Constructor function 
* @param x contains an x-coordinate of the table 
* @param y contains a y-coordinate of the table 
* @param width contains the width of a box in the table 
* @param height contains the height of a box in the table 
* @param col contains the number of columns in the table 
* @param rows contains the number of rows in the table 
* @param itemImg contains the "target" image ie: ant, fly, ... unicorn 
* @return none 
*/ 
public Table(int x, int y, int width, int height, int col, int rows, Image itemImg, Image swatterImg) 
{ 
    /*set values*/ 
numOfGridLocs = (col - 1) * (rows - 1); 
    //initialize arrays 
coordLoc = new Point[numOfGridLocs]; 
    for(int i = 0; i < numOfGridLocs; i++) 
    coordLoc[i] = new Point(); 

    Rows = rows; 
    Columns = col; 
    BoxWidth = width; 
    BoxHeight = height; 
    Pos.x = x; 
    Pos.y = y; 
    this.itemImg = itemImg; 
    tableHeight = Rows*BoxHeight; 
    tableWidth = Columns*BoxWidth; 
    itemMarker = new boolean[numOfGridLocs]; 
    coords = new int [numOfGridLocs][2]; 
    this.setValues(); 
    mapGrid(); 
} 


/** 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 

    //draw vertical table lines 
    for (int i = 0 ; i <= Columns ; i++) 
    { 
     //make center line thicker 
     if(i == Rows/2) 
      g2.setStroke(new BasicStroke(2)); 
     else 
      g2.setStroke(new BasicStroke(1)); 

    g2.drawLine(i*BoxWidth + Pos.x, Pos.y, i*BoxWidth + Pos.x, tableHeight+Pos.y); 
} 

    //draw horizontal table line 
    for(int i = 0 ; i <= Rows ; i++) 
    { 
     //make center line thicker 
     if(i == Rows/2) 
      g2.setStroke(new BasicStroke(2)); 
     else 
      g2.setStroke(new BasicStroke(1)); 

    g2.drawLine(Pos.x, i*BoxHeight + Pos.y, tableWidth+Pos.x, i*BoxHeight + Pos.y); 
    } 

    drawLables(g); 





} 
/** Description of drawLables(Graphics g) 
* 
* Function draws the Lables of the Table 
* @param g object used to draw the table 
* @return none 
*/ 
private void drawLables(Graphics g) 
{ 
    String Lable; 
    Graphics2D g2 = (Graphics2D)g; 

    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, 
     RenderingHints.VALUE_ANTIALIAS_ON); 
    Font font = new Font("Serif", Font.PLAIN, 10); 
    g2.setFont(font); 

    int xLabel = this.Columns/2 * -1; 
    int yLabel = this.Rows/2; 

    //draw Row lables 
    for (int i = 0 ; i <= Rows ; i++) 
    { 
     Lable = "" + yLabel; 
     g2.drawString(Lable, Pos.x - 25, Pos.y + BoxHeight*i); 
     yLabel--; 
    } 

    //draw Column lables 
    for (int i = 0 ; i <= Columns ; i++) 
    { 
     Lable = "" + xLabel; 
     g2.drawString(Lable, Pos.x + BoxWidth*i - 5, Pos.y - 20); 
     xLabel++; 
    } 
} 

/** Description of randomChangeFunc() 
* 
* Function randomly determines which table value to change 
* @param none 
* @return void 
*/ 
public int getX(int XCordinate) 
{ 
    int x = XCordinate+Columns/2; 
    if(x < 0) x *= -1;  //x must be positive 
    x *= BoxWidth; 
    x += Pos.x; 
    return x-BoxWidth/2; 
} 

//returns Position of Y-cordinate 
public int getY(int YCordinate) 
{ 
    int y = YCordinate -Rows/2; 
    if (y < 0) y *= -1;  //y must be positive 
    y *= BoxHeight; 
    y += Pos.y; 
    return y-BoxHeight/2; 
} 


/** Description of getValue(col, row) 
* 
* Function draws the lines used in the table 
* @param col contains a column coordinate 
* @param row contains a row coordinate 
* @return returns table coordinates 
*/ 
public char getValue(int col, int row) 
{ 
    return values[row][col]; 
} 

/** Description of isDrawable(x, y) 
* 
* Function returns true if (x,y) is a point in the table 
* @param x contains a table column 
* @param y contains a table row 
* @return boolean if (x,y) is a point in the table 
*/ 
public boolean isDrawable(int x, int y) 
{ 
    if((this.getRow(y)!=-1)||(this.getColumn(x)!=-1)) 
     return true; 
    else 
     return false; 
} 

    private void drawValues(Graphics g) 
{ 
     for(int i = 0; i < numOfGridLocs; i++) 
     if(itemMarker[i]) 
      g.drawImage(itemImg,coordLoc[i].x+1, coordLoc[i].y+1, BoxWidth-1, BoxHeight-1, null); 

     g.setColor(Color.black); // set color of table to black 
} 

//sets the randomized boolean values in itemMarker array 
private void setValues() 
{ 
    double probOfItem = .25; 

    for(int count = 0; count < numOfGridLocs; count++){ 
    itemMarker[count] = randomBool(probOfItem); 
    if(itemMarker[count]) 
     System.out.println("true"); 
    else 
    System.out.println("false"); 
} 
} 

    //returns random boolean value, p is prob of 'true' 
    private boolean randomBool(double p) 
    { 
    return (Math.random() < p); 
    } 

public int getColumn(int x) 
{ 
    x += (BoxWidth/2); //aTable.getX/Y returns in the middle of squares not at upper left point 
    int offsetx=0; 
    for (int i = 0 ; i < Columns*2 ; i++) 
    { 
     offsetx = i*BoxWidth; 
     if((x>=Pos.x+offsetx)&& (x<Pos.x+offsetx+BoxWidth)) 
      return i-Columns; 
    } 
    return -100; 
} 

public int getRow(int y) 
{ 
    int offsety=0; 
    y += (BoxHeight/2); //aTable.getX/Y returns in the middle of squares not at upper left point 
    for (int i = 0 ; i < Rows*2 ; i++) { 
     offsety = i * BoxHeight; 
     if((y >= (offsety+Pos.y))&& (y < (offsety+BoxHeight+Pos.y))) 
     { 
      return ((i)*-1)+Rows; 
     } 
    } 
    return -100; 
} 

public boolean isValidGuess(int x, int y) 
{ 
    if ((x > Columns/2) || (x < Columns/2*-1) || (y > Rows/2) || (y < Rows/2*-1)) 
     return false; 

    return true; 
} 

/** Description of randomChangeFunc() 
* 
* Function randomly determines which table value to change 
* @param none 
* @return void 
*/ 
public void randomChangeFunc() 
{ 

    //get random row and column 
    Random rand=new Random(); 

     int randRow = rand.nextInt(Rows); // gets and holds a random column 
     int randCol = rand.nextInt(Columns); // gets and holds a random column 

     System.out.println("randRow = " + randRow + " randCol = " + randCol); 

    if(values[randRow][randCol] == 'X') 
     values[randRow][randCol] = 'O'; 
    else if(values[randRow][randCol] == 'O') 
     values[randRow][randCol] = 'X'; 
    else 
     System.out.println("ERROR SWAPPING SQUARE VALUE"); // error message 

} 


    private void mapGrid() //set values for coordLoc array 
{ 

    //set counter variables 
    int count = 0; 
    int index = 0; 

    //loop through all points, assigning them to the coordLoc array 
    for (int r=0; r < Rows-1; r++) 
     for (int c=0; c < Columns-1; c++) { 
     //the width/height/2 places the points on grid line intersections, not the boxes they create 
      coordLoc[count].x = Pos.x + (BoxWidth) * c + (BoxWidth/2); // record x-point 
      coordLoc[count].y = Pos.y + (BoxHeight) * r + (BoxHeight/2); // record y-point 
      System.out.println(coordLoc[count].getX() + ", " + coordLoc[count].getY()); 
      count++; 

    } //end inner for 

//set positive x coord values for coords array 
int y_axisBeginingIndex = (Rows - 2)/2; 
for(int greaterIndex = 0; greaterIndex < ((Rows)/2); greaterIndex++){ 
    for(int minorIndex = 0; minorIndex < (Rows - 1); minorIndex++){ 
    index = y_axisBeginingIndex + greaterIndex + ((Rows - 1) * minorIndex); 
    coords[index][0] = greaterIndex; 
    } 
} 

//set negative x coord values for coords array 
for(int greaterIndex = -1; greaterIndex > (0-((Rows)/2)); greaterIndex--){ 
    for(int minorIndex = 0; minorIndex < (Rows - 1); minorIndex++){ 
    index = y_axisBeginingIndex + greaterIndex + ((Rows - 1) * minorIndex); 
    coords[index][0] = greaterIndex; 
    } 
} 

//set positive y values for coords array 
int x_axisBeginingIndex = (Rows - 1) * ((Rows/2) - 1); 
for(int greaterIndex = 0; greaterIndex < ((Rows)/2); greaterIndex++){ 
    for(int minorIndex = 0; minorIndex < (Rows - 1); minorIndex++){ 
    index = x_axisBeginingIndex + minorIndex; 
    coords[index][1] = greaterIndex; 
    } 
    x_axisBeginingIndex -= (Rows - 1); 
} 

//set negative y values for coords array 
x_axisBeginingIndex = (Rows - 1) * ((Rows/2) - 1) + (Rows - 1); 
for(int greaterIndex = -1; greaterIndex > (0-((Rows)/2)); greaterIndex--){ 
    for(int minorIndex = 0; minorIndex < (Rows - 1); minorIndex++){ 
    index = x_axisBeginingIndex + minorIndex; 
    coords[index][1] = greaterIndex; 
    } 
    x_axisBeginingIndex += (Rows - 1); 
} 

    //print out the x and y coords 
for(int i = 0; i < numOfGridLocs; i++){ 
    System.out.println("[" + i + "] -> x = " + coords[i][0] + " y = " + coords[i][1]); 
} 

} 

public boolean thereIsAnItemAt(int index){ 
    return itemMarker[index]; 
} 


public boolean bugsLeft(){ 
    boolean thereAreBugsLeft = false; 

    for(int i = 0; i < numOfGridLocs; i++) 
    if(itemMarker[i]) 
     thereAreBugsLeft = true; 

    return thereAreBugsLeft; 
} 



void update() 
{ 

    this.repaint(); 

} 


} 

被难倒这几个星期。再次感谢...

回答

0

答案改变了将JPanel扩展到paintComponent()并将调用中的最后一个参数切换为drawImage()为'this'而不是'null'的类中的draw()方法。立即完美工作!

1

我相信是相关的 部分代码如下所列。

根据定义,当你有问题时,你不知道哪部分代码是(或不是)相关的。这就是为什么你需要发布SSCCE来证明问题,所以我们可以看到你在做什么。

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

drawImage(...., this); 

的“本”,而不是“空”通知面板重新绘制图像时,图像变得满载。

或者,也许,你添加的面板到帧的帧是可见的,忘了使用

panel.revalidate(). 

通过调整框架您强制再验证之后。

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

+0

谢谢。在它上面工作。实际的代码使用7个不同的类,所以给我一分钟尝试将所有这些打倒。ps:更改null为此没有工作:( – danwoods 2009-12-05 05:31:00

+0

抽出尽可能多的代码,我可以。希望帮助 – danwoods 2009-12-05 05:51:31

0

对不起还有太多的代码。

您需要阅读有关Painting in AWT and Swing的文章。你的代码是两者的混合体。

基本上,就像您在上次发布中告诉的那样,自定义绘画是通过覆盖JPanel的paintComponent(...)方法完成的。所以你做了自定义绘画,然后将JPanel添加到JApplet中。我给了你最后一篇文章中Swing教程的链接,它还包含了如何编写Applet的一节。

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

请先阅读文章并解决问题。

+0

更改扩展Panel来扩展Table类中的JPanel和示例类中的public void paint(Graphics g)protected void paintComponent(Graphics g)根据链接中的指示,现在没有任何被绘制/绘制...将继续使用它 – danwoods 2009-12-05 23:43:30

+0

注释update()似乎没有帮助 – danwoods 2009-12-05 23:45:11

+0

更改调用paint()重绘( )和编译器说,它不能找到符号 – danwoods 2009-12-05 23:49:48