-
Notifications
You must be signed in to change notification settings - Fork 0
/
SortPanel.java
executable file
·63 lines (50 loc) · 1.7 KB
/
SortPanel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package net.bohush.sorting;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JPanel;
public abstract class SortPanel extends JPanel implements Runnable {
private static final long serialVersionUID = 1L;
protected static final int BORDER_WIDTH = 10;
private Dimension prefferedDimension;
protected int size;
protected int[] list;
protected int sleepTime;
private String name;
public SortPanel(String name, int sleepTime, int width, int height) {
prefferedDimension = new Dimension(width, height);
this.name = name;
this.sleepTime = sleepTime;
setBackground(Color.BLACK);
}
public void setList(int[] list) {
reset();
this.size = list.length;
this.list = java.util.Arrays.copyOf(list, size);
setBackground(Color.BLACK);
}
@Override
public Dimension getPreferredSize() {
return prefferedDimension;
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//draw border
g.setColor(Color.WHITE);
g.drawRect(BORDER_WIDTH, BORDER_WIDTH, getWidth() - 2 * BORDER_WIDTH, getHeight() - 2 * BORDER_WIDTH);
//draw title
Font nameFont = new Font(Font.MONOSPACED, Font.BOLD, 18);
FontMetrics nameFontMetrix = getFontMetrics(nameFont);
g.setColor(Color.BLACK);
g.fillRect((getWidth() - nameFontMetrix.stringWidth(name)) / 2, 0, nameFontMetrix.stringWidth(name), BORDER_WIDTH + nameFontMetrix.getAscent() / 3);
g.setColor(Color.WHITE);
g.setFont(nameFont);
g.drawString(name, (getWidth() - nameFontMetrix.stringWidth(name)) / 2, BORDER_WIDTH + nameFontMetrix.getAscent() / 3);
}
@Override
public abstract void run();
public abstract void reset();
}