java - A comparable sorter -
i'm trying code compare height of randomly generated objects , sort them in ascending order. i'm having trouble understanding basic concepts , as read on java docs i'm not finding answer works me. know don't have in sorting listener, i'm sort of @ loss go here.
edit: i've fixed portions of code, there's problem redrawing few of bars , out of order.
import java.awt.event.*; import javax.swing.*; import java.awt.*; import java.util.random; public class lab7 extends jpanel { random gen = new random(); boolean tick = true; private static bars[] bar = new bars[20]; private static integer[] height = new integer[20], heighttemp = new integer[1]; private final int width = 500, height = 300; private final int delay = 900; private int size = 15; int[] y = new int[20], ytemp = new int[1]; int [] x = new int [20]; timer timer; public lab7() { if(tick == true) { for(int count = 0; count < bar.length; count++) { height[count] = gen.nextint(250) + 5; y[count] = (height - height[count]); x[count] = (count * size +(count*5)); bar[count] = new bars(x[count], y[count], size, height[count]); } tick = false; } timer = new timer(delay, new sortinglistener()); setpreferredsize(new dimension(width, height)); setbackground(color.white); timer.start(); } public void paintcomponent(graphics page) { super.paintcomponent(page); for(int count = 0; count < bar.length; count++) { bar[count].draw(page); } } private class sortinglistener implements actionlistener { public void actionperformed(actionevent event) { int min = 0; for(int count = 0; count < height.length; count++) { for(int index = count + 1; index < height.length; index ++) { if(height[count].compareto(height[index]) > 0) { heighttemp[0] = height[count]; ytemp[0] = y[count]; height[count] = height[index]; y[count] = y[index]; height[index] = heighttemp[0]; y[index] = ytemp[0]; } system.out.println(count + ":" + min); if(index == 19 && height[count].compareto(height[index]) != 0) { bar[count] = new bars(x[count], y[count], size, height[count]); bar[index] = new bars(x[index], y[index], size, height[index]); } } repaint(); } timer.stop(); } } public static void main(string[]args) { jframe frame = new jframe("lab7"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.getcontentpane().add(new lab7()); frame.pack(); frame.setvisible(true); } }
here's bars class well.
public class bars{ random gen = new random(); private int width = 20, height, x, y; private int r = gen.nextint(256); private int g = gen.nextint(256); private int b = gen.nextint(256); public color randomcolor; //creates rectangle public bars(int x, int y, int width, int height) { this.x = x; this.y = y; this.width = width; this.height = height; } public void draw(graphics page) { color randomcolor = new color(r, g, b); page.setcolor(randomcolor); page.fillrect(x, y, width, height); polygon p = new polygon(); page.setcolor(randomcolor); page.fillpolygon(p); } public void setwidth(int width) {this.width = width;} public int getwidth() {return width;} public void setheight(int height) {this.height = height;} public int getheight() {return height;} public void setx(int x) {this.x = x;} public int getx() {return x;} public void sety(int y) {this.y = y;} public int gety() {return y;} public void setcolor(color randomcolor) {this.randomcolor = randomcolor;} public color getcolor() {return randomcolor;}}
if you're doing selection sort, inner loop needs start @ value of outer loop. change
for(int index = 0;
to
for(int index = count;
the error you're getting list unsorted right? looks first element of "sorted" list in correct spot.
Comments
Post a Comment