I'm writing java code for a game and when creating the health bars I saw an exception that confused me greatly. The code and stack is below:
Code:
package com.teamanubiz.pixelhero;
import java.awt.Graphics;
import java.awt.Image;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import com.teamanubiz.gameapi.gfx.Sprite;
public class GUIRenderLayer {
public void renderStatBar(GUIPosition pos, Graphics g, int health, int maxHealth, int mana, int maxMana) {
Sprite healthBar = null;
try {
healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
healthBar.crop(0, 0, 128, 32);
healthBar.scale(256, 32);
Sprite manaBar = null;
try {
manaBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
manaBar.crop(0, 32, 128, 32);
manaBar.scale(265, 16);
Sprite temp = null;
try {
temp = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
temp.crop(0, 64, 128, 32);
temp.scale(256, 32);
Sprite emptyHealth = new Sprite(temp.getCurrent());
temp.scale(256, 16);
Sprite emptyMana = new Sprite(temp.getCurrent());
if (pos == GUIPosition.BOTTOM) {
double percent_h = health / maxHealth;
double percent_m = mana / maxMana;
healthBar.crop(0, 0, (int) ((int) 256 * percent_h), 32);
manaBar.crop(0, 0, (int) ((int) 256 * percent_m), 16);
g.drawImage(emptyMana.getCurrent(), 100, 464, null);
g.drawImage(emptyHealth.getCurrent(), 100, 432, null);
g.drawImage(healthBar.getCurrent(), 100, 432, null);
g.drawImage(manaBar.getCurrent(), 100, 464, null);
}
}
}
This class is referencing a custom library containing the class Sprite. It for some reason says that I am trying to cast a ToolkitImage to a BufferedImage in the below method of Sprite.java.
public void crop(int xOffset, int yOffset, int width, int height) {
BufferedImage temp = (BufferedImage) source;
temp = temp.getSubimage(xOffset, yOffset, width, height);
source = temp;
}
The variable source is an instance of an Image that is a field in Sprite.java
The stack below claims I am creating a ToolkitImage despite the fact new ImageIcon("res\\gui\\bar.png").getImage() returns only an Image. I do not convert the Image to a ToolkitImage ever in the code. This makes it extremely confusing.
Stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: sun.awt.image.ToolkitImage cannot be cast to java.awt.image.BufferedImage
at com.teamanubiz.gameapi.gfx.Sprite.crop(Sprite.java:48)
at com.teamanubiz.pixelhero.GUIRenderLayer.renderStatBar(GUIRenderLayer.java:55)
at com.teamanubiz.pixelhero.GameWindow.tick(GameWindow.java:14)
at com.teamanubiz.gameapi.Display.paint(Display.java:95)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1300(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$400(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
BufferedImage is a type of Image, but Image is not a type BufferedImage, you could, for example, cast BufferedImage to an Image.
Instead of using ImageIcon to load the image, use ImageIO.read, which returns a BufferedImage
So instead of...
Sprite healthBar = new Sprite((Image)new ImageIcon("res\\gui\\bar.png").getImage()); // I Never Instatiated a ToolkitImage!!!!!!!!
Nb: ImageIcon delegates the actual loading of the image to Toolkit and wraps it in a Icon interface
Use something like...
Sprite healthBar = new Sprite(ImageIO.read(new File("res\\gui\\bar.png")));
No you don't "convert" the Image to a ToolKitImage, but that's what the JVM is giving you when you call new ImageIcon("res\\gui\\bar.png").getImage(), and not a BufferedImage. The ImageIcon API states that getImage() returns an object of Image type, but does not specify what type of Image. Since Image is an interface, you know that there must be some concrete type underlying the returned Image object. If you need a BufferedImage you could one, and draw this Image into it.
Myself, I wouldn't even use an ImageIcon here if I wanted a BufferedImage, but rather would use ImageIO.read(...) which returns a BufferedImage.
So....
BufferedImage img = ImageIO.read(getClass().getResourceAsStream("res/gui/bar.png"));
Sprite healthBar = new Sprite(img);
Related
i got many errors about js... so i changed to chromedriver headless, and it work better for screenshot a specific element, but i got an errors in a other screenshot(s) code
Starting ChromeDriver 2.45.615291 (ec3682e3c9061c10f26ea9e5cdcf3c53f3f74387)
on port 44903
Only local connections are allowed.
janv. 31, 2019 7:02:22 AM org.openqa.selenium.remote.ProtocolHandshake createSession
INFOS: Detected dialect: OSS
Exception in thread "AWT-EventQueue-0" java.awt.image.RasterFormatException: (y + height) is outside of Raster
at sun.awt.image.ByteInterleavedRaster.createWritableChild(Unknown Source)
at java.awt.image.BufferedImage.getSubimage(Unknown Source)
at CarteEtdInfo.photoProfile(CarteEtdInfo.java:57)
at Accueil.<init>(Accueil.java:99)
at Login$2.actionPerformed(Login.java:287)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.AbstractButton.doClick(Unknown Source)
at javax.swing.plaf.basic.BasicRootPaneUI$Actions.actionPerformed(Unknown Source)
at javax.swing.SwingUtilities.notifyAction(Unknown Source)
at javax.swing.JComponent.processKeyBinding(Unknown Source)
at javax.swing.KeyboardManager.fireBinding(Unknown Source)
at javax.swing.KeyboardManager.fireKeyboardAction(Unknown Source)
at javax.swing.JComponent.processKeyBindingsForAllComponents(Unknown Source)
at javax.swing.JComponent.processKeyBindings(Unknown Source)
at javax.swing.JComponent.processKeyEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.KeyboardFocusManager.redispatchEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.preDispatchKeyEvent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.typeAheadAssertions(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
this is the the code that have errors from class CarteEtdInfo
public void photoProfile() throws IOException {
String cookie = String.join("\n",Files.readAllLines(Paths.get("temp\\cookie.txt")));
Login webpage = new Login();
WebDriver pagee = webpage.driver;
pagee.get("https://www4.inscription.tn/ORegMx/servlet/AuthentificationEtud?Idsession="+cookie+"&action1=toCarteEtd");
// Get entire page screenshot
WebElement taswira = driver.findElement(By.xpath("/html[1]/body[1]/table[1]/tbody[1]/tr[1]/td[1]/table[1]/tbody[1]/tr[1]/td[1]/table[2]/tbody[1]/tr[4]/td[1]/div[2]/div[1]/table[2]/tbody[1]/tr[2]/td[1]/img[1]"));
File screenshot = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
BufferedImage fullImg = null;
try {
fullImg = ImageIO.read(screenshot);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get the location of element on the page
org.openqa.selenium.Point point = taswira.getLocation();
// Get width and height of the element
int eleWidth = taswira.getSize().getWidth();
int eleHeight = taswira.getSize().getHeight();
// Crop the entire page screenshot to get only element screenshot
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight); // line 57
try {
ImageIO.write(eleScreenshot, "png", screenshot);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
// Copy the element screenshot to disk
File screenshotLocation = new File("temp\\avatar.png");
try {
FileUtils.copyFile(screenshot, screenshotLocation);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this is the line 57
BufferedImage eleScreenshot = fullImg.getSubimage(point.getX(), point.getY(), eleWidth, eleHeight);
the preivious code of screenshot an element in Login class works fine, so why this one have a problem ?
also the chromedriver not headless works fine but the headless not
Headless chrome default window size might be smaller than expected and this might cause your element to be outside of the window, hence not visible. You can either set the widndow size yourself or maximize window by using:
chromeOptions.addArguments(""--start-maximized")
i fixed the problem by adding
chromeOptions.addArguments("window-size=1980,960");
I have looked all over StackOverflow trying to find a solution for this but it's just kicking my ass. I have to remake Space Invaders for a class project and I need to load in the images (.gif files) using ImageIO.
I have all of my images and sound files in the source folder as shown below.
class structure
Whenever I use new File(/SItop0.gif"), it gives me an IIOException. Why can't I load my images in?
Here is my code:
public BufferedImage getImage() {
try {
BufferedImage image = ImageIO.read(new File(/SItop0.gif")); // Line 30: this is the same for SITop, SIMiddle, and SIBottom. I'm just getting different images for each.
return image;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
That is in the class for the object I want to draw on the panel, and the code below is in the class for the panel in which I call the method:
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
for(int i = 0; i < 5; i++) {
for(int j = 0; j < 10; j++) {
if(i == 0) {
SITop invader = new SITop();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null);
}
if(i == 1 || i == 2) {
SIMiddle invader = new SIMiddle();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null);
}
if(i == 3 || i == 4) {
SIBottom invader = new SIBottom();
g.drawImage(invader.getImage(), 75+35*j, 80+25*i, null); // Line 42
}
}
}
}
This is the error I get:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at SIBottom.getImage(SIBottom.java:30)
at SIPanel.paintComponent(SIPanel.java:42)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at javax.swing.JLayeredPane.paint(Unknown Source)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paintToOffscreen(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(Unknown Source)
at javax.swing.RepaintManager$PaintManager.paint(Unknown Source)
at javax.swing.RepaintManager.paint(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
at java.awt.GraphicsCallback$PaintCallback.run(Unknown Source)
at sun.awt.SunGraphicsCallback.runOneComponent(Unknown Source)
at sun.awt.SunGraphicsCallback.runComponents(Unknown Source)
at java.awt.Container.paint(Unknown Source)
at java.awt.Window.paint(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at javax.swing.RepaintManager$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.paintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.prePaintDirtyRegions(Unknown Source)
at javax.swing.RepaintManager.access$1200(Unknown Source)
at javax.swing.RepaintManager$ProcessingRunnable.run(Unknown Source)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
EDIT:
I changed the line to BufferedImage image = ImageIO.read(new File("Assets/SItop0.gif")); and changed the method to overwriting the paintComponent and now it opens up the frame and the panel but it doesn't seem to draw anything. The screen is just black.
EDIT 2:
Changed the code in my question to coincide with the code I'm using now.
as the title says, I'm getting a bunch of exceptions while trying to create several labels off an array into a GridLayout of a 100x100 cells, I believed it was supposed to fit perfectly but as I try to create the Tiles (which are merely an exception to JLabel with x,y and type variables) inside the panel which would represent the map, it seems that something is having trouble drawing them somewhere, been trying to use debug but haven't got any useful information about why it's happening.
This is the pertinent code (don't know if pastebin is against the rules, but it will make my life easier showing it to you all)
GUI class:
package gui;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class GUIJuego extends JFrame{
private JButton botonConstruirEscuela = new JButton("Escuela");
private JButton botonConstruirComisaria = new JButton("Comisaria");
private JButton botonConstruirCuartel = new JButton("Cuartel de Bomberos");
private JButton botonConstruirArbol = new JButton("Arbol");
private JButton botonConstruirCasa = new JButton("Casa");
private JButton botonConstruirComercio = new JButton("Comercio");
private JButton botonConstruirIndustria = new JButton("Industria");
private Tile[][] mapaTiles = new Tile[100][100];
private JLabel arcaLabel = new JLabel("Arca");
private JLabel puntosBellezaLabel = new JLabel("Puntos de Belleza");
private JLabel habitantesLabel = new JLabel("Habitantes");
public GUIJuego(){
JPanel panelConstruccion = new JPanel(new GridLayout(7,1));
JPanel panelDatosCiudad = new JPanel(new GridLayout(1,3));
JPanel panelMapa = new JPanel(new GridLayout(100,100));
add(panelConstruccion, BorderLayout.WEST);
panelConstruccion.add(botonConstruirEscuela);
panelConstruccion.add(botonConstruirComisaria);
panelConstruccion.add(botonConstruirCuartel);
panelConstruccion.add(botonConstruirArbol);
panelConstruccion.add(botonConstruirCasa);
panelConstruccion.add(botonConstruirComercio);
panelConstruccion.add(botonConstruirIndustria);
add(panelDatosCiudad, BorderLayout.NORTH);
panelDatosCiudad.add(arcaLabel);
panelDatosCiudad.add(puntosBellezaLabel);
panelDatosCiudad.add(habitantesLabel);
add(panelMapa, BorderLayout.CENTER);
cargarTiles(panelMapa);
}
private void cargarTiles(JPanel panel){
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
mapaTiles[i][j] = new Tile(i, j, 0);
asignarTile(mapaTiles[i][j], panel);
}
}
}
private void asignarTile(Tile tile, JPanel panel){
if(tile.getTipo() == 0){
tile.setText("Pasto");
panel.add(tile);
}
}
}
This is an isolated GUIJuego class, showing what I believe is causing problems:
public class GUIJuego extends JFrame{
private Tile[][] mapaTiles = new Tile[100][100];
public GUIJuego(){
JPanel panelMapa = new JPanel(new GridLayout(100,100));
add(panelMapa, BorderLayout.CENTER);
cargarTiles(panelMapa);
}
private void cargarTiles(JPanel panel){
for(int i = 0; i < 100; i++){
for(int j = 0; j < 100; j++){
mapaTiles[i][j] = new Tile(i, j, 0);
asignarTile(mapaTiles[i][j], panel);
}
}
}
private void asignarTile(Tile tile, JPanel panel){
if(tile.getTipo() == 0){
tile.setText("Pasto");
panel.add(tile);
}
}
}
I believe the issue is within the GUI Class, the demise of my application starting at line 50. This is the dump of the errors it gives me:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.SequencedEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Comparison method violates its general contract!
at java.util.TimSort.mergeLo(Unknown Source)
at java.util.TimSort.mergeAt(Unknown Source)
at java.util.TimSort.mergeCollapse(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.TimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
at java.util.Collections.sort(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.enumerateAndSortCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFocusTraversalCycle(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.LayoutFocusTraversalPolicy.getFirstComponent(Unknown Source)
at javax.swing.SortingFocusTraversalPolicy.getDefaultComponent(Unknown Source)
at java.awt.FocusTraversalPolicy.getInitialComponent(Unknown Source)
at java.awt.DefaultKeyboardFocusManager.dispatchEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.SequencedEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
EDIT: Tile Class
import javax.swing.JLabel;
public class Tile extends JLabel{
private int x, y, tipo;
public int getX() {
return x;
}
public void setX(int x) {
this.x = x;
}
public int getY() {
return y;
}
public void setY(int y) {
this.y = y;
}
public int getTipo(){
return tipo;
}
public void setTipo(int tipo){
if(tipo >= 0 || tipo <= 6)
this.tipo = tipo;
}
public Tile(int x, int y, int tipo) {
this.setX(x);
this.setY(y);
this.setTipo(tipo);
}
}
New information:
What is that black block?
That's what I get in the panel where the map is supposed to be, I don't know if that makes my issue clearer.
Thanks for reading!
In the Tile class don't override getX() and getY() methods. Those are methods defined by JComponent().
Instead maybe use getRow() and getColumn() along with setRow() and setColumn().
I am trying to add an image to an applet and I keep on failing. I am following http://docs.oracle.com/javase/tutorial/2d/images/loadimage.html but first of all, getCodeBase() is not defined. Then I got rid of it and came up with this code, which is giving some error messages and is not working. I am just getting a blank screen.
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
public class Cricket extends Canvas{
BufferedImage background;
/**
*
*/
private static final long serialVersionUID = 1L;
public Cricket() {
setSize(1000,500);
setBackground(Color.white);
}
#Override
public void paint(Graphics g) {
// TODO Auto-generated method stub
super.paint(g);
try {
URL url = new URL("resources/cricket_homescreen.png");
background = ImageIO.read(url);
} catch (IOException e) {
}
g.drawImage(background, 0, 0, null);
}
}
And I keep on getting these error messages:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MTProgramming.getImage(MTProgramming.java:18)
at MTProgramming.paint(MTProgramming.java:30)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at javax.swing.ImageIcon.<init>(Unknown Source)
at MTProgramming.getImage(MTProgramming.java:18)
at MTProgramming.paint(MTProgramming.java:30)
at sun.awt.RepaintArea.paintComponent(Unknown Source)
at sun.awt.RepaintArea.paint(Unknown Source)
at sun.awt.windows.WComponentPeer.handleEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$200(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Any ideas of what I am doing wrong here? (by the way my package explorer shows that the files are fine)
My below code works fine on running from NetBeans IDE
public Selector() {
try {
if (count == 0) {
initComponents();
count++;
ims.CPool.configureConnPool();
fillTable();
setResizable(false);
Dimension Size = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(new Double((Size.getWidth() / 2) - (getWidth() / 2)).intValue(), new Double((Size.getHeight() / 2) - (getHeight() / 2)).intValue());
jTable1.addMouseListener(this);
img = ImageIO.read(Selector.class.getResource("/ims/Icons/login.png"));
sci = img.getScaledInstance(jLabel3.getWidth(), jLabel3.getHeight(), java.awt.Image.SCALE_SMOOTH);
ImageIcon newIconImage = new javax.swing.ImageIcon(sci);
jLabel3.setIcon(newIconImage);
} else {
throw new myExc("Only one instance of the application is allowed to run at a time !");
}
} catch (HeadlessException | IOException | myExc x) {
x.printStackTrace();
JOptionPane.showMessageDialog(null,x.getMessage());
}
}
Whenever i try to launch the class having the above constructor i get an exception shown below
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input
== null!
at javax.imageio.ImageIO.read(Unknown Source)
at ims.init.Selector.<init>(Selector.java:60)
at ims.init.Selector$8.run(Selector.java:502)
at java.awt.event.InvocationEvent.dispatch(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Sour
ce)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
pls help
Check what's returned by the getResource() call
img = ImageIO.read(Selector.class.getResource("/ims/Icons/login.png"));
Could be that path is not correct "ims" but could be "imgs"