java - Painting JFrame Difficulty -
in code trying paint jframe
not painting correctly. tell frame paint in beginning create normal grey color once created. think may have fact repainting it, if how can make sure repainted yellow? try figure out why code not painting jframe
yellow? thank you!
public class evolutioncolor { public static void main(string args[]) { jframe frame = new jframe("bouncing ball"); frame.setdefaultcloseoperation(jframe.exit_on_close); ballpanel bp = new ballpanel(); frame.add(bp); frame.setsize(600, 600); // set frame size frame.setvisible(true); // display frame frame.setbackground(color.yellow); } class ballpanel extends jpanel implements actionlistener { private int delay = 10; protected timer timer; private int x = 0; // x position private int y = 0; // y position private int radius = 15; // ball radius private int dx = 2; // increment amount (x coord) private int dy = 2; // increment amount (y coord) public ballpanel() { timer = new timer(delay, this); timer.start(); // start timer } public void actionperformed(actionevent e) { repaint(); } public void paintcomponent(graphics g) { super.paintcomponent(g); // call superclass's paintcomponent g.setcolor(color.red); // check boundaries if (x < radius) { dx = math.abs(dx); } if (x > getwidth() - radius) { dx = -math.abs(dx); } if (y < radius) { dy = math.abs(dy); } if (y > getheight() - radius) { dy = -math.abs(dy); } // adjust ball position x += dx; y += dy; g.filloval(x - radius, y - radius, radius * 2, radius * 2); } }
don't set jframe yellow, set ballpanel object yellow.
Comments
Post a Comment