java - JComponents are not showing up on JPanel -
my jcomponents aren't showing on jpanel reason.
here's code:
main class
import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import javax.swing.jframe; import javax.swing.jpanel; // main class - executes game public class main extends jframe { public static int width = 800, height = 800; private jpanel jp; private dimension d; private snake s; private food f; public main() { settitle("snake"); setsize(width, height); setvisible(true); setdefaultcloseoperation(jframe.exit_on_close); setlocationrelativeto(null); d = new dimension(width, height); jp = new jpanel(); jp.setbackground(color.gray); jp.setpreferredsize(d); getcontentpane().add(jp); // adds snake s = new snake(); jp.add(s); // adds food f = new food(); jp.add(f); } @override public void paint(graphics g) { super.paint(g); graphics2d g2 = (graphics2d) g; } public static void main(string args[]) { new main(); } }
snake class
import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.awt.event.keyevent; import java.awt.event.keylistener; import java.util.arraylist; import javax.swing.jcomponent; // snake class - creates snake object public class snake extends jcomponent implements keylistener { public arraylist snakelist = new arraylist(); public int snakex, snakey, snakewidth, snakeheight; public snake() { snakex = 100; snakey = 100; snakewidth = 20; snakeheight = 20; snakelist.add(1); addkeylistener(this); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setcolor(color.red); g2.fillrect(snakex, snakey, snakewidth, snakeheight); } @override public void keypressed(keyevent e) { if(e.getkeycode() == keyevent.vk_w || e.getkeycode() == keyevent.vk_up) { snakex += 10; system.out.println("up"); } if(e.getkeycode() == keyevent.vk_s || e.getkeycode() == keyevent.vk_down) { snakex -= 10; system.out.println("down"); } if(e.getkeycode() == keyevent.vk_d || e.getkeycode() == keyevent.vk_right) { snakey += 10; system.out.println("right"); } if(e.getkeycode() == keyevent.vk_a || e.getkeycode() == keyevent.vk_left) { snakey -= 10; system.out.println("left"); } } @override public void keyreleased(keyevent e) { } @override public void keytyped(keyevent e) {} }
food class
import java.awt.color; import java.awt.graphics; import java.awt.graphics2d; import java.util.random; import javax.swing.jcomponent; import javax.swing.jpanel; // food class - class adds more snake objects when collided snake public class food extends jcomponent { private random r = new random(); public int foodx, foody, foodwidth = 20, foodheight = 20; public food() { foodx = 400; foody = 400; } @override public void paintcomponent(graphics g) { super.paintcomponent(g); graphics2d g2 = (graphics2d) g; g2.setcolor(color.yellow); g2.fillrect(foodx, foody, foodwidth, foodheight); } }
as can see, have 3 classes: 2 jcomponents , 1 jframe (my main class).
the main class has jpanel within added jframe, , jpanel adds both 2 jcomponents reason jcomponents not showing when execute though i'm pretty sure have done correctly.
if knows why not working that'd awesome. thanks!
your food jcomponent's preferred size 0,0, , size preferentially take. 1 simple solution either set preferred size or override getpreferredsize()
method, think if might expose bigger problem program. assume want display snake , food in same screen space. if so, you're program structure wrong -- food , snake classes should not component classes (e.g., shouldn't extend jpanel or jcomponent) rather should logical classes. should have 1 jpanel drawing has paintcomponent overridden , inside method paints snakes , food.
a side bit: you're overriding jframe's paint method, dangerous thing do. fortunately you're doing harm in currently, presence might tempt later draw in there. don't. recommend remove method , temptation. own preference never extend jframe there reason this, , risks doing so.
regarding comments:
to make clear, you're saying have 1 paintcomponent() in main
i don't know "main" is. mentioned above, create 1 jpanel painting, override protected void paintcomponent(graphics g)
method, , paint snake , food within method.
and not in either snake or food class?
correct. snake , food should not extend swing component , therefore should not override paintcomponent. give them public void draw(graphics g)
method called in above class's paintcomponent method , passes in graphics or graphics2d object given jvm.
and also, i'll make main extend jpanel instead of jframe. if that's case, create snake , food objects in main class?
these details depend on actual code. i'm confident you'll figure out.
and also, can keep keylistener in snake class or should put in main class?
i never use keylisteners , instead favor key bindings. @ many posts on subject, many example code more.
Comments
Post a Comment