here's code:
class aa
package com.mahbonnets.game; import javax.swing.*; public class aa { public static ab f = new ab(); public static int width = 600; public static int height = 400; public static void main(string args[]) { f.setsize(width, height); f.setresizable(false); f.setvisible(true); f.setdefaultcloseoperation(jframe.exit_on_close); f.settitle("mah bonnets gone"); f.setlocationrelativeto(null); system.out.println("running!!"); } }
ab
package com.mahbonnets.game; import java.awt.gridlayout; import javax.swing.*; public class ab extends jframe { public ac panel; public ab() { panel = new ac(this); setlayout(new gridlayout (1, 1, 0, 0)); add(panel); } }
and ac
package com.mahbonnets.game; import java.awt.color; import java.awt.graphics; import java.awt.rectangle; import javax.swing.*; public class ac extends jpanel implements runnable { public rectangle floor; public int floorheight = 80; public int fps = 1000; public boolean objectdefine = false; public thread game; public ac(ab f) { setbackground(color.black); defineobjects(); game = new thread(this); game.start(); } void defineobjects() { floor = new rectangle(-10, aa.height-floorheight, aa.width+10, floorheight); objectdefine = true; repaint(); } public void paint(graphics g) { super.paint(g); if(objectdefine) { g.setcolor(color.red); g.fillrect(floor.x, floor.y, floor.width, floor.height); } } public void fpssetter() { try{ thread.sleep(fps/1000); }catch(exception e) { e.printstacktrace(); } } @override public void run(){ // todo auto-generated method stub } }
what isn't happening that's supposed happening red rectangle supposed appear @ bottom of jframe. i'm entirely new programming, i've looked on section of code pertaining rectangle , looks in order... @ least... far can tell.
if have idea what's wrong please me out. thanks.
here's youtube tutorial i've been following http://www.youtube.com/watch?v=0lfhckair-8
- don't rely on magic numbers...
- don't rely on parameters may not match reality (
public static int width = 600
isn't going same size child component). usegetwidth
,getheight
actual size of component... - get rid of
defineobjects
, rely on known states of component - get rid of
paint
method , usepaintcomponent
instead static
not friend- don't use youtube reference, unless poster has references ;)
- do read performing custom painting
- do read painting in awt , swing
- do read concurrency in swing
replace paint
method more this...
@override protected void paintcomponent(graphics g) { super.paintcomponent(g); //to change body of generated methods, choose tools | templates. int x = -10; int y = getheight() - floorheight; int width = getwidth() + 10; int height = floorheight; floor = new rectangle(x, y, width, height); g.setcolor(color.red); g.fillrect(floor.x, floor.y, floor.width, floor.height); }
and thread.sleep(1000 / 1000)
close no sleep makes no difference ;) - 25fps rough 40 milliseconds ;)
Comments
Post a Comment