막대 그래프를 그리기 위해이 작업을 수행했습니다. 그러나 repaint() 작업을 수행하면 새 차트가 이전 작업의 위에 그려집니다. 새로운 배열을 가진 약간의 repaints 후에 그것은 this처럼 보입니다. 그려진 막대를 지우거나 제거하려면 어떻게해야합니까?그래픽에 의해 그려진 사각형 제거/지우기 # fillRect()
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;
public class BarChart extends JPanel {
private int[] array;
public BarChart(int[] array) {
this.array = array;
}
public int[] getArray() {
return array;
}
public void setArray(int[] array) {
this.array = array;
}
@Override
protected void paintComponent(Graphics g) {
//Determine longest bar
int max = Integer.MIN_VALUE;
for (Integer value : array) {
max = Math.max(max, value);
}
//Paint bars
int width = (getWidth()/array.length) - 2;
int x = 1;
for (Integer value : array) {
int height = (int) ((getHeight() - 5) * ((double) value/max));
g.setColor(Color.blue);
g.fillRect(x, getHeight() - height, width, height);
g.setColor(Color.black);
g.drawRect(x, getHeight() - height, width, height);
x += (width + 2);
}
}
}
Billiant합니다. 고맙습니다. –