Código:
--------------------Configuration: <Default>--------------------import java.awt.*; import javax.swing.*; public class Bomb extends Sprite { private int stepSize; private ImageIcon bombImage; public Bomb(){ x = initialX; y = initialY; width = 5; height = 10; stepSize = 10; bombImage = new ImageIcon("imagddes.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); bombImage.paintIcon(this, paper, x, y); } public void move(){ y = y + stepSize; } }
C:\Documents and Settings\Administrador\Mis documentos\Bomb.java:26: error: method paintIcon in class ImageIcon cannot be applied to given types;
bombImage.paintIcon(this, paper, x, y);
^
required: Component,Graphics,int,int
found: Bomb,Graphics,int,int
reason: actual argument Bomb cannot be converted to Component by method invocation conversion
1 error
Process completed.
error: method paintIcon in class ImageIcon cannot be applied to given types;
ereda de esta clase:
Código:
y este no es necesario de momento es el principal public class Sprite{ protected int x, y, width, height; public int getX(){ return x; } public int getY(){ return y; } public int getWidth(){ return width; } public int getHeight(){ return height; } }
Código:
import java.awt.*; import java.awt.event.*; public class newGame extends Frame implements ActionListener, WindowListener{ private Graphics paper; private Defender defender; private Laser laser; private Bomb bomb; public static void main (String[] args){ newGame Jugar = new newGame(); Jugar.setSize(600,634); Jugar.setVisible(true); } private void newGame(){ setTitle("Invasores del espacio"); setLayout(new FlowLayout()); defender = new Defender(); alien = new Alien(); timer.start(); this.addWindowListener(this); } private void timer_Tick(){ if (bomb == null){ bomb = new Bomb(alien.getX(), alien.getY()); } moveAll(); drawAll(); checkHits(); } public void mouseClicked(MouseEvent event){ int initialX = defender.getX(); int initialY = defender.getY(); if (laser == null){ laser = new Laser(initialX, initialY); } } public void mouseMoved(MouseEvent event){ defender.move(event.getX()); } private void moveAll(){ alien.move(); if (bomb != null){ bomb.move(); } if (laser != null){ laser.move(); } } private void drawAll(){ Graphics paper = panel.getGraphics(); paper.setColor(Color.white); paper.fillRect(0, 0, panel.getWidth(), panel.getHeight()); paper.setColor(Color.black); defender.draw(panel); alien.draw(panel); if (laser != null){ laser.draw(panel); } if (bomb != null){ bomb.draw(panel); } } private void checkHits(){ if (collides(laser, alien)){ endGame("user"); } else{ if (collides(bomb, defender)){ endGame("alien"); } } if (bomb != null){ if (bomb.getY() > panel.getHeight()){ bomb = null; } } if (laser != null){ if (laser.getY() < 0){ laser = null; } } } private boolean collides(Sprite one, Sprite two){ if (one == null || two == null){ return false; } if ( one.getX() > two.getX() && one.getY() < (two.getY() + two.getHeight()) && (one.getX() + one.getWidth()) < (two.getX() + two.getWidth()) && (one.getY() + one.getWidth()) > (two.getY())){ return true; } else{ return false; } } private void endGame(String winner){ laser = null; bomb = null; timer.stop(); JOptionPane.showMessageDialog(null,"game over - " + winner + " wins"); } } public class Sprite{ protected int x, y, width, height; public int getX(){ return x; } public int getY(){ return y; } public int getWidth(){ return width; } public int getHeight(){ return height; } } public class Laser extends Sprite{ private int stepSize; private ImageIcon laserImage; public Laser(int newX, int newY){ x = newX; y = newY; width = 5; height = 5; stepSize = 10; laserImage = new ImageIcon("laser.jpg"); } public void draw(JPanel panel){ Graphics paper = panel.getGraphics(); laserImage.paintIcon(this, paper, x, y); } public void move(){ y = y - stepSize; } public void windowClosing(WindowEvent Event){ System.exit(0); } public void windowIconified(WindowEvent Event){ } public void windowOpened(WindowEvent Event){ } public void windowClosed(WindowEvent Event){ } public void windowDeiconified(WindowEvent Event){ } public void windowActivated(WindowEvent Event){ } public void windowDeactivated(WindowEvent Event){ } }