Results 1 to 3 of 3
  1. #1
    ventrue is offline Member
    Join Date
    Feb 2009
    Rep Power

    Default Need help with paintImmediately method

    Hi, I am working on an assignment where I have to display all components of an array (with random values from 0 to 50) as rectangles, while they are being sorted by an algorithm (such as Selection Sort, Insertion Sort and Merge Sort, with more).

    I am not going to use threads, so I was recommended by my teacher to use a method called "paintImmediately()".
    So, I got 3 classes: one that will handle the painting, one for the Gui part(with panels, buttons etc.) and one for an algorithm, called Overview, Gui and Selection respectively.

    Some parts of the code can be found below, and I will mark the field where I need some help/insight.

    This is an outtake from my Gui class, it should display the array from the Overview class, consisting of 10 randomly generated integer values, after I push the button marked "New Array". I would be happy if anyone could explain how to use the marked method to paint the components of the array.

    Java Code:
     public void actionPerformed(ActionEvent handler){
               if (handler.getActionCommand().equals("New Array")){
                    Overview view = new Overview();
                    [COLOR="SeaGreen"][U][B]view.paintImmediately(int x,int y,int w, int h);[/B] [/U]          [/COLOR]   
    This next outtake is from the Overview class. This is where I have my code for creating the array and painting the values inside it as rectangles with a fixed color.

    Java Code:
        public class Overview extends JPanel{
        int array[];
        Random a = new Random();
        int k = 0;
        public Overview(){
            array = new int[10];
        public void makeRandomArray(){
            for (int i = 0; i < array.length; i++){
                array[i] = a.nextInt(50);
    [COLOR="SeaGreen"][B]    public void paintComponent(Graphics g){
            for (int i = 0; i< array.length; i++){
                g.drawRect(k, 50-array[i], 10, array[i]);
                g.fillRect(k, 50-array[i], 10, array[i]);
    The marked field here represents the code that paints graphics: rectangles with 5 pixels space (reason for 15 increment, since the width of the rectangles is 10), height is computed in a way so I can watch the rectangles grow up (instead of down, reason for 50-array[i], since I will set this panel to only be 50 pixels high).

    So to my question: Could anyone explain how to use the paintImmediately method to paint the "updated" values on the panel? Maybe my code for the "paintComponent()" method is wrong?

    I should be able to figure out how to use paintImmediately() on the algorithm section as long as I get to know how to use it first :).

  2. #2
    Steve11235's Avatar
    Steve11235 is offline Senior Member
    Join Date
    Dec 2008
    Rep Power


    What values are you using for x, y, height, and width? I'm not that familiar with painting, but you have to specify the region you want repainted.

    I'm guessing either x and y = 0, and height and width from view.getSize() are appropriate, or the rectangle from view.getBounds().

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Blog Entries
    Rep Power


    I'm not sure why your teacher would recommend to use paintImmediately here. If you wanted to visualize a sort, then you could use a Swing Timer and do passive painting via paintComponent.

Similar Threads

  1. Replies: 2
    Last Post: 03-26-2010, 05:12 PM
  2. Replies: 1
    Last Post: 02-10-2009, 10:03 AM
  3. how to use a variable of one method in another method
    By lucasautomacao in forum New To Java
    Replies: 4
    Last Post: 10-29-2008, 02:21 PM
  4. Replies: 29
    Last Post: 09-25-2008, 07:55 PM
  5. cannot call private method from static method
    By jon80 in forum New To Java
    Replies: 3
    Last Post: 05-07-2008, 08:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts