java - Painting a visual of Selection Sort -


i doing program creates array of 50 numbers, paints them in panel rectangles, size based on number. when panel clicked, array sorted , panel redrawn show sort of animation of numbers being sorted. here's it's supposed before , after clicking panel: picture http://oi47.tinypic.com/o57scm.jpg

and here code have:

public class animatedselectionsortpanel extends javax.swing.jpanel {  int[] numbers = new int[50]; int min = 20; int max = 100;  private void loadarray() {     int num;     (int = 0; < numbers.length; i++) {         numbers[i] = min + (int) math.random() * ((max - min) + 1);     } }  public static void selectionsort(int[] x) {     (int = 0; < x.length - 1; i++) {         int minindex = i;      // index of smallest remaining value.         (int j = + 1; j < x.length; j++) {             if (x[minindex] > x[j]) {                 minindex = j;  // remember index of new minimum             }         }         if (minindex != i) {             //...  exchange current element smallest remaining.             int temp = x[i];             x[i] = x[minindex];             x[minindex] = temp;         }     } }  private void drawpass(graphics g) {     int xpos = 10;     int ypos = 120;     int rectwidth = 1;      (int num : numbers) {         g.setcolor(color.black);         g.drawrect(xpos, ypos, rectwidth, num);         xpos += 11;     } }  @override public void paintcomponent (graphics g) {     while (numbers.length == 0) {         loadarray();     }     drawpass(g); }  private void sortpanelmouseclicked(java.awt.event.mouseevent evt) {     selectionsort(numbers);     sortpanel.repaint(); } 

the problem i'm having nothing being painted in frame when click panel. can tell me what's wrong i'm doing?

here's auto-generated code gui builder if helps:

    private void initcomponents() {      sortpanel = new javax.swing.jpanel();      sortpanel.setborder(javax.swing.borderfactory.createlineborder(new java.awt.color(0, 0, 0)));     sortpanel.addmouselistener(new java.awt.event.mouseadapter() {         public void mouseclicked(java.awt.event.mouseevent evt) {             sortpanelmouseclicked(evt);         }     });      javax.swing.grouplayout sortpanellayout = new javax.swing.grouplayout(sortpanel);     sortpanel.setlayout(sortpanellayout);     sortpanellayout.sethorizontalgroup(         sortpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgap(0, 398, short.max_value)     );     sortpanellayout.setverticalgroup(         sortpanellayout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgap(0, 165, short.max_value)     );      javax.swing.grouplayout layout = new javax.swing.grouplayout(this);     this.setlayout(layout);     layout.sethorizontalgroup(         layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addcomponent(sortpanel, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)     );     layout.setverticalgroup(         layout.createparallelgroup(javax.swing.grouplayout.alignment.leading)         .addgroup(layout.createsequentialgroup()             .addcomponent(sortpanel, javax.swing.grouplayout.preferred_size, javax.swing.grouplayout.default_size, javax.swing.grouplayout.preferred_size)             .addcontainergap(javax.swing.grouplayout.default_size, short.max_value))     ); }// </editor-fold> 

here suggestions:

  1. please post compilable example - sscce

  2. don't put logic paintcomponent , don't allocate data in method. purpose painting. prepare array of numbers in advance.

  3. min + (int) math.random() * ((max - min) + 1); 20 since (int) math.random() zero. should cast result, ie: min + (int) (math.random() * ((max - min) + 1));

  4. wrap sorting process thread or timer. wait between iterations , call repaint() paint intermediate results. wrapping swing timer cleaner , preferable, in posted code may easier/faster dump whole logic of selectionsort thread. see performing custom painting tutorial examples. see how use swing timers.

  5. looks drawpass draws rectangles upside down.

here example uses existing code minimal changes:

enter image description here enter image description here

import java.awt.*; import java.awt.event.*; import javax.swing.jframe; import javax.swing.swingutilities;  public class animatedselectionsortpanel extends javax.swing.jpanel {      public static void main(string[] args){         swingutilities.invokelater(new runnable() {              @override             public void run() {                 jframe frame = new jframe();                 frame.add(new animatedselectionsortpanel());                 frame.setdefaultcloseoperation(jframe.exit_on_close);                 frame.pack();                 frame.setlocationbyplatform(true);                 frame.setvisible(true);             }         });     }       private int[] numbers = new int[50];     private int min = 20;     private int max = 100;     private boolean shuffle = false;     public static final int iteration_sleep = 100;      public animatedselectionsortpanel() {         loadarray();         addmouselistener(new mouseadapter() {             @override             public void mouseclicked(mouseevent e) {                 if (shuffle)                     loadarray();                 selectionsort(numbers);                 shuffle = true;             }         });     }      @override     public dimension getpreferredsize() {         return new dimension(300, 100);     }      private void loadarray() {         (int = 0; < numbers.length; i++) {             numbers[i] = min + (int) (math.random() * ((max - min) + 1));         }     }      public void selectionsort(final int[] x) {         new thread(new runnable() {             @override             public void run() {                     (int = 0; < x.length - 1; i++) {                     int minindex = i; // index of smallest remaining value.                     (int j = + 1; j < x.length; j++) {                         if (x[minindex] > x[j]) {                             minindex = j; // remember index of new minimum                         }                     }                     if (minindex != i) {                         // ... exchange current element smallest remaining.                         int temp = x[i];                         x[i] = x[minindex];                         x[minindex] = temp;                     }                     repaint();                     try {                         thread.sleep(iteration_sleep);                     } catch (interruptedexception e) {                         e.printstacktrace();                     }                 }                            }         }).start();     }      private void drawpass(graphics g) {         int rectwidth = 1;          int width = getwidth() - 1;         int height = getheight() - 1;         int colspan = math.round((float)width / (float)numbers.length);         int x = 0;          (int num : numbers) {             int colheight = (int) ((float) height * ((float) num / (float) 100));             g.fillrect(x, height - colheight, rectwidth, colheight);              x += colspan;         }     }      @override     public void paintcomponent(graphics g) {         super.paintcomponent(g);         drawpass(g);     } } 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -