jframe - Draw things after board is drawn with paint (JPanel) (java) -
i trying make simple tic tac toe game in java. however, cannot figure out how draw after i've drawn board. here's structure of game.
public class threeinarowmain extends jpanel { public static final int width = 600, height = 640; public void paint(graphics g) { g.setcolor(color.white); g.fillrect(0, 0, width, height); g.setcolor(color.black); g.drawstring("1.", 0,20); g.drawstring("2.", 210,20); g.drawstring("3.", 410,20); g.drawstring("4.", 0,220); g.drawstring("5.", 210,220); g.drawstring("6.", 410,220); g.drawstring("7.", 0,420); g.drawstring("8.", 210,420); g.drawstring("9.", 410,420); //horizontal lines g.drawline(0, 200, width, 200); g.drawline(0, 400, width, 400); //vertical lines g.drawline(200, 0, 200, height); g.drawline(400, 0, 400, height); } public static void main (string [] args) { boolean firstorsecond = true; jframe frame = new jframe("tic tac toe"); frame.setsize(width, height); frame.getcontentpane().add(new threeinarowmain()); frame.setlocationrelativeto(null); frame.setbackground(color.black); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); boolean someonewins = false; int firstplayerpos, secondplayerpos; int [] [] board = new int [3] [3]; scanner in = new scanner(system.in); while (!someonewins){ system.out.println("p1, enter number of square you'd mark."); firstplayerpos = in.nextint(); drawxoro(firstplayerpos,firstorsecond); system.out.println("p2, enter number of square you'd mark."); secondplayerpos = in.nextint(); drawxoro(secondplayerpos,!firstorsecond); } } public static void drawxoro (int position, boolean firstorsecond) { } }
i know badly designed game @ moment; correct later. however, want drawxoro draw x or , o in position player has typed in. how add graphics after using method paint? thanks!
- don't mix console input (
scanner
) ui. ui's event driven, is, happens, respond it. - have read of painting in awt , swing , performing custom painting. don't override
paint
, instead of overridepaintcomponent
, make sure callsuper.paintcomponent
- and might read through creating gui jfc/swing, how use buttons, check boxes, , radio buttons, how write action listenersand how write mouse listener ideas in how ui works , how respond user interation
Comments
Post a Comment