Hallo Community,
Erstmal, da ich neu bin, werfe ich erstmal ein nettes Hallo in die Runde..
Jetzt zu meinem Problem. Und zwar möchte ich mehrere ImageIcon erstellen, es sind zwar nur 14 Icons aber ich möchte, da ich neu in Java bin, über auch mit Arrays zu arbeiten.
(Hintergrund: Es sollen aus dieser Klasse 5 JToogleButten mit einem Würfel als Icon erstellt werden)
Ich hoffe das Bild ist Selbsterklärend
http://www.pic-upload.eu/p/e914f229058d775637c0c029029c6243
Und Hier nochmal die Codes:
Danke für Hilfe
TheCranky
Erstmal, da ich neu bin, werfe ich erstmal ein nettes Hallo in die Runde..
Jetzt zu meinem Problem. Und zwar möchte ich mehrere ImageIcon erstellen, es sind zwar nur 14 Icons aber ich möchte, da ich neu in Java bin, über auch mit Arrays zu arbeiten.
(Hintergrund: Es sollen aus dieser Klasse 5 JToogleButten mit einem Würfel als Icon erstellt werden)
Ich hoffe das Bild ist Selbsterklärend
http://www.pic-upload.eu/p/e914f229058d775637c0c029029c6243
Und Hier nochmal die Codes:
Java:
package com.thecranky.fivedices;
import com.thecranky.fivedices.gamewindow.GameWindow;
public class MainClass {
public static void main(String[] args) {
StartWindow();
}
private static void StartWindow() {
GameWindow win = new GameWindow();
win.setVisible();
}
}
Java:
package com.thecranky.fivedices.gamewindow;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import com.thecranky.fivedices.wuerfel.Wuerfel;
public class GameWindow extends JFrame {
Wuerfel wuerfel1 = new Wuerfel(0, 1, 1);
Wuerfel wuerfel2 = new Wuerfel(0, 1, 1);
Wuerfel wuerfel3 = new Wuerfel(0, 1, 1);
Wuerfel wuerfel4 = new Wuerfel(0, 1, 1);
Wuerfel wuerfel5 = new Wuerfel(0, 1, 1);
//Constructor
public GameWindow() {
this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
this.setLayout(null);
this.setSize(600, 600);
this.add(wuerfel1);
this.add(wuerfel2);
this.add(wuerfel3);
this.add(wuerfel4);
this.add(wuerfel5);
}
public void setVisible(){
this.setVisible(true);
}
}
Java:
package com.thecranky.fivedices.wuerfel;
import javax.swing.ImageIcon;
import javax.swing.JToggleButton;
public class Wuerfel extends JToggleButton {
private ImageIcon[] imageDiceUnS = new ImageIcon[7];
private ImageIcon[] imageDiceIsS = new ImageIcon[7];
private int wuerfelWert;
// Constructor
public Wuerfel(int wuerfelWert, int x, int y) {
iconImagesErstellen();
this.wuerfelWert = wuerfelWert;
this.setBounds(x, y, 60, 60);
this.setEnabled(false);
this.setIcon(imageDiceUnS[0]);
}
// Methods
public void iconImagesErstellen() {
for (int i = 0; i < imageDiceIsS.length; i++) {
this.imageDiceIsS[i] = new ImageIcon(getClass().getResource(
"wuerfel/images/DiceIsS" + i + ".gif"));
this.imageDiceUnS[i] = new ImageIcon(getClass().getResource(
"wuerfel/images/DiceUnS" + i + ".gif"));
}
}
// Getter & Setter
public int getWuerfelWert() {
return wuerfelWert;
}
public void setWuerfelWertAndImages(int wuerfelWert) {
this.wuerfelWert = wuerfelWert;
switch (wuerfelWert) {
case 1 :
setWuerfelImages("1");
break;
case 2 :
setWuerfelImages("2");
break;
case 3 :
setWuerfelImages("3");
break;
case 4 :
setWuerfelImages("4");
break;
case 5 :
setWuerfelImages("5");
break;
case 6 :
setWuerfelImages("6");
break;
default :
break;
}
}
public void setWuerfelImages(String x) {
this.setIcon(imageDiceUnS[Integer.parseInt("x")]);
this.setSelectedIcon(imageDiceIsS[Integer.parseInt("x")]);
this.setDisabledIcon(imageDiceIsS[Integer.parseInt("x")]);
}
}
Danke für Hilfe
TheCranky
Zuletzt bearbeitet: