java - How to attach an image file to a randomly rolled number that will display in a JPanel -
java - How to attach an image file to a randomly rolled number that will display in a JPanel -
i need create programme randomly roll 5 dice , display faces of these dice in jpanel, yahtzee game. have images of each die face , having problem trying attach randomly rolled number image. instance if random number 1 display die face 1 5 different faces. have below die class, panel class , gamedrive in order. sorry codes copied on , not sure if driver needed solve problem dint know code needed written work.
public class die implements comparator { private int face; public die() { super(); face = (int)(math.random() * 6) + 1; } public die(int f){ super(); face = f; } public int getface() { homecoming face; } public void setface(int face) { this.face = face; } @override public string tostring() { homecoming "die [face=" + face + "]"; } @override public boolean equals(object obj) { homecoming (this.getface() == ((die)obj).getface()); } @override public int compare(object a, object b) { die d1 = (die)a; die d2 = (die)b; if (d1.getface() == d2.getface()) homecoming 0; if (d1.getface() > d2.getface()) homecoming 1; homecoming -1; } }
panel class
public class panel extends jpanel { private bufferedimage [] img = new bufferedimage [6]; private int w = 0, h = 0, xloc = 0, yloc = 0; public panel() { super(); seek { img[0] = imageio.read(new file("die1.jpg")); img[1] = imageio.read(new file("die2.jpg")); img[2] = imageio.read(new file("die3.jpg")); img[3] = imageio.read(new file("die4.jpg")); img[4] = imageio.read(new file("die5.jpg")); img[5] = imageio.read(new file("die6.jpg")); } grab (ioexception e) { e.printstacktrace(); } this.setpreferredsize(new dimension(1300, 600)); w = img[0].getwidth(); h = img[0].getheight(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); xloc = 0; yloc = 0; (bufferedimage : img) { g.drawimage(i, xloc, yloc, w, h, null); xloc += w; } yloc += h; xloc = 0; (int = 0; < img.length; i++) { g.drawimage(img[i], xloc, yloc, w, h, null); xloc += w; } } }
driver
public class gamedriver { public static void main(string[] args) { frame f = new frame(); arraylist<die> roll = new arraylist<die>(5); (int =0; < 5; i++) roll.add(new die()); roll.sort(roll.get(0)); (die e : roll) system.out.println(e); } }
when create panel, need pass reference dice display.
(i think asking)
you pass in die values in constructor , reference them in "paint" method, example...
public class panel extends jpanel { private bufferedimage [] img = new bufferedimage [6]; private int w = 0, h = 0, xloc = 0, yloc = 0; private die[] state; public panel(die[] state) { super(); this.state = state; seek { img[0] = ... ... img[5] = imageio.read(new file("die6.jpg")); } grab (ioexception e) { e.printstacktrace(); } this.setpreferredsize(new dimension(1300, 600)); w = img[0].getwidth(); h = img[0].getheight(); } @override protected void paintcomponent(graphics g) { super.paintcomponent(g); // ...i got rid of bit draws of faces... xloc = 0; yloc = 0; (int = 0; < state.length; i++) { int imageindex = state[i].getface() - 1; g.drawimage(img[imageindex], xloc, yloc, w, h, null); xloc += w; } }
}
(i have not tried run obviously, might not compile!)
it not creating panel in of code posted, assuming want display in main method, you'd need this...
public class gamedriver { public static void main(string[] args) { frame f = new frame(); arraylist<die> roll = new arraylist<die>(5); (int =0; < 5; i++) roll.add(new die()); // bit strange... roll.sort(roll.get(0)); (die e : roll) system.out.println(e); // create panel panel panel = new panel(roll.toarray(new die[0])); // todo set in frame , show } }
java inheritance random jframe jpanel
Comments
Post a Comment