继续获取错误:javax.swing.ImageIcon类型的方法getImg()未定义

时间:2017-11-06 04:20:56

标签: java swing imageicon

我的朋友代码这个破砖游戏扯掉了,但他不断收到这个错误 “错误:方法getImg()未定义类型javax.swing.ImageIcon”,我不知道如何修复它。有人可以帮忙吗? 这里是代码:

public class Ball extends Sprite implements iFace{  
private int xDir;
  private int yDir;

public Ball(){

xDir = 1;
yDir = -1;

String path = "MAJOR_JAVA_ASSESSMENT/ball.png";
ImageIcon i = new ImageIcon(this.getClass().getResource(path));
img = i.getImg();


iWidth = img.getWidth(null);
iHeight = img.getHeight(null);

reset(); }

  public void move()
  {
    x += xDir;
    y += yDir;

if(x == 0)
{
  setXDir(1);
}

if(x == width - iWidth)
{
  setXDir(-1);
}

if (y == 0)
{
  setYDir(1);
}  }

  private void reset()
  {
    x = initBallX;
    y = initBallY;
  }

  public void setXDir(int x)
  {
    xDir = x;
  }

  public void setYDir(int y)
  {
    yDir = y;
  }

  public int getYDir()
  {
    return yDir;
  }
}

这里有另一个代码:

  import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.RenderingHints;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.util.Timer;
import java.util.TimerTask;
import javax.swing.JPanel;

public class Board extends JPanel implements iFace
{

 private Timer timer;
  private String message = "You lose";
  private Ball ball;
  private Paddle paddle;
  private Brick bricks[];
  private boolean inGame = true;

  public Board()
  {
    initBoard();
  }



 private void initBoard()
  {
    addKeyListener(new timeAdapter());
    setFocusable(true);

    bricks = new Brick[numOfBricks];
    setDoubleBuffered(true);
    timer = new Timer();
    timer.scheduleAtFixedRate(new ScheduleTask(), delay, period);
  }

  @Override
  public void addNotify()
  {
    super.addNotify();
    gameInit();
  }

  private void gameInit()
  {
    ball = new Ball();
    paddle = new Paddle();

int k = 0;
  for(int i = 0; i < 5; i++)
{
  for(int j = 0; j < 6; j++)
  {
    bricks[k] = new Brick(j * 40 + 30, i * 10 + 50);
    k++;
  }
}
  }

 @Override
  public void paintComponent(Graphics g)
  {
    super.paintComponent(g);

    Graphics2D graphics2D = (Graphics2D) g;

graphics2D.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
graphics2D.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

if (inGame)
{
  drawObj(graphics2D);
}

else
{
  gameDone(graphics2D);
}

Toolkit.getDefaultToolkit().sync();
  }




 private void drawObj(Graphics graphics2D)
  {
    graphics2D.drawImage(ball.getImg(), ball.getX(), ball.getY(), ball.getWidth(), ball.getHeight(), this);
    graphics2D.drawImage(paddle.getImg(), paddle.getX(), paddle.getY(), paddle.getWidth(), paddle.getHeight(), this);

for (int i = 0; i < numOfBricks; i++)
{
  if(!bricks[i].isDead())
  {
    graphics2D.drawImage(bricks[i].getImg(), bricks[i].getX(), bricks[i].getY(), bricks[i].getWidth(), bricks[i].getHeight(), this);
  }
}
  }



  private void gameDone(Graphics2D graphics2D)
  {
    Font f = new Font("TimesRoman", Font.BOLD, 20);
    FontMetrics fm = this.getFontMetrics(f);

    graphics2D.setColor(Color.RED);
    graphics2D.setFont(f);
    graphics2D.drawString(message, (iFace.width - fm.stringWidth(message)) / 2, iFace.width / 2);

  }



  private class timeAdapter extends KeyAdapter
  {
    @Override    
    public void keyReleased(KeyEvent p)
    {
      keyReleased(p);
    }

@Override
public void keyPressed(KeyEvent p)
{
  keyPressed(p);
}
  }



  private class ScheduleTask extends TimerTask
  {
    @Override
    public void run()
    {
      ball.move();
      paddle.move();
      checkCollision();
      repaint();
    }
  }

  private void stopGame()
  {
    inGame = false;
    timer.cancel();
  }



  private void checkCollision()
  {
    if(ball.getRectangle().getMaxY() > iFace.bottom)
    {
      stopGame();
    }

for(int i = 0, j = 0; i < numOfBricks; i++)
{
  if(bricks[i].isDead())
  {
    j++;
  }

  if(j == numOfBricks)
  {
    message = "GG YOU WIN";
    stopGame();
  }
}

if ((ball.getRectangle()).intersects(paddle.getRectangle()))
{
  int paddlePos = (int) paddle.getRectangle().getMinX();
  int ballPos = (int) ball.getRectangle().getMinX();

  int first = paddlePos + 8;
  int second = paddlePos + 16;
  int third = paddlePos + 24;
  int fourth = paddlePos + 32;

  if (ballPos < first)
  {
    ball.setXDir(-1);
    ball.setYDir(-1);
  }

  if (ballPos >= first && ballPos < second)
  {
    ball.setXDir(-1);
    ball.setYDir(-1 + ball.getYDir());
  }

  if (ballPos >= second && ballPos < third)
  {
    ball.setXDir(0);
    ball.setYDir(-1);
  }

  if (ballPos >= third && ballPos < fourth)
  {
    ball.setXDir(1);
    ball.setYDir(-1);
  }
}

for (int i = 0; i < numOfBricks; i++)
{
  if ((ball.getRectangle()).intersects(bricks[i].getRectangle()))
  {
    int ballLeft = (int) ball.getRectangle().getMinX();
    int ballHeight = (int) ball.getRectangle().getHeight();
    int ballWidth = (int) ball.getRectangle().getWidth();
    int ballTop = (int) ball.getRectangle().getMinY();

    Point pR = new Point(ballLeft + ballWidth + 1, ballTop);
    Point pL = new Point(ballLeft - 1, ballTop);
    Point pT = new Point(ballLeft, ballTop - 1);
    Point pB = new Point(ballLeft, ballTop + ballHeight + 1);

    if (!bricks[i].isDead())
    {
      if (bricks[i].getRectangle().contains(pR))
      {
        ball.setXDir(-1);
      }
      else if
        (bricks[i].getRectangle().contains(pL))
      {
        ball.setXDir(1);
      }

      if (bricks[i].getRectangle().contains(pT))
      {
        ball.setYDir(1);
      }
      else if (bricks[i].getRectangle().contains(pB))
      {
        ball.setYDir(-1);
      }

      bricks[i].setDeath(true);
    }
  }
}
  }
}

我只给你球代码,因为有问题的两个代码基本上都有相同的问题,所以如果有人可以解决这个问题,那么我们可以休息。 还给你板子作为最大和最重要的代码,所以这个代码可能是问题? 感谢您阅读本文,我希望您能帮助我的朋友。

1 个答案:

答案 0 :(得分:1)

getImg API中不存在

ImageIcon,我想你会发现它是ImageIcon#getImage而不是

话虽如此,我建议使用ImageIO API