/* 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 rpairV2 extends Applet { public rpairV2() { border = 20; nopaint = -100; fontname = "TimesRoman"; fontsize = 14; } void drawimageobjectatpoint(Graphics g, Point point, Image image, Dimension dimension) { Point point1 = new Point(point.x - dimension.width / 2, point.y - dimension.height / 2); g.drawImage(image, point1.x, point1.y, this); } void drawobjectatpoint(Graphics g, Point point, int i) { currentobjectSize = new Dimension(objectPic[i].getWidth(this), objectPic[i].getHeight(this)); drawimageobjectatpoint(g, point, objectPic[i], currentobjectSize); } void drawstringatpoint(String s, Graphics g, Point point) { int i = fmetrics.stringWidth(s); g.setFont(f); g.setColor(Color.black); g.drawString(s, point.x - i / 2, point.y + fontasc / 2); } void drawtargets(Graphics g) { initializetargets(); for(int i = 1; i < noOfobjects + 1; i++) { Point point = new Point(targetLocations[i]); g.setColor(Color.black); g.drawRoundRect(targetAreas[i].x, targetAreas[i].y, targetAreas[i].width, targetAreas[i].height, 3, 3); } for(int j = 1; j < noOfobjects / 2 + 1; j++) g.drawLine(targetLocations[j].x + targetobjectSize.width / 2, targetLocations[j].y, targetLocations[j + noOfobjects / 2].x - targetobjectSize.width / 2, targetLocations[j].y); } public String getValue() { String s = "Applet:"; s = s + getnameoftarget() + "; " + readpoints(); return s; } String getnameoftarget() { String s = ""; String s1 = ""; for(int i = 1; i < noOfobjects + 1; i++) { if(objectType.equals("LABELS") || objectType.equals("TEXT")) s1 = objectIDs[i]; Point point = new Point(objectLocations[i].x, objectLocations[i].y); int j = point.x; int k = point.y; for(int l = 1; l < noOfassociations * 2 + 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)) if(s.equals("")) { if(s1.equals("")) s = targetNames[l]; else s = s1 + "," + targetNames[l]; } else { s = s + ":" + s1 + "," + targetNames[l]; } } } return s; } void identifyobjects() { objectLabels = new String[noOfobjects + 1]; objectPicNames = new String[noOfobjects + 1]; objectIDs = new String[noOfobjects + 1]; objectTypes = new String[noOfobjects + 1]; objectmatchMaxs = new String[noOfobjects + 1]; objectSizes = new Dimension[noOfobjects + 1]; objectText = new String[noOfobjects + 1]; try { if(objectType.equals("TEXT")) { for(int i = 1; i < noOfobjects + 1; i++) { String s = "labelNo" + String.valueOf(i); objectLabels[i] = getParameter(s); String s2 = objectLabels[i]; objectIDs[i] = s2.substring(0, s2.indexOf(":")); s2 = s2.substring(s2.indexOf(":") + 1); objectText[i] = s2.substring(0, s2.indexOf(":")); objectmatchMaxs[i] = s2.substring(s2.indexOf(":") + 1); } } else { for(int j = 1; j < noOfobjects + 1; j++) { String s1 = "labelNo" + String.valueOf(j); objectLabels[j] = getParameter(s1); String s3 = objectLabels[j]; objectIDs[j] = s3.substring(0, s3.indexOf(":")); s3 = s3.substring(s3.indexOf(":") + 1); objectTypes[j] = s3.substring(0, s3.indexOf(":")); s3 = s3.substring(s3.indexOf(":") + 1); objectPicNames[j] = s3.substring(0, s3.indexOf(":")); s3 = s3.substring(s3.indexOf(":") + 1); String s4 = s3.substring(0, s3.indexOf(":")); s3 = s3.substring(s3.indexOf(":") + 1); String s5 = s3.substring(0, s3.indexOf(":")); objectSizes[j] = new Dimension(Integer.parseInt(s4), Integer.parseInt(s5)); s3 = s3.substring(s3.indexOf(":") + 1); objectmatchMaxs[j] = s3.substring(0, s3.indexOf(":")); } } } catch(Exception _ex) { bug = true; problem = problem + "object label Parameter missing or incorrect"; } } public void init() { bug = false; problem = ""; offScreenImg = createImage(getSize().width, getSize().height); offScreenG = offScreenImg.getGraphics(); feedbackInfo = getParameter("feedbackState"); baseType = getParameter("baseType"); objectType = getParameter("objectType"); noOfobjects = Integer.parseInt(getParameter("noOfobjects")); noOfassociations = Integer.parseInt(getParameter("noOfassociations")); if(objectType.equals("LABELS")) { identifyobjects(); objectPic = new Image[noOfobjects + 1]; for(int i = 1; i < noOfobjects + 1; i++) { String s1 = objectPicNames[i]; objectPic[i] = getImage(getCodeBase(), s1); } int j1 = 0; int l1 = 0; for(int j = 1; j < noOfobjects + 1; j++) { if(objectSizes[j].width > j1) j1 = objectSizes[j].width; if(objectSizes[j].height > l1) l1 = objectSizes[j].height; } biggestImageobjectSize = new Dimension(j1, l1); targetobjectSize = biggestImageobjectSize; } if(objectType.equals("TEXT")) { identifyobjects(); f = new Font(fontname, 1, fontsize); fmetrics = getFontMetrics(f); fontht = fmetrics.getHeight(); fontdesc = fmetrics.getDescent(); fontasc = fmetrics.getAscent(); int k1 = 1; String s2 = ""; for(int k = 1; k < noOfobjects + 1; k++) { String s = objectText[k]; int i2 = objectText[k].length(); if(i2 > k1) { k1 = i2; s2 = objectText[k]; } } strlen = fmetrics.stringWidth(s2); biggestTextobjectSize = new Dimension(strlen + 10, fontht); targetobjectSize = biggestTextobjectSize; int j2 = targetobjectSize.width; int k2 = targetobjectSize.height; } try { targetrect = new Rectangle(0, 0); objectLocations = new Point[noOfobjects + 1]; startLine = border; borderLine = 2 * border; space = getSize().width / (1 + noOfobjects); if(feedbackInfo.equals("No")) { feedback = false; initializeobjects(); } else { feedback = true; for(int l = 1; l < noOfobjects + 1; l++) objectLocations[l] = new Point(nopaint, nopaint); settingstr = feedbackInfo; settingstr = feedbackInfo.substring(feedbackInfo.indexOf(":") + 1); for(int i1 = 1; i1 < noOfobjects + 1; i1++) { boolean flag; if(settingstr.indexOf(":") < 0) { valuestr = settingstr; flag = true; } else { valuestr = settingstr.substring(0, settingstr.indexOf(":")); flag = false; } objectLocations[i1].x = Integer.parseInt(valuestr.substring(0, valuestr.indexOf(","))); objectLocations[i1].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"; } setBackground(Color.white); } void initializeobjects() { for(int j = 1; j < noOfobjects + 1; j++) { int i; if(noOfobjects > 5) { if(j % 2 == 0) i = startLine + 10; else i = startLine - 10; } else { i = startLine; } int k = space * j; objectLocations[j] = new Point(space * j, i); } } void initializetargets() { int j1 = targetobjectSize.width / 2; int k1 = targetobjectSize.height / 2; int i1 = getSize().height - border; targetLocations = new Point[noOfobjects + 1]; targetAreas = new Rectangle[noOfobjects + 1]; targetNames = new String[noOfobjects + 1]; int l = i1 / (1 + noOfobjects / 2); int k = getSize().width / 3; for(int l1 = 1; l1 < noOfassociations + 1; l1++) { int i = k; targetLocations[l1] = new Point(i, l * l1 + borderLine); targetNames[l1] = String.valueOf(l1); } for(int i2 = 1; i2 < noOfassociations + 1; i2++) { int j = k + k; targetLocations[i2 + noOfassociations] = new Point(j, l * i2 + borderLine); targetNames[i2 + noOfassociations] = String.valueOf(i2); } for(int j2 = 1; j2 < noOfassociations * 2 + 1; j2++) targetAreas[j2] = new Rectangle(targetLocations[j2].x - j1, targetLocations[j2].y - k1, j1 * 2, k1 * 2); } public boolean mouseDown(Event event, int i, int j) { for(int k = 1; k < noOfobjects + 1; k++) { Point point = objectLocations[k]; Dimension dimension; if(objectType.equals("TEXT")) dimension = biggestTextobjectSize; else dimension = objectSizes[k]; movetargettopoint(point, dimension, 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; objectLocations[currentIndex].x = k; objectLocations[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()); drawtargets(offScreenG); if(objectType.equals("TEXT")) { for(int i = 1; i < noOfobjects + 1; i++) drawstringatpoint(objectText[i], offScreenG, objectLocations[i]); } else { for(int j = 1; j < noOfobjects + 1; j++) drawobjectatpoint(offScreenG, objectLocations[j], j); } 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() { initializeobjects(); repaint(); } String readpoints() { String s = String.valueOf(objectLocations[1].x) + "," + String.valueOf(objectLocations[1].y); for(int i = 2; i < noOfobjects + 1; i++) s = s + ":" + String.valueOf(objectLocations[i].x) + "," + String.valueOf(objectLocations[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 objectLocations[]; Point targetLocations[]; Rectangle targetrect; Rectangle targetAreas[]; int noOfobjects; int noOfassociations; String Imagedetails[]; String targetNames[]; String objectLabels[]; String objectPicNames[]; String objectIDs[]; String objectTypes[]; String objectText[]; String objectmatchMaxs[]; Dimension objectSizes[]; Dimension biggestTextobjectSize; Dimension currentobjectSize; Dimension targetobjectSize; Dimension biggestImageobjectSize; Image objectPic[]; Image offScreenImg; Graphics offScreenG; String problem; boolean bug; String feedbackInfo; String baseType; String objectType; String settingstr; String valuestr; boolean feedback; boolean pickedup; 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; }