/* 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 rorderV2 extends Applet { public rorderV2() { nopaint = -100; fontname = "TimesRoman"; fontsize = 28; } void drawImageatpoint(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 drawstringatpoint(String s, Graphics g, Point point) { String s1 = s; int i = fmetrics.stringWidth(s); g.setFont(f); g.drawString(s, point.x - i / 2, point.y + fontasc / 2); } public String getValue() { String s = "RenderObjectApplet:"; return s + readpoints(); } void identifyobjects() { String as[] = new String[noOfObjects + 1]; objectnames = new String[noOfObjects + 1]; objectidents = new String[noOfObjects + 1]; objectpic = new Image[noOfObjects + 1]; try { for(int i = 1; i < noOfObjects + 1; i++) { String s2 = "ObjectNo" + String.valueOf(i); as[i] = getParameter(s2); String s = as[i].substring(as[i].indexOf(":") + 1); int j = s.indexOf(";"); if(j > 0) s = s.substring(0, s.indexOf(";")); objectnames[i] = s; String s1 = as[i].substring(0, as[i].indexOf(":")); objectidents[i] = s1; if(!textmarkers) objectpic[i] = getImage(getCodeBase(), objectnames[i]); } } catch(Exception _ex) { bug = true; Problem = Problem + "Response 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"); orientation = getParameter("orientation"); objectType = getParameter("objectType"); fontsize = Integer.parseInt(getParameter("fontsize")); if(objectType.equals("Text")) textmarkers = true; else textmarkers = false; if(textmarkers) { f = new Font(fontname, 1, fontsize); fmetrics = getFontMetrics(f); fontht = fmetrics.getHeight(); fontdesc = fmetrics.getDescent(); fontasc = fmetrics.getAscent(); } noOfObjects = Integer.parseInt(getParameter("noOfObjects")); identifyobjects(); try { targetrect = new Rectangle(0, 0); objectpoints = new Point[noOfObjects + 1]; if(orientation.equals("Column")) { space = getSize().height / noOfObjects; startline = getSize().width / 2; } else { space = getSize().width / noOfObjects; startline = getSize().height / 2; } if(feedbackinfo.equals("No")) { feedback = false; initpoints(); } else { feedback = true; for(int i = 1; i < noOfObjects + 1; i++) objectpoints[i] = new Point(nopaint, nopaint); settingstr = feedbackinfo.substring(feedbackinfo.indexOf(":") + 1); settingstr = settingstr.substring(settingstr.indexOf(":") + 1); for(int j = 1; j < noOfObjects + 1; j++) { boolean flag; if(settingstr.indexOf(";") < 0) { valuestr = settingstr; flag = true; } else { valuestr = settingstr.substring(0, settingstr.indexOf(";")); flag = false; } if(orientation.equals("Row")) { objectpoints[j].x = Integer.parseInt(valuestr.substring(valuestr.indexOf(",") + 1)); objectpoints[j].y = startline; } else { objectpoints[j].y = Integer.parseInt(valuestr.substring(valuestr.indexOf(",") + 1)); objectpoints[j].x = startline; } 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 initpoints() { if(orientation.equals("Column")) { int j = startline; objectpoints[1] = new Point(j, space / 2); for(int k = 2; k < noOfObjects + 1; k++) objectpoints[k] = new Point(j, space / 2 + space * (k - 1)); } else { int i = startline; objectpoints[1] = new Point(space / 2, i); for(int l = 2; l < noOfObjects + 1; l++) objectpoints[l] = new Point(space / 2 + space * (l - 1), i); } } public boolean mouseDown(Event event, int i, int j) { try { for(int k = 1; k < noOfObjects + 1; k++) { Dimension dimension; if(!textmarkers) { Image image = objectpic[k]; dimension = new Dimension(image.getWidth(this), image.getHeight(this)); } else { dimension = new Dimension(fmetrics.stringWidth(objectnames[k]), fontht); } Point point = objectpoints[k]; movetargettopoint(point, dimension, targetrect); if(targetrect.contains(i, j)) { pickedup = true; currentindex = k; break; } pickedup = false; } } catch(Exception _ex) { bug = true; Problem = " Error in mousedown."; } if(bug) showStatus(Problem); return true; } public boolean mouseDrag(Event event, int i, int j) { int i1 = i; int j1 = j; if(pickedup && !feedback) { int k; int l; if(!textmarkers) { Image image = objectpic[currentindex]; k = image.getHeight(this); l = image.getWidth(this); } else { k = fontht; l = fmetrics.stringWidth(objectnames[currentindex]); } if(i > getSize().width - l / 2) i1 = getSize().width - l / 2; if(i < l / 2) i1 = l / 2; if(j > getSize().height - k / 2) j1 = getSize().height - k / 2; if(j < k / 2) j1 = k / 2; objectpoints[currentindex].x = i1; objectpoints[currentindex].y = j1; 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()); for(int i = 1; i < noOfObjects + 1; i++) if(!textmarkers) { Image image = objectpic[i]; Dimension dimension = new Dimension(image.getWidth(this), image.getHeight(this)); drawImageatpoint(offscreenG, objectpoints[i], image, dimension); } else { drawstringatpoint(objectnames[i], offscreenG, objectpoints[i]); } g.drawImage(offscreenImg, 0, 0, this); } catch(Exception _ex) { bug = true; Problem = " Error in paint: probable missing parameters."; } if(bug) showStatus(Problem); } public void reSet() { initpoints(); repaint(); } String readpoints() { String s = "R"; if(orientation.equals("Row")) { s = objectidents[1] + "," + String.valueOf(objectpoints[1].x); for(int i = 2; i < noOfObjects + 1; i++) s = s + ";" + objectidents[i] + "," + String.valueOf(objectpoints[i].x); } else { s = objectidents[1] + "," + String.valueOf(objectpoints[1].y); for(int j = 2; j < noOfObjects + 1; j++) s = s + ";" + objectidents[j] + "," + String.valueOf(objectpoints[j].y); } String s4 = s; if(baseType.equals("identifier")) { String as[] = new String[noOfObjects + 1]; for(int k = 1; k < noOfObjects + 1; k++) if(orientation.equals("Row")) as[k] = objectidents[k] + ";" + String.valueOf(objectpoints[k].x); else as[k] = objectidents[k] + ";" + String.valueOf(objectpoints[k].y); for(int l = 1; l < noOfObjects; l++) { for(int j1 = 1; j1 < noOfObjects; j1++) { String s2 = as[j1].substring(as[j1].indexOf(";") + 1); String s3 = as[j1 + 1].substring(as[j1 + 1].indexOf(";") + 1); if(Integer.parseInt(s2) > Integer.parseInt(s3)) { String s1 = as[j1 + 1]; as[j1 + 1] = as[j1]; as[j1] = s1; } } } s = as[1].substring(0, as[1].indexOf(";")); for(int i1 = 2; i1 < noOfObjects + 1; i1++) s = s + "," + as[i1].substring(0, as[i1].indexOf(";")); return s + ":" + s4; } else { return s; } } Rectangle stringToRect(String s) { int i = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); int j = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); int k = Integer.parseInt(s.substring(0, s.indexOf(","))); s = s.substring(s.indexOf(",") + 1); int l = Integer.parseInt(s); Rectangle rectangle = new Rectangle(i, j, k - i, l - j); return rectangle; } public void update(Graphics g) { paint(g); } Point objectpoints[]; Rectangle targetrect; int noOfObjects; String objectnames[]; String objectidents[]; Image objectpic[]; Image offscreenImg; Graphics offscreenG; String Problem; boolean bug; String feedbackinfo; String baseType; String orientation; String objectType; String settingstr; String valuestr; String objectno; boolean feedback; boolean pickedup; int currentindex; int startline; int space; boolean textmarkers; int nopaint; Font f; String fontname; int fontsize; int strlen; int fontht; int fontdesc; int fontasc; FontMetrics fmetrics; }