0

This application sorts one array . Want to represent the current item changed by a line equal value. I do not know where I would put the paint method (or draw) to draw my current item for the method "run ()" can not.

class sortareBubbleSort extends Canvas implements Runnable{

    Dimension dim = new Dimension (300 , 300) ;

    public Dimension getPreferredSize () {
        return dim ;
    }

    public void paint ( Graphics g) {
        g.setColor (Color.black);
        g.drawRect (0, 0, dim .width -1, dim.height -1);
    }

    //public void update ( Graphics g) {
    //paint (g);} 

    int nre,min,max;

    public sortareBubbleSort(int nre,int min,int max){
        this.nre=nre;
        this.min=min;
        this.max=max;
    }

    public void run(){
        int[] x=new int [nre];
        for(int i=0;i<x.length-1;i++)
            x[i]=min+(int)(Math.random()*((max-min)+1));
        boolean doMore = true;
        while (doMore) {
            doMore = false;  
            for (int i=0; i<x.length-1; i++) {
                if (x[i] > x[i+1]) {
                    int temp = x[i];
                    x[i] = x[i+1];
                    x[i+1] = temp;
                    doMore = true; 
                    g.drawLine(50,50,x[i+1],50);
                }
            }
        }
    }

In final I want it to look something like this:

for this array Arr[]={4,3,5,2,1} to draw in canvas :_ __ _ _ ____

( Arr sorted with one of sorted vector with one of methods)

1 2 3 4 5

Jarrod
  • 9,349
  • 5
  • 58
  • 73
  • 1
    Next time, please re-indent the code without using TABs (this is an example of why you should let your editor to convert tabs to spaces, to avoid mess like this), add some empty lines. And finally, but most important, **what exactly is your question?** – hyde Apr 11 '13 at 20:24
  • i want to draw a line equals with value of current element – user2248861 Apr 11 '13 at 20:44
  • Well that makes perfect sense. – christopher Apr 11 '13 at 20:53
  • @user2248861 Do you mean, you want to update the canvas after every swap? Then do that. You can't just draw the one element. And have all painting in that paint method. – hyde Apr 11 '13 at 20:55

1 Answers1

0

You need to do all painting to your paint() method, based on contents of x, so something like this:

public void paint ( Graphics g) {
    g.setColor (Color.black);
    g.drawRect (0, 0, dim .width -1, dim.height -1);
    if (x != null) {
      // loop which paints x
    }
}

For that to work, paint() method needs access to x, so it must be member variable, like this:

int nre,min,max;
int[] x; //added, leave to default value null 

Then change your run() method to match, first set the member variable at start:

x = new int[nre]; // int[] removed from this line, x is member variable

And then in your inner loop, replace g.drawLine() call simply with call to repaint().


You could optimize the drawing so that everything isn't repainted every time, but I think that's probably out of the scope of what you are doing, and anyway it's worth it only if you see performance is too poor.

Also, you may need to add other member variables, if you for example need this "current item" information for painting. If so, just add a member variable, and set it to correct value before calling repaint().

hyde
  • 60,639
  • 21
  • 115
  • 176