S
SenioreFamicom
Gast
Hallöchen, ich sitz nun schon Stunden an dem Problem und langsam platzt meine Geduld.
Ziel ist es, eine Bilddatei als File-Objekt zu laden. Das Ergebnis wird weitergereicht an die Klasse TilesetPanel(File f), diese wiederrum übergibt die Datei als ImageIO.read(f)-Produkt an die Klasse TileMap(Image map), deren Konstrukter die Klasse Tile(Image img) auffruft und "map.getSubimage(...).getScaledInstance(...)" weiterreicht. Dummerweise zeigt mein Programm (siehe zweite Codebox -> Zeile 29) als Fehlermeldung "Can't read input file!" an.
Ich hoffe, ihr könnt mir helfen
Der Code:
class DisableIcon implements Constants {
private int x, y;
private Image img;
public DisableIcon(int x, int y) throws IOException {
this.x = x;
this.y = y;
img = ImageIO.read(new File(PATH + "/Icons/Stop(2).gif")).getScaledInstance(16 * 2, 16 * 2, 0);
}
public void draw(Graphics g) {
g.drawImage(img, x, y, null);
}
}
[/code]
Ziel ist es, eine Bilddatei als File-Objekt zu laden. Das Ergebnis wird weitergereicht an die Klasse TilesetPanel(File f), diese wiederrum übergibt die Datei als ImageIO.read(f)-Produkt an die Klasse TileMap(Image map), deren Konstrukter die Klasse Tile(Image img) auffruft und "map.getSubimage(...).getScaledInstance(...)" weiterreicht. Dummerweise zeigt mein Programm (siehe zweite Codebox -> Zeile 29) als Fehlermeldung "Can't read input file!" an.
Ich hoffe, ihr könnt mir helfen
Der Code:
Code:
public class TilesetPanel extends JPanel {
private JGrid grid;
private TileMap tileMap = null;
public TilesetPanel(File f) throws IOException {
tileMap = new TileMap(ImageIO.read(f));
grid = new JGrid(tileMap.getWidth(), tileMap.getHeight());
this.setPreferredSize(new Dimension(tileMap.getWidth()*16*2, tileMap.getHeight()*16*2));
this.setVisible(true);
//this.addMouseListener(this);
}
public Image getTileImageAt(int x, int y) {
return tileMap.tile[x][y].img;
}
public Tile getTileAt(int x, int y) {
return this.tileMap.tile[x][y];
}
public TileMap getTileMap() {
return this.tileMap;
}
public int getTileAtX(int x) {
return Math.round(x/(16*2));
}
public int getTileAtY(int y) {
return Math.round(y/(16*2));
}
public boolean allowDisable(int index) {
//for (int row = 0; row < parent.getJList().getModel().getSize(); row++) {
// int [] seq = parent.getSequenceData(parent.deleteSpace((String) parent.getJList().getModel().getElementAt(row)));
//for (int i = 0; i < seq.length; i++) {
// if (seq[i] == index) {
// return false;
// }
//}
//}
return true;
}
private void openPopupMenu(MouseEvent event) {
if (event.isPopupTrigger()) {
JPopupMenu tilemapPopup = new JPopupMenu();
JMenuItem mItem = new JMenuItem("Bearbeiten");
JCheckBoxMenuItem checkBoxItem = new JCheckBoxMenuItem("Enable", true);
//mItem.addActionListener(this);
tilemapPopup.add(mItem);
tilemapPopup.addSeparator();
tilemapPopup.add(checkBoxItem);
tilemapPopup.show(event.getComponent(), event.getX(), event.getY());
}
}
public void mousePressed(MouseEvent event) {
if (event.getButton() == MouseEvent.BUTTON1) {
if (allowDisable(tileMap.tile[getTileAtX(event.getX())][getTileAtY(event.getY())].getIndex())) {
if (tileMap.tile[getTileAtX(event.getX())][getTileAtY(event.getY())].getEnabled() == true) {
tileMap.tile[getTileAtX(event.getX())][getTileAtY(event.getY())].setEnabled(false);
} else {
tileMap.tile[getTileAtX(event.getX())][getTileAtY(event.getY())].setEnabled(true);
}
} else {
//new JErrorMessage(parent);
}
}
}
public void mouseReleased(MouseEvent event) {
openPopupMenu(event);
}
public void mouseClicked(MouseEvent event) {}
public void mouseEntered(MouseEvent event) {}
public void mouseExited(MouseEvent event) {}
public void actionPerformed(ActionEvent event) {}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (tileMap != null) {
tileMap.draw(g);
}
grid.draw(g);
repaint();
}
}
Code:
public class TileMap {
private int cnt = 0;
private int height;
private int width;
private BufferedImage map;
public Tile [][] tile;
public TileMap(Image map) {
System.out.println(map.getClass());
this.map = (BufferedImage) map;
System.out.println(map.getClass());
updateSet();
}
public void updateSet() {
this.tile = null;
this.width = this.map.getWidth () / 16;
this.height = this.map.getHeight() / 16;
tile = new Tile[this.width+1][this.height+1];
for (int y = 0; y < this.height; y++) {
for (int x = 0; x < this.width; x++) {
try {
System.out.println("..." + map.getSubimage(x*16, y*16, 16, 16).getClass());
System.out.println("..." + map.getSubimage(x*16, y*16, 16, 16).getScaledInstance(16*2, 16*2, 0).getClass());
tile[x][y] = new Tile(map.getSubimage(x*16, y*16, 16, 16).getScaledInstance(16*2, 16*2, 0), x*16*2, y*16*2, cnt);
} catch (IOException exc) {
System.out.println(exc.getMessage());
}
cnt++;
}
}
}
public int getWidth() {
return this.width;
}
public int getHeight() {
return this.height;
}
public void draw(Graphics g) {
if ((height != 0 || width != 0) && tile != null) {
for (int y = 0; y < this.height; y++) {
for (int x = 0; x < this.width; x++) {
tile[x][y].draw(g);
}
}
}
}
}
class DisableIcon implements Constants {
private int x, y;
private Image img;
public DisableIcon(int x, int y) throws IOException {
this.x = x;
this.y = y;
img = ImageIO.read(new File(PATH + "/Icons/Stop(2).gif")).getScaledInstance(16 * 2, 16 * 2, 0);
}
public void draw(Graphics g) {
g.drawImage(img, x, y, null);
}
}
Code:
public class Tile {
private int strXOffs;
private boolean enabled = true;
public int ID;
public int x, y;
public Image img = null;
private DisableIcon disableIcon = null;
private Font font;
public Tile(Image img, int x, int y, int ID) throws IOException {
this.x = x;
this.y = y;
this.ID = ID;
if (this.ID < 10) {
this.strXOffs = 27;
} else if (this.ID >= 10 && this.ID < 100) {
this.strXOffs = 21;
} else {
this.strXOffs = 17;
}
this.img = img;
this.font = new Font("Arial", Font.PLAIN, 9);
this.disableIcon = new DisableIcon(x, y);
}
public int getIndex() {
return this.ID;
}
public boolean getEnabled() {
return this.enabled;
}
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
public void draw(Graphics g) {
if (img != null) {
g.drawImage(img, x, y, null);
g.setColor(Color.WHITE);
g.setFont(font);
g.drawString("" + ID, x + strXOffs, y + 30);
if (this.enabled == false) {
this.disableIcon.draw(g);
}
}
}
}