Foros del Web » Programación para mayores de 30 ;) » Java »

[SOLUCIONADO] Error de compilacion de clase chica

Estas en el tema de Error de compilacion de clase chica en el foro de Java en Foros del Web. por que esta clase sale error: Código: import java.awt.*; import javax.swing.*; public class Bomb extends Sprite { private int stepSize; private ImageIcon bombImage; public Bomb(){ ...
  #1 (permalink)  
Antiguo 14/10/2013, 23:24
Avatar de CMushroom  
Fecha de Ingreso: diciembre-2011
Ubicación: Morelos
Mensajes: 99
Antigüedad: 12 años, 4 meses
Puntos: 1
Pregunta Error de compilacion de clase chica

por que esta clase sale error:

Código:
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;
	}
}
--------------------Configuration: <Default>--------------------
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:
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;
	}
}
y este no es necesario de momento es el principal

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){
    }
    
}

Última edición por CMushroom; 15/10/2013 a las 01:23
  #2 (permalink)  
Antiguo 15/10/2013, 01:38
Avatar de Malenko
Moderador
 
Fecha de Ingreso: enero-2008
Mensajes: 5.323
Antigüedad: 16 años, 3 meses
Puntos: 606
Respuesta: Error de compilacion de clase chica

Limitandome a ver la documentación, la función paintIcon necesita como primer parámetro una clase de tipo Component (como podria ser JPanel o JComponent), en cambio, al usar el puntero this le estas pasando la clase Bomb que hereda de Sprite.

Yo probaría a cambiar el código a:

Código Java:
Ver original
  1. public void draw(JPanel panel){
  2.         Graphics paper = panel.getGraphics();
  3.         bombImage.paintIcon(panel, paper, x, y);
  4.         }
__________________
Aviso: No se resuelven dudas por MP!

Etiquetas: libro, programa
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:25.