-
Notifications
You must be signed in to change notification settings - Fork 1
/
SortPanel.java
76 lines (61 loc) · 2.29 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
64
65
66
67
68
69
70
71
72
73
74
75
76
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.ORANGE);
// setBackground(Color.WHITE);
// setBackground(Color.YELLOW);
}
@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("serif", Font.BOLD, 25);
FontMetrics nameFontMetrix = getFontMetrics(nameFont);
g.setColor(Color.ORANGE);
//g.fillRect(x,y,widht, height) where we take the midway of the panel and zeo hefiht and then take the string wight and height in the wight and height arameters
g.fillRect((getWidth() - nameFontMetrix.stringWidth(name)) / 2, 0, nameFontMetrix.stringWidth(name), BORDER_WIDTH + nameFontMetrix.getAscent() / 3);
g.setColor(Color.BLACK);
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();
protected void printNumberOnTop(Graphics g,int i, int x, int y){
Font nameFont = new Font("serif", Font.BOLD, 25);
FontMetrics nameFontMetrix = getFontMetrics(nameFont);
g.setColor(Color.BLACK);
g.setFont(nameFont);
// g.drawString(list[i], (getWidth() - nameFontMetrix.stringWidth(name)) / 2, BORDER_WIDTH + nameFontMetrix.getAscent() / 3);
Integer I = i;
g.drawString(I.toString(),x,y);
}
}