/* Copyright (c) 2008, University of Southampton All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the University of Southampton nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ import java.applet.Applet; import java.awt.*; public class rassociateV2 extends Applet { public rassociateV2() { markerdiameter = 8; markerSize = new Dimension(markerdiameter, markerdiameter); border = 30; nopaint = -100; fontname = "TimesRoman"; fontsize = 12; } int companion(int i) { if(i % 2 == 0) return i - 1; else return i + 1; } void drawconnection(Graphics g, Point point, int i) { Point point1 = markerLocations[companion(i)]; g.drawLine(point.x, point.y, point1.x, point1.y); } void drawconnectionspare(int i, Graphics g) { Point point2 = new Point(0, 0); Point point3 = new Point(0, 0); Point point = markerLocations[i]; Point point1 = markerLocations[companion(i)]; int j = point.x - point1.x; int k = point.y - point1.y; double d = Math.sqrt(j * j + k * k); double d1 = (double)(markerdiameter / 2) / d; point2.x = markerLocations[i].x + (int)Math.round((double)j / d1); point2.y = markerLocations[i].y + (int)Math.round((double)k / d1); point3.x = markerLocations[companion(i)].x + (int)Math.round((double)j / d1); point3.y = markerLocations[companion(i)].y + (int)Math.round((double)k / d1); g.drawLine(point2.x, point2.y, point3.x, point3.y); } void drawmarkeratpoint(Graphics g, Point point, Dimension dimension, int i) { Point point1 = new Point(point.x - dimension.width / 2, point.y - dimension.width / 2); g.drawOval(point1.x, point1.y, markerdiameter, markerdiameter); g.fillOval(point1.x, point1.y, markerdiameter, markerdiameter); drawconnection(g, point, i); } public String getValue() { String s = "Applet:"; if(targetsIdentified) s = s + getnameoftarget() + "; " + readpoints(); else s = s + readpoints(); return s; } String getnameoftarget() { String s = ""; for(int i = 1; i < noOfMarkers + 1; i++) { Point point = new Point(markerLocations[i].x, markerLocations[i].y); int j = point.x; int k = point.y; boolean flag = false; String s1 = ""; for(int l = 1; l < noOfTargets + 1; l++) { int i1 = targetAreas[l].x; int j1 = targetAreas[l].y; int k1 = targetAreas[l].width; int l1 = targetAreas[l].height; if(targetAreas[l].contains(point)) { flag = true; s1 = targetNames[l]; } } if(flag) { if(s.equals("")) s = s1; else s = s + ":" + s1; } else if(s.equals("")) s = "%"; else s = s + ":" + "%"; } return s; } void identifytargetareas() { noOfTargets = Integer.parseInt(getParameter("noOfTargets")); String as[] = new String[noOfTargets + 1]; targetNames = new String[noOfTargets + 1]; targetAreas = new Rectangle[noOfTargets + 1]; try { for(int i = 1; i < noOfTargets + 1; i++) { String s2 = "Targetno" + String.valueOf(i); String s = getParameter(s2); as[i] = getParameter(s2); String s1 = as[i].substring(0, as[i].indexOf(":")); as[i] = as[i].substring(as[i].indexOf(":") + 1); s = as[i]; targetNames[i] = as[i].substring(0, as[i].indexOf(":")); s = targetNames[i]; String s3 = as[i].substring(as[i].indexOf(":") + 1); targetAreas[i] = stringToRect(s3, s1); } } catch(Exception _ex) { bug = true; problem = problem + "Target Area Parameter missing or incorrect"; } } public void init() { bug = false; problem = ""; offScreenImg = createImage(getSize().width, getSize().height); offScreenG = offScreenImg.getGraphics(); noOfImages = Integer.parseInt(getParameter("NoOfMainimages")); Imagedetails = new String[noOfImages + 1]; for(int i = 1; i < noOfImages + 1; i++) { String s1 = "Mainimageno" + String.valueOf(i); String s = getParameter(s1); Imagedetails[i] = getParameter(s1); } feedbackInfo = getParameter("feedbackState"); baseType = getParameter("baseType"); markerType = getParameter("markerType"); noOfMarkers = Integer.parseInt(getParameter("noOfMarkers")); noOfMarkers = noOfMarkers * 2; noOfTargets = Integer.parseInt(getParameter("noOfTargets")); if(getParameter("identifiedTargets").equals("TRUE")) targetsIdentified = true; else targetsIdentified = false; try { targetrect = new Rectangle(0, 0); markerLocations = new Point[noOfMarkers + 1]; startLine = getSize().height - border; borderLine = getSize().height - 2 * border; space = getSize().width / (1 + noOfMarkers / 2); if(feedbackInfo.equals("No")) { feedback = false; initializemarkers(); } else { feedback = true; for(int j = 1; j < noOfMarkers + 1; j++) markerLocations[j] = new Point(nopaint, nopaint); settingstr = feedbackInfo.substring(feedbackInfo.indexOf(":") + 1); for(int k = 1; k < noOfMarkers + 1; k++) { boolean flag; if(settingstr.indexOf(":") < 0) { valuestr = settingstr; flag = true; } else { valuestr = settingstr.substring(0, settingstr.indexOf(":")); flag = false; } markerLocations[k].x = Integer.parseInt(valuestr.substring(0, valuestr.indexOf(","))); markerLocations[k].y = Integer.parseInt(valuestr.substring(valuestr.indexOf(",") + 1)); settingstr = settingstr.substring(settingstr.indexOf(":") + 1); if(flag) break; } } } catch(Exception _ex) { bug = true; problem = problem + "Feedback state Parameter missing or incorrect"; } if(targetsIdentified) identifytargetareas(); setBackground(Color.white); } void initializemarkers() { for(int k = 1; k < noOfMarkers + 1; k++) { int i; int j; if(k % 2 == 1) { i = startLine - markerdiameter - markerdiameter / 2; j = (space * (k + 1)) / 2 - markerdiameter / 2; } else { i = (startLine + markerdiameter) - markerdiameter / 2; j = (space * k) / 2 - markerdiameter / 2; } int l = space * k; markerLocations[k] = new Point(j, i); } } public boolean mouseDown(Event event, int i, int j) { for(int k = 1; k < noOfMarkers + 1; k++) { Point point = markerLocations[k]; movetargettopoint(point, markerSize, targetrect); if(targetrect.contains(i, j)) { pickedup = true; currentIndex = k; break; } pickedup = false; } return true; } public boolean mouseDrag(Event event, int i, int j) { int k = i; int l = j; if(pickedup && !feedback) { if(i > getSize().width) k = getSize().width - 5; if(i < 0) k = 5; if(j > getSize().height) l = getSize().height - 5; if(j < 0) l = 5; markerLocations[currentIndex].x = k; markerLocations[currentIndex].y = l; repaint(); } return true; } public boolean mouseUp(Event event, int i, int j) { if(!feedback) showStatus(getValue()); return true; } void movetargettopoint(Point point, Dimension dimension, Rectangle rectangle) { rectangle.setSize(dimension.width, dimension.height); Point point1 = new Point(point.x - dimension.width / 2, point.y - dimension.height / 2); rectangle.setLocation(point1.x, point1.y); } public void paint(Graphics g) { try { offScreenG.setColor(getBackground()); offScreenG.fillRect(0, 0, getSize().width, getSize().height); offScreenG.setColor(getForeground()); int i = getSize().width; int j = getSize().height; for(int k1 = 1; k1 < noOfImages + 1; k1++) { String s = Imagedetails[k1]; String s1 = s.substring(0, s.indexOf(":")); String s2 = s.substring(s.indexOf(":") + 1); String s3 = s2.substring(0, s2.indexOf(":")); s2 = s2.substring(s2.indexOf(":") + 1); String s4 = s2.substring(0, s2.indexOf(":")); s2 = s2.substring(s2.indexOf(":") + 1); String s5 = s2.substring(0, s2.indexOf(":")); s2 = s2.substring(s2.indexOf(":") + 1); String s6 = s2; int k; if(s3.length() == 0) k = 0; else k = Integer.parseInt(s3); int l; if(s4.length() == 0) l = 0; else l = Integer.parseInt(s4); int i1; if(s5.length() == 0) i1 = 0; else i1 = Integer.parseInt(s5); int j1; if(s6.length() == 0) j1 = 0; else j1 = Integer.parseInt(s6); Image image = getImage(getCodeBase(), s1); if(i1 == 0) i1 = image.getWidth(this); if(j1 == 0) j1 = image.getHeight(this); offScreenG.drawImage(image, k, l, i1, j1, this); } for(int l1 = 1; l1 < noOfMarkers + 1; l1++) drawmarkeratpoint(offScreenG, markerLocations[l1], markerSize, l1); offScreenG.drawLine(0, borderLine, getSize().width, borderLine); offScreenG.drawRect(0, 0, getSize().width - 1, getSize().height - 1); g.drawImage(offScreenImg, 0, 0, this); } catch(Exception exception) { bug = true; problem = " Error in paint: probable missing parameters. " + exception.getMessage(); } if(bug) showStatus(problem); } public void reSet() { initializemarkers(); repaint(); } String readpoints() { String s = String.valueOf(markerLocations[1].x) + "," + String.valueOf(markerLocations[1].y); for(int i = 2; i < noOfMarkers + 1; i++) s = s + ":" + String.valueOf(markerLocations[i].x) + "," + String.valueOf(markerLocations[i].y); return s; } Rectangle stringToRect(String s, String s1) { int i = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); s = s.trim(); int j = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); s = s.trim(); Rectangle rectangle; if(s1.equals("circle")) { int k = Integer.parseInt(s); rectangle = new Rectangle(i - k, j - k, 2 * k, 2 * k); } else { int l = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); s = s.trim(); int i1 = Integer.parseInt(s); rectangle = new Rectangle(i, j, l - i, i1 - j); } return rectangle; } public void update(Graphics g) { paint(g); } Point markerLocations[]; Rectangle targetrect; Rectangle targetAreas[]; int noOfMarkers; int noOfTargets; int noOfImages; int markerdiameter; String Imagedetails[]; String targetNames[]; Dimension markerSize; Image defaultMarkerPic; Image markerPic[]; String testImage; Image mainPic[]; Image offScreenImg; Graphics offScreenG; String problem; boolean bug; String feedbackInfo; String baseType; String markerType; String settingstr; String valuestr; boolean feedback; boolean pickedup; boolean targetsIdentified; int currentIndex; int startLine; int border; int borderLine; int space; int nopaint; Font f; String fontname; int fontsize; int strlen; int fontht; int fontdesc; int fontasc; FontMetrics fmetrics; }