Skip to content

Commit 512d305

Browse files
committed
Now draw image static annotation on the image panel properly.
1 parent 23a9f66 commit 512d305

4 files changed

Lines changed: 108 additions & 40 deletions

File tree

src/hk/microos/data/Ellipse.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,28 @@
55
import java.util.ArrayList;
66

77
public class Ellipse {
8-
private ArrayList<Point_> keyPts = new ArrayList<>(); // 3 key points + 1 center
8+
private ArrayList<Point_> keyPts = new ArrayList<>(); // 3 key points + 1
9+
// center
910
public double major, minor, angle, x, y; // half mj mi
1011

11-
public Ellipse(ArrayList<Float> v){
12+
public Ellipse(ArrayList<Double> v) {
1213
this.major = v.get(0);
1314
this.minor = v.get(1);
1415
this.angle = v.get(2);
1516
this.x = v.get(3);
1617
this.y = v.get(4);
1718
}
18-
public Ellipse(float major, float minor, float angle, float x, float y) {
19+
public Ellipse(double major, double minor, double angle, double x, double y) {
1920
this.major = major;
2021
this.minor = minor;
2122
this.angle = angle;
2223
this.x = x;
2324
this.y = y;
24-
//calculate 3 key points
25+
// calculate 3 key points
2526
}
2627

2728
public Ellipse(Point_ mjA, Point_ mjB, Point_ miC) {
28-
29-
29+
3030
double mjA_x = mjA.x;
3131
double mjA_y = mjA.y;
3232
double mjB_x = mjB.x;
@@ -47,18 +47,21 @@ public Ellipse(Point_ mjA, Point_ mjB, Point_ miC) {
4747
keyPts.add(mjA);
4848
keyPts.add(mjB);
4949
keyPts.add(miC);
50-
keyPts.add(new Point_(this.x,this.y));
50+
keyPts.add(new Point_(this.x, this.y));
5151
}
52-
public ArrayList<Point_> getKeyPoints(){
52+
53+
public ArrayList<Point_> getKeyPoints() {
5354
return keyPts;
5455
}
55-
public Point_ getCenter(){
56+
57+
public Point_ getCenter() {
5658
return new Point_(this.x, this.y);
5759
}
60+
5861
public Ellipse2D.Double getErectedEllipse2D() {
5962
return new Ellipse2D.Double(x - major, y - minor, major * 2, minor * 2);
6063
}
61-
64+
6265
@Override
6366
public boolean equals(Object obj) {
6467
// TODO Auto-generated method stub
@@ -73,16 +76,19 @@ public String toString() {
7376
sb.append(" Angle: " + this.angle + " (" + this.angle * 180 / Math.PI + "dgr)\n");
7477
return sb.toString();
7578
}
76-
public String toRowFormatString(){
79+
public Ellipse offset(double x, double y){
80+
return new Ellipse(major, minor, angle, this.x+x, this.y+y);
81+
}
82+
public String toRowFormatString() {
7783
String fmt = "%.2f";
7884
String[] strs = new String[5];
79-
85+
8086
strs[0] = String.format(fmt, this.major);
8187
strs[1] = String.format(fmt, this.minor);
8288
strs[2] = String.format(fmt, this.angle);
8389
strs[3] = String.format(fmt, this.x);
8490
strs[4] = String.format(fmt, this.y);
85-
91+
8692
return String.join(",", strs);
8793
}
8894

src/hk/microos/frames/MyImagePanel.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,45 @@ public void paint(Graphics g) {
9898
drawElps(g2d, mImg.getElpses());
9999
drawUnfinishedPoints(g2d);
100100
drawLiveAssist(g2d);
101+
drawStaticEllipses(g2d);
102+
}
103+
}
104+
105+
public void drawStaticEllipses(Graphics2D g2d) {
106+
AffineTransform old = g2d.getTransform();
107+
// -----------------------------------------//
108+
ArrayList<Ellipse> staticEllipses = mImg.getEllipseStatic();
109+
if (staticEllipses == null)
110+
return;
111+
int i = 0;
112+
for (Ellipse e : staticEllipses) {
113+
e = e.offset(minX, minY);
114+
BasicStroke bs = UniversalTool.getPreferableStroke(Math.max(e.major, e.minor)*2);
115+
116+
117+
// do rotation
118+
g2d.rotate(e.angle, e.x, e.y);
119+
// draw elps
120+
121+
122+
Ellipse2D.Double ed = e.getErectedEllipse2D();
123+
124+
125+
126+
127+
128+
g2d.setColor(Color.black);
129+
g2d.setStroke(bs);
130+
g2d.draw(ed);
131+
132+
g2d.setColor(Color.yellow);
133+
g2d.setStroke(new BasicStroke(bs.getLineWidth()/2));
134+
135+
g2d.draw(ed);
136+
137+
// reset transform
138+
g2d.setTransform(old);
139+
i++;
101140
}
102141
}
103142

src/hk/microos/tools/IOTool.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ private static ArrayList<Ellipse> readNEllipse(ArrayList<String> lines, int star
106106
if(splitStr.length < 5){
107107
throw new Exception(String.format("Line %d: contains less than 5 float values.", at));
108108
}
109-
ArrayList<Float> splitFlt = new ArrayList<>();
109+
ArrayList<Double> splitFlt = new ArrayList<>();
110110
for(String s:splitStr){
111-
splitFlt.add(Float.parseFloat(s));
111+
splitFlt.add(Double.parseDouble(s));
112112
}
113113
elps.add(new Ellipse(splitFlt));
114114
}

src/hk/microos/tools/TableHelper.java

Lines changed: 48 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11
package hk.microos.tools;
22

3+
import java.awt.Color;
4+
import java.awt.Component;
35
import java.util.ArrayList;
46
import java.util.Set;
57

68
import javax.swing.DefaultListSelectionModel;
79
import javax.swing.JTable;
810
import javax.swing.ListSelectionModel;
11+
import javax.swing.table.DefaultTableCellRenderer;
912
import javax.swing.table.DefaultTableModel;
1013
import javax.swing.table.TableColumn;
1114

@@ -16,6 +19,7 @@ public class TableHelper {
1619
public DefaultTableModel tm;
1720
private ArrayList<String> rowStringList;
1821
private int staticRowNum = 0;
22+
1923
public TableHelper(JTable table) {
2024
this.table = table;
2125
this.tm = (DefaultTableModel) table.getModel();
@@ -46,41 +50,56 @@ public void setColSize(int[] colSize) {
4650
tc.setPreferredWidth(colSize[i]);
4751
}
4852
}
49-
50-
public void fillRightTable(ArrayList<String> staticCoords,ArrayList<String> coords) {
53+
54+
public void fillRightTable(ArrayList<String> staticCoords, ArrayList<String> coords) {
5155
clearAll();
52-
53-
//fill ellipses list
54-
//id, mja, min, angle, x, y
56+
// fill ellipses list
57+
// id, mja, min, angle, x, y
5558
int id = 1;
5659
rowStringList = new ArrayList<>();
57-
58-
if(staticCoords != null){
60+
61+
if (staticCoords != null) {
62+
5963
rowStringList.addAll(staticCoords);
6064
staticRowNum = staticCoords.size();
61-
for(String c: rowStringList){
62-
c = String.format("%d,%s", id, c);
63-
String[] splitStr = c.split(",");
64-
tm.addRow(splitStr);
65-
id++;
66-
}
67-
if(coords != null){
68-
rowStringList.addAll(coords);
69-
for(String c: coords){
65+
table.setSelectionBackground(Color.RED);
66+
table.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {
67+
@Override
68+
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
69+
boolean hasFocus, int row, int column) {
70+
71+
final Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row,
72+
column);
73+
if (!isSelected) {
74+
c.setBackground(row < staticRowNum ? Color.YELLOW : Color.WHITE);
75+
}
76+
77+
return c;
78+
}
79+
});
80+
for (String c : rowStringList) {
7081
c = String.format("%d,%s", id, c);
7182
String[] splitStr = c.split(",");
7283
tm.addRow(splitStr);
7384
id++;
7485
}
86+
if (coords != null) {
87+
rowStringList.addAll(coords);
88+
for (String c : coords) {
89+
c = String.format("%d,%s", id, c);
90+
String[] splitStr = c.split(",");
91+
tm.addRow(splitStr);
92+
id++;
93+
}
94+
}
7595
}
76-
}
77-
96+
7897
}
7998

8099
public void fillLeftRows(Set<String> list) {
81-
//list is path string
100+
// list is path string
82101
clearAll();
83-
102+
84103
// fill image List
85104
// "id","Image name", "#Marks","Path prefix"
86105
int id = 1;
@@ -108,23 +127,27 @@ public String getBehindRowDataAt(int row) {
108127
}
109128

110129
public void setValueAt(int row, int col, Object v) {
111-
if(row > this.tm.getRowCount())
112-
this.tm.setRowCount(row+1);
130+
if (row > this.tm.getRowCount())
131+
this.tm.setRowCount(row + 1);
113132
tm.setValueAt(v, row, col);
114133
}
115-
public void clearAll(){
134+
135+
public void clearAll() {
116136
for (int i = 0; i < tm.getRowCount(); i++) {
117137
tm.removeRow(i);
118138
}
119139
tm.setRowCount(0);
120140
}
141+
121142
public void setSelectedRow(int row) {
122143
if (row >= tm.getRowCount())
123144
return;
124145
this.table.getSelectionModel().setSelectionInterval(row, row);
125146
}
126-
public int getRowIndexOfValue(String v){
127-
if(rowStringList == null) return -1;
147+
148+
public int getRowIndexOfValue(String v) {
149+
if (rowStringList == null)
150+
return -1;
128151
return rowStringList.indexOf(v);
129152
}
130153
}

0 commit comments

Comments
 (0)