Hi, this is simple but I can't get it it to work. I want an initially yellow circle to then alternate b/t colors of green and blue. And also why can's I use the parameterized repaint method, it doesn't compile if I use it. Any help please.
import javax.swing.*; import java.awt.*; import java.awt.event.*; @SuppressWarnings("serial") public class Circle_Practice extends JFrame { private int node_height = 30;//imaginary rectangle's ht to draw the circle private int node_width = 30;//imaginary rectangle's width to draw the circle private int canvas_width = 360;//width of JFrame private int canvas_height = 360;//height of JFrame private int start_x = (canvas_width/2) - node_width/2; private int start_y = 10; private int delay = 200; private int count = 0; public Circle_Practice(int[] arr ) { this.add( new Canvas() ); this.setDefaultCloseOperation(EXIT_ON_CLOSE); this.setVisible(true); this.pack(); Timer t = new Timer(delay, new ActionListener() {//Timer is the listener public void actionPerformed(ActionEvent e) { while ( count < 20_000 ) { if ( count % 2 == 0 ) { dummy_change_to_green();//why doesn't the red circle change color? repaint(/**ball_x, ball_y, ball_width, ball_height*/);//how do I only redraw necessary updates and not entire screen, so just the updated circle and not the background too? count++; } else { dummy_change_to_blue(); repaint(); count++; } } } }); t.start(); } //dummy method to practice Swing timer //change border to red...doesn't work... private void dummy_change_to_green()//(Graphics2D g2d) { g2d.setColor(Color.GREEN); g2d.drawOval(start_x, start_y, node_width, node_height); } private void dummy_change_to_blue()//(Graphics2D g2d) { g2d.setColor(Color.BLUE); g2d.drawOval(start_x, start_y, node_width, node_height); } //draw the initial binary heap here only private class Canvas extends JPanel { public Canvas() { setBorder(BorderFactory.createLineBorder(Color.black)); } public Dimension getPreferredSize() {//overridden method (to draw inside Canvas, NOT JFrame) return new Dimension(360,360); } public void paintComponent(Graphics g)//NB: paintComponent is a method that I override from JPanel class (which I extend) { super.paintComponent(g);//Q:why do I need this again? A: // Most of the standard Swing components have their look & feel // implemented by separate "UI Delegate" objs. The invocation of super.paintComponent(g) passes the graphics context off to the component's // UI delegate, which paints the panel's background (src: http://docs.oracle.com/javase/tutorial/uiswing/painting/step2.html) final Graphics2D g2d = (Graphics2D)g; //draw first node centered to canvas g2d.setColor(Color.YELLOW); g2d.fillOval(start_x, start_y, node_width, node_height); g2d.setColor(Color.BLACK); g2d.drawOval(start_x, start_y, node_width, node_height); g2d.drawString("80", start_x + 5, start_y + 15); //gist: draw all the nodes and wait for some delay before beginning heap sort (to call update_nodes method) } } }