'(java) - repaint() works in one method but doesn’t work in another
repaint() works in one method but not another
So, I am trying to create a visual sorting algorithm for a project. Basically I draw 150 2d lines on a panel and, initially they are drawn in sorted order. Then I press a button to scramble them and it works perfectly. It redraws all the lines in random order every time I press the button. I then implemented a sorting algorithm i tested in a different project first to make sure it worked and it does. However, when I tell my program to sort my list of lines nothing happens visually on the program. I used print statements to make sure my list is actually being sorted and it is. I don't understand why the repaint() method works in the scramble() method but it doesn't work in the BubbleSort() method. I'm pretty new to GUI building and i'm using Apache Netbeans if thats important. I'll post my code below.
import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.ArrayList;
public class mainJpanel extends javax.swing.JPanel {
/**
* Creates new form mainJpanel
*/
ArrayList<Line> Mainlist = new ArrayList<>();
ArrayList<Line> templist = new ArrayList<>();
public mainJpanel() {
initComponents();
addLines(150);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
drawList(g);
}
public void drawList(Graphics g) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setStroke(new BasicStroke(4));
for (Line l : Mainlist) {
g2.drawLine(l.x1, l.y1, l.x2, l.y2);
}
}
public void addLines(int s) {
int size = s;
int x1 = 800;
int y1 = 300;
int x2 = 800;
int y2 = 600;
for (int i = 0; i < size; i++) {
Mainlist.add(new Line(x1, y1, x2, y2));
x1 -= 5;
x2 -= 5;
y1 += 2;
}
}
public void scramble() {
int x = 50;
int rand = 0;
int size = Mainlist.size();
for (int j = 0; j < size; j++) {
rand = (int) (Math.random() * Mainlist.size());
Mainlist.get(rand).setx1(x);
Mainlist.get(rand).setx2(x);
templist.add(Mainlist.get(rand));
Mainlist.remove(rand);
x += 5;
}
ResetLists();
repaint(); // repaint works here
}
private void ResetLists() {
for (int i = 0; i < templist.size(); i++) {
Mainlist.add(templist.get(i));
}
templist.clear();
}
public void BubbleSort() {
templist.add(new Line(0, 0, 0, 0));
int swaps = 1;
while (swaps > 0) {
swaps = 0;
for (int i = 1; i < Mainlist.size(); i++) {
if (Mainlist.get(i - 1).getYSize() > Mainlist.get(i).getYSize()) {
templist.set(0, Mainlist.get(i));
Mainlist.set(i, Mainlist.get(i - 1));
Mainlist.set(i - 1, templist.get(0));
swaps++;
} else {
//nothing to swap
}
}
}
repaint(); // repaint does nothing here
}
private void ScrambleButtonActionPerformed(java.awt.event.ActionEvent evt) {
scramble();
}
private void SortButtonActionPerformed(java.awt.event.ActionEvent evt) {
BubbleSort();
}
}
and below is the Line class I created to store Line objects in an ArrayList
public class Line {
int x1;
int y1;
int x2;
int y2;
public Line(int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void setx1(int x){
this.x1 = x;
}
public void setx2(int x){
this.x2 = x;
}
public int getYSize(){
return Math.abs(this.y1 - this.y2);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
