/** * Reads image meta data. * * @param oimage * @return */ public static Map<String, String> readImageData(IIOImage oimage) { Map<String, String> dict = new HashMap<String, String>(); IIOMetadata imageMetadata = oimage.getMetadata(); if (imageMetadata != null) { IIOMetadataNode dimNode = (IIOMetadataNode) imageMetadata.getAsTree("javax_imageio_1.0"); NodeList nodes = dimNode.getElementsByTagName("HorizontalPixelSize"); int dpiX; if (nodes.getLength() > 0) { float dpcWidth = Float.parseFloat(nodes.item(0).getAttributes().item(0).getNodeValue()); dpiX = (int) Math.round(25.4f / dpcWidth); } else { dpiX = Toolkit.getDefaultToolkit().getScreenResolution(); } dict.put("dpiX", String.valueOf(dpiX)); nodes = dimNode.getElementsByTagName("VerticalPixelSize"); int dpiY; if (nodes.getLength() > 0) { float dpcHeight = Float.parseFloat(nodes.item(0).getAttributes().item(0).getNodeValue()); dpiY = (int) Math.round(25.4f / dpcHeight); } else { dpiY = Toolkit.getDefaultToolkit().getScreenResolution(); } dict.put("dpiY", String.valueOf(dpiY)); } return dict; }
public void printLoop(int n, float x[], float y[]) { // Need to swap the y component Polygon P = new Polygon(); for (int i = 0; i < n; i++) P.addPoint(Math.round(x[i]), Math.round(height - y[i])); poly_draw.add(P); poly_draw_color.add(curColor); }
/** * Zooms out by making the source rectangle (srcRect) larger and centering it on (x,y). If we * can't make it larger, then make the window smaller. */ public void zoomOut(int x, int y) { if (magnification <= 0.03125) return; double oldMag = magnification; double newMag = getLowerZoomLevel(magnification); double srcRatio = (double) srcRect.width / srcRect.height; double imageRatio = (double) imageWidth / imageHeight; double initialMag = imp.getWindow().getInitialMagnification(); if (Math.abs(srcRatio - imageRatio) > 0.05) { double scale = oldMag / newMag; int newSrcWidth = (int) Math.round(srcRect.width * scale); int newSrcHeight = (int) Math.round(srcRect.height * scale); if (newSrcWidth > imageWidth) newSrcWidth = imageWidth; if (newSrcHeight > imageHeight) newSrcHeight = imageHeight; int newSrcX = srcRect.x - (newSrcWidth - srcRect.width) / 2; int newSrcY = srcRect.y - (newSrcHeight - srcRect.height) / 2; if (newSrcX < 0) newSrcX = 0; if (newSrcY < 0) newSrcY = 0; srcRect = new Rectangle(newSrcX, newSrcY, newSrcWidth, newSrcHeight); // IJ.log(newMag+" "+srcRect+" "+dstWidth+" "+dstHeight); int newDstWidth = (int) (srcRect.width * newMag); int newDstHeight = (int) (srcRect.height * newMag); setMagnification(newMag); setMaxBounds(); // IJ.log(newDstWidth+" "+dstWidth+" "+newDstHeight+" "+dstHeight); if (newDstWidth < dstWidth || newDstHeight < dstHeight) { // IJ.log("pack"); setDrawingSize(newDstWidth, newDstHeight); imp.getWindow().pack(); } else repaint(); return; } if (imageWidth * newMag > dstWidth) { int w = (int) Math.round(dstWidth / newMag); if (w * newMag < dstWidth) w++; int h = (int) Math.round(dstHeight / newMag); if (h * newMag < dstHeight) h++; x = offScreenX(x); y = offScreenY(y); Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h); if (r.x < 0) r.x = 0; if (r.y < 0) r.y = 0; if (r.x + w > imageWidth) r.x = imageWidth - w; if (r.y + h > imageHeight) r.y = imageHeight - h; srcRect = r; } else { srcRect = new Rectangle(0, 0, imageWidth, imageHeight); setDrawingSize((int) (imageWidth * newMag), (int) (imageHeight * newMag)); // setDrawingSize(dstWidth/2, dstHeight/2); imp.getWindow().pack(); } // IJ.write(newMag + " " + srcRect.x+" "+srcRect.y+" "+srcRect.width+" "+srcRect.height+" // "+dstWidth + " " + dstHeight); setMagnification(newMag); // IJ.write(srcRect.x + " " + srcRect.width + " " + dstWidth); setMaxBounds(); repaint(); }
void ajustarYCentrar(JComponent porAjustar, JComponent ajustador) { // Se agregan 30 de alto y 6 de ancho por adornos de la ventana porAjustar.setSize( ajustador.getMinimumSize().getSize().width + 16, ajustador.getMinimumSize().getSize().height + 44); int x = Math.round(Principal.sysAncho / 2) - Math.round(porAjustar.getBounds().width / 2); int y = Math.round(Principal.sysAlto / 2) - Math.round(porAjustar.getBounds().height / 2); porAjustar.setLocation(x, y); }
void handleMouseMove(int sx, int sy) { // Do rubber banding int tool = Toolbar.getToolId(); if (!(tool == Toolbar.POLYGON || tool == Toolbar.POLYLINE || tool == Toolbar.ANGLE)) { imp.deleteRoi(); imp.draw(); return; } drawRubberBand(sx, sy); degrees = Double.NaN; double len = -1; if (nPoints > 1) { double x1, y1, x2, y2; if (xpf != null) { x1 = xpf[nPoints - 2]; y1 = ypf[nPoints - 2]; x2 = xpf[nPoints - 1]; y2 = ypf[nPoints - 1]; } else { x1 = xp[nPoints - 2]; y1 = yp[nPoints - 2]; x2 = xp[nPoints - 1]; y2 = yp[nPoints - 1]; } degrees = getAngle( (int) Math.round(x1), (int) Math.round(y1), (int) Math.round(x2), (int) Math.round(y2)); if (tool != Toolbar.ANGLE) { Calibration cal = imp.getCalibration(); double pw = cal.pixelWidth, ph = cal.pixelHeight; if (IJ.altKeyDown()) { pw = 1.0; ph = 1.0; } len = Math.sqrt((x2 - x1) * pw * (x2 - x1) * pw + (y2 - y1) * ph * (y2 - y1) * ph); } } if (tool == Toolbar.ANGLE) { if (nPoints == 2) angle1 = degrees; else if (nPoints == 3) { double angle2 = getAngle(xp[1], yp[1], xp[2], yp[2]); degrees = Math.abs(180 - Math.abs(angle1 - angle2)); if (degrees > 180.0) degrees = 360.0 - degrees; } } String length = len != -1 ? ", length=" + IJ.d2s(len) : ""; double degrees2 = tool == Toolbar.ANGLE && nPoints == 3 && Prefs.reflexAngle ? 360.0 - degrees : degrees; String angle = !Double.isNaN(degrees) ? ", angle=" + IJ.d2s(degrees2) : ""; int ox = ic != null ? ic.offScreenX(sx) : sx; int oy = ic != null ? ic.offScreenY(sy) : sy; IJ.showStatus(imp.getLocationAsString(ox, oy) + length + angle); }
/** * Draw picture (gif, jpg, or png) centered on (x, y). * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @throws RuntimeException if the image is corrupt */ public static void picture(double x, double y, String s) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); int ws = image.getWidth(null); int hs = image.getHeight(null); if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt"); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null); draw(); }
public static BufferedImage scaleImage(BufferedImage bi, double scale) { int w1 = (int) (Math.round(scale * bi.getWidth())); int h1 = (int) (Math.round(scale * bi.getHeight())); BufferedImage image = new BufferedImage(w1, h1, BufferedImage.TYPE_INT_RGB); Graphics2D g2 = image.createGraphics(); g2.setRenderingHint( RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); g2.setPaint(Color.white); g2.fillRect(0, 0, w1, h1); g2.drawImage(bi, 0, 0, w1, h1, null); // this); g2.dispose(); return image; }
/** * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @param degrees is the number of degrees to rotate counterclockwise * @throws IllegalArgumentException if the image is corrupt */ public static void picture(double x, double y, String s, double degrees) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); int ws = image.getWidth(null); int hs = image.getHeight(null); if (ws < 0 || hs < 0) throw new IllegalArgumentException("image " + s + " is corrupt"); offscreen.rotate(Math.toRadians(-degrees), xs, ys); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), null); offscreen.rotate(Math.toRadians(+degrees), xs, ys); draw(); }
/** Frees memory by deleting a few slices from the end of the stack. */ public void trim() { int n = (int) Math.round(Math.log(nSlices) + 1.0); for (int i = 0; i < n; i++) { deleteLastSlice(); System.gc(); } }
void adjustSourceRect(double newMag, int x, int y) { // IJ.log("adjustSourceRect1: "+newMag+" "+dstWidth+" "+dstHeight); int w = (int) Math.round(dstWidth / newMag); if (w * newMag < dstWidth) w++; int h = (int) Math.round(dstHeight / newMag); if (h * newMag < dstHeight) h++; x = offScreenX(x); y = offScreenY(y); Rectangle r = new Rectangle(x - w / 2, y - h / 2, w, h); if (r.x < 0) r.x = 0; if (r.y < 0) r.y = 0; if (r.x + w > imageWidth) r.x = imageWidth - w; if (r.y + h > imageHeight) r.y = imageHeight - h; srcRect = r; setMagnification(newMag); // IJ.log("adjustSourceRect2: "+srcRect+" "+dstWidth+" "+dstHeight); }
/** Prototype give player a leveled up look */ public void Opify(double Fac) { for (int i = 0; i < imgCHARAC.getWidth(); i++) { for (int j = 0; j < imgCHARAC.getHeight(); j++) { if (imgCHARAC.getRGB(i, j) != imgCHARAC.getRGB(2, 2)) { imgCHARAC.setRGB(i, j, (int) Math.round(imgCHARAC.getRGB(i, j) * Fac)); } } } }
/** Draws an outline of this OvalRoi on the image. */ public void drawPixels(ImageProcessor ip) { Polygon p = getPolygon(); if (p.npoints > 0) { int saveWidth = ip.getLineWidth(); if (getStrokeWidth() > 1f) ip.setLineWidth((int) Math.round(getStrokeWidth())); ip.drawPolygon(p); ip.setLineWidth(saveWidth); } if (Line.getWidth() > 1 || getStrokeWidth() > 1) updateFullWindow = true; }
/** * Draw picture (gif, jpg, or png) centered on (x, y), rescaled to w-by-h. * * @param x the center x coordinate of the image * @param y the center y coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @param w the width of the image * @param h the height of the image * @throws RuntimeException if the width height are negative * @throws RuntimeException if the image is corrupt */ public static void picture(double x, double y, String s, double w, double h) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); if (w < 0) throw new RuntimeException("width is negative: " + w); if (h < 0) throw new RuntimeException("height is negative: " + h); double ws = factorX(w); double hs = factorY(h); if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt"); if (ws <= 1 && hs <= 1) pixel(x, y); else { offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), (int) Math.round(ws), (int) Math.round(hs), null); } draw(); }
/** * Draw picture (gif, jpg, or png) centered on (x, y), rotated given number of degrees, rescaled * to w-by-h. * * @param x the center x-coordinate of the image * @param y the center y-coordinate of the image * @param s the name of the image/picture, e.g., "ball.gif" * @param w the width of the image * @param h the height of the image * @param degrees is the number of degrees to rotate counterclockwise * @throws RuntimeException if the image is corrupt */ public static void picture(double x, double y, String s, double w, double h, double degrees) { Image image = getImage(s); double xs = scaleX(x); double ys = scaleY(y); double ws = factorX(w); double hs = factorY(h); if (ws < 0 || hs < 0) throw new RuntimeException("image " + s + " is corrupt"); if (ws <= 1 && hs <= 1) pixel(x, y); offscreen.rotate(Math.toRadians(-degrees), xs, ys); offscreen.drawImage( image, (int) Math.round(xs - ws / 2.0), (int) Math.round(ys - hs / 2.0), (int) Math.round(ws), (int) Math.round(hs), null); offscreen.rotate(Math.toRadians(+degrees), xs, ys); draw(); }
/* * This is the method for quantizing a block DCT'ed with forwardDCTExtreme * This method quantitizes data and rounds it to the nearest integer. */ public int[] quantizeBlockExtreme(double[][] inputData, int code) { int[] outputData = new int[N * N]; int i; int j; int index; index = 0; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { outputData[index] = (int) (Math.round(inputData[i][j] / (double) (((int[]) (quantum[code]))[index]))); index++; } } return outputData; }
/* * This method quantitizes data and rounds it to the nearest integer. */ public int[] quantizeBlock(double[][] inputData, int code) { int[] outputData = new int[N * N]; int i; int j; int index; index = 0; for (i = 0; i < 8; i++) { for (j = 0; j < 8; j++) { // The second line results in significantly better compression. outputData[index] = (int) (Math.round(inputData[i][j] * (((double[]) (Divisors[code]))[index]))); // outputData[index] = (int)(((inputData[i][j] * (((double[]) // (Divisors[code]))[index])) + 16384.5) -16384); index++; } } return outputData; }
public void tick(double dt) { if (updt) { nw = 0; int aw = 0; for (TypeMod m : mods) { if (m.rn == null) { try { BufferedImage img = m.t.get().layer(Resource.imgc).img; String nm = m.t.get().layer(Resource.tooltip).t; Text rt = tnf.render(nm); int h = Inventory.sqsz.y; BufferedImage buf = TexI.mkbuf(new Coord(img.getWidth() + 10 + rt.sz().x, h)); Graphics g = buf.getGraphics(); g.drawImage(img, 0, (h - img.getHeight()) / 2, null); g.drawImage(rt.img, img.getWidth() + 10, (h - rt.sz().y) / 2, null); g.dispose(); m.rn = new TexI(rasterimg(blurmask2(buf.getRaster(), 2, 1, new Color(32, 0, 0)))); m.rh = new TexI(rasterimg(blurmask2(buf.getRaster(), 2, 1, new Color(192, 128, 0)))); } catch (Loading l) { } } if (m.ra == null) { Text rt = tnf.render( (int) Math.round(m.a * 100) + "%", new Color(255, (int) (255 * m.a), (int) (255 * m.a))); m.ra = new TexI(rasterimg(blurmask2(rt.img.getRaster(), 2, 1, new Color(0, 0, 0)))); } nw = Math.max(nw, m.rn.sz().x); aw = Math.max(aw, m.ra.sz().x); } int h = (Inventory.sqsz.y + 5) * mods.size(); h += levels.sz().y + 20; resize(new Coord(Math.max(nw + 20 + aw, boxsz.x), h)); this.c = Gobble.this.parentpos(parent).add(boxc).add(0, boxsz.y + 5); updt = false; } }
public void printPoly(int n, float x[], float y[]) { Polygon P = new Polygon(); for (int i = 0; i < n; i++) P.addPoint(Math.round(x[i]), Math.round(height - y[i])); poly_fill.add(P); poly_fill_color.add(curColor); }
/** * Sets frame rate in frames per second. Equivalent to <code>setDelay(1000/fps)</code>. * * @param fps float frame rate (frames per second) */ public void setFrameRate(float fps) { if (fps != 0f) { delay = Math.round(100f / fps); } }
/** * Sets the delay time between each frame, or changes it for subsequent frames (applies to last * frame added). * * @param ms int delay time in milliseconds */ public void setDelay(int ms) { delay = Math.round(ms / 10.0f); }
/** * Constructor to create a character * * @param x X Position * @param y Y Position * @param w Width of Image * @param h Height of Image * @param FileN Name of image file * @param S Max number of frames */ public GameCharacter( double x, double y, double w, double h, String FileN, int S, int Ty, String c) { // Set position and size X = x; Y = y; W = w; H = h; BASIC_PROCESS = new ProgressBar(new Font("Arial", 36, 36), this); BASIC_EFFECTS = new ProgressBar(new Font("Arial", 36, 36), this); // Add magic attacks // Fireball for (int i = 0; i < FIREBALL.length; i++) { FIREBALL[i] = new MagicAttack(-500, -500, 39, 41, "Fireball.png", 3, 1, 2, "Fireball"); FIREBALL[i].STATS.setStats(new String[] {"Damage=10", "Points=10"}); FIREBALL[i].CASTER = this; } // Shock for (int i = 0; i < SHOCK.length; i++) { SHOCK[i] = new MagicAttack(-500, -500, 39, 41, "Shock.png", 2, 1, 0, "Shock"); SHOCK[i].STATS.setStats(new String[] {"Damage=20", "Points=15"}); SHOCK[i].CASTER = this; } // Darkness for (int i = 0; i < DARKNESS.length; i++) { DARKNESS[i] = new MagicAttack(-500, -500, 165, 164, "Darkness.png", 3, 1, 2, "Darkness"); DARKNESS[i].STATS.setStats(new String[] {"Damage=100", "Points=50"}); DARKNESS[i].CASTER = this; } // Life Drain for (int i = 0; i < LIFE_DRAIN.length; i++) { LIFE_DRAIN[i] = new MagicAttack(-500, -500, 32, 32, "Life Drain.png", 7, 1, 0, "Life Drain"); LIFE_DRAIN[i].STATS.setStats(new String[] {"Damage=50", "Points=25"}); LIFE_DRAIN[i].CASTER = this; } // Get Image try { if (isJar()) { // Character imgCHARAC = getImage("/Graphics/Character/", FileN); // Blood int BloodType = (int) Math.round(Math.random() * 3) + 1; imgBLOOD = getImage("/Graphics/Effects/", "Dead" + BloodType + ".png"); // Quest imgQStart = getImage("/Graphics/Effects/", "Quest_Start.png"); imgQEnd = getImage("/Graphics/Effects/", "Quest_Complete.png"); } else { // Character imgCHARAC = ImageIO.read(Paths.get(DIRECT + FileN).toFile()); // Blood int BloodType = (int) Math.round(Math.random() * 3) + 1; imgBLOOD = ImageIO.read(Paths.get(DIRECT2 + "Dead" + BloodType + ".png").toFile()); // Quest imgQStart = ImageIO.read(Paths.get(DIRECT2 + "Quest_Start.png").toFile()); imgQEnd = ImageIO.read(Paths.get(DIRECT2 + "Quest_Complete.png").toFile()); } } catch (Exception e) { } // Max number of frames SIZE = S; // Sprite type TYPE = Ty; // Assign class CLASS = c; Cl = c; // Add items and attacks according to class if (CLASS.equals("Player")) { // Add items INVENTORY.addItem( "Health Potion", 25, 1, "Health Pot.png", "Potion", new String[] {"Points=50"}); INVENTORY.addItem( "Rusted Dagger", 20, 1, "Dagger_4.png", "Meele", new String[] {"Damage=10", "Attack Speed=1"}); INVENTORY.addItem( "Wooden Staff", 20, 1, "Staff_1.png", "Meele", new String[] {"Damage=5", "Attack Speed=1"}); // Equip items INVENTORY.ItemEffect(1, this); // MEELE_WEAPON=null; // Assign Magic SPELL_LIST.add(FIREBALL); // Inventory type INVENTORY.Type = "Player"; } else if (CLASS.equals("Citizen")) { // Add items INVENTORY.addItem( "Health Potion", 25, 1, "Health Pot.png", "Potion", new String[] {"Points=50"}); // Add Ai NAI = new NPCAI(this); // Add Gold this.GOLD = (int) Math.round(Math.random() * 15 + 5); INVENTORY.Type = "Friendly"; } else if (CLASS.equals("Blacksmith")) { // Add items INVENTORY.addItem( "Silver Dagger", 250, 1, "Dagger_3.png", "Meele", new String[] {"Damage=10", "Attack Speed=1"}); INVENTORY.addItem( "Steel Dagger", 450, 1, "Dagger_5.png", "Meele", new String[] {"Damage=35", "Attack Speed=1"}); // INVENTORY.addItem("Assassin blade", 750,1, "Dagger_6.png","Meele",new // String[]{"Damage=45","Attack Speed=1"}); // INVENTORY.addItem("Serpent Dagger", 5000,1, "Dagger_2.png","Meele",new // String[]{"Damage=50","Attack Speed=1"}); // INVENTORY.addItem("Dagger of Time", 5050,1, "Dagger_1.png","Meele",new // String[]{"Damage=75","Attack Speed=1"}); INVENTORY.addItem( "Steel Sword", 450, 1, "Sword_1.png", "Meele", new String[] {"Damage=30", "Attack Speed=0.65"}); INVENTORY.addItem( "Iron Sword", 50, 1, "Sword_2.png", "Meele", new String[] {"Damage=15", "Attack Speed=0.65"}); INVENTORY.addItem( "Silver Sword", 100, 1, "Sword_5.png", "Meele", new String[] {"Damage=20", "Attack Speed=0.5"}); // INVENTORY.addItem("Iron Scimitar", 150,1, "Sword_7.png","Meele",new // String[]{"Damage=15","Attack Speed=0.75"}); // INVENTORY.addItem("Steel Scimitar", 500,1, "Sword_4.png","Meele",new // String[]{"Damage=50","Attack Speed=0.75"}); // INVENTORY.addItem("Steel Katana", 450,1, "Sword_6.png","Meele",new // String[]{"Damage=40","Attack Speed=0.95"}); // INVENTORY.addItem("Butcher's Sword", 5000,1, "Sword_3.png","Meele",new // String[]{"Damage=100","Attack Speed=0.55"}); // INVENTORY.addItem("Blood Thirster", 6000,1, "Sword_8.png","Meele",new // String[]{"Damage=200","Attack Speed=0.75"}); INVENTORY.addItem( "Iron Hammer", 150, 1, "Hammer_1.png", "Meele", new String[] {"Damage=40", "Attack Speed=0.15"}); // INVENTORY.addItem("Steel Hammer", 450,1, "Hammer_0.png","Meele",new // String[]{"Damage=60","Attack Speed=0.15"}); // INVENTORY.addItem("Iron Mace", 50,1, "Mace_1.png","Meele",new String[]{"Damage=15","Attack // Speed=0.5"}); INVENTORY.addItem("Steel Helmet", 250, 1, "Head_1.png", "H_armor", new String[] {"Armor=20"}); INVENTORY.addItem("Iron Helmet", 150, 1, "Head_2.png", "H_armor", new String[] {"Armor=5"}); // INVENTORY.addItem("Iron Horn Helmet", 350,1, "Head_6.png","H_armor",new // String[]{"Armor=50","Magic Resist=0"}); // INVENTORY.addItem("Steel Horn Helmet", 500,1, "Head_7.png","H_armor",new // String[]{"Armor=80","Magic Resist=0"}); // INVENTORY.addItem("Skysteel Helmet", 4000,1, "Head_4.png","H_armor",new // String[]{"Armor=60","Magic Resist=25"}); INVENTORY.addItem( "Iron Cuirass", 250, 1, "Chest_4.png", "C_armor", new String[] {"Armor=20"}); INVENTORY.addItem( "Steel Cuirass", 350, 1, "Chest_1.png", "C_armor", new String[] {"Armor=30"}); // INVENTORY.addItem("Scale Cuirass", 550,1, "Chest_3.png","C_armor",new // String[]{"Armor=50"}); // INVENTORY.addItem("Dark metal Cuirass", 750,1, "Chest_6.png","C_armor",new // String[]{"Armor=70"}); // INVENTORY.addItem("Master Cuirass", 3050,1, "Chest_5.png","C_armor",new // String[]{"Armor=80","Magic Resist=25"}); // INVENTORY.addItem("Legendary Cuirass", 3050,1, "Chest_2.png","C_armor",new // String[]{"Armor=100","Magic Resist=100"}); INVENTORY.addItem( "Wooden Shield", 50, 1, "Shield_1.png", "Shield", new String[] {"Armor=5", "Magic Resist=0"}); // Add AI NAI = new NPCAI(this); // Identify as trader INVENTORY.Type = "Trader"; // Set Stats STATS.setStats(new String[] {"Level = 5 "}); MAX_HEALTH = 200; HEALTH = (int) MAX_HEALTH; } else if (CLASS.equals("Alchemist")) { // Add Items INVENTORY.addItem( "Health Potion", 25, 50, "Health Pot.png", "Potion", new String[] {"Points=50"}); INVENTORY.addItem( "Mana Potion", 20, 50, "Mana Pot.png", "Potion", new String[] {"Points=50"}); INVENTORY.addItem( "Speed Potion", 10, 50, "Speed Pot.png", "Potion", new String[] {"Points=5"}); // INVENTORY.addItem("Invisib Potion", 50, 10, "Invisibility Pot.png","Potion",new // String[]{"Points=5"}); // Add AI NAI = new NPCAI(this); // Identify as trader INVENTORY.Type = "Trader"; } else if (CLASS.equals("Inn Keeper")) { // Add Items INVENTORY.addItem("Roasted Fish", 15, 10, "Fish.png", "Food", new String[] {"Points=5"}); INVENTORY.addItem("Apple", 15, 10, "Apple.png", "Food", new String[] {"Points=2"}); // Add AI NAI = new NPCAI(this); // Identify as trader INVENTORY.Type = "Trader"; } else if (CLASS.equals("Mage")) { INVENTORY.addItem( "Leather Cap", 250, 1, "Head_8.png", "H_armor", new String[] {"Armor=5", "Magic Resist=25"}); INVENTORY.addItem( "Dark Leather Cap", 300, 1, "Head_9.png", "H_armor", new String[] {"Armor=5", "Magic Resist=50"}); // INVENTORY.addItem("Jesters Cap", 500,1, "Head_5.png","H_armor",new // String[]{"Armor=10","Magic Resist=90"}); // INVENTORY.addItem("Skull Helmet", 5000,1, "Head_3.png","H_armor",new // String[]{"Armor=100","Magic Resist=100"}); INVENTORY.addItem( "Shock Spell Stone", 250, 1, "Stone_1.png", "SpellStoner", new String[] {"Damage=" + SHOCK[0].STATS.DAMAGE}); // INVENTORY.addItem("Darkness Spell Stone", 500,1, "Stone_1.png","SpellStoner",new // String[]{"Damage="+DARKNESS[0].STATS.DAMAGE}); INVENTORY.addItem( "Life Drain Spell Stone", 300, 1, "Stone_1.png", "SpellStoner", new String[] {"Damage=" + LIFE_DRAIN[0].STATS.DAMAGE}); // Add AI NAI = new NPCAI(this); // Identify as trader INVENTORY.Type = "Trader"; } else if (CLASS.equals("Skeleton")) { // Add items INVENTORY.addItem( "Bone Club", 5, 1, "Mace_2.png", "Meele", new String[] {"Damage=5", "Attack Speed=1"}); // Add Gold this.GOLD = (int) Math.round(Math.random() * 10 + 2); // Use Item INVENTORY.ItemEffect(0, this); // Add AI EAI = new EnemyAI(this); } else if (CLASS.equals("Skeleton Chieftan")) { // Add Item INVENTORY.addItem( "Iron Sword", 50, 1, "Sword_2.png", "Meele", new String[] {"Damage=15", "Attack Speed=0.65"}); INVENTORY.addItem( "Health Potion", 25, 1, "Health Pot.png", "Potion", new String[] {"Points=50"}); // Add Gold this.GOLD = (int) Math.round(Math.random() * 50 + 25); // Use Item INVENTORY.ItemEffect(0, this); // Assign Stats STATS.LEVEL = 3; HEALTH = 250; MAX_HEALTH = 250; // Opify Opify(1 / 1.25); // Add AI EAI = new EnemyAI(this); } else if (CLASS.equals("Shaman")) { // Add items INVENTORY.addItem( "Health Potion", 25, 1, "Health Pot.png", "Potion", new String[] {"Points=50"}); // Add Ai NAI = new NPCAI(this); } else if (CLASS.equals("Dark Elf")) { // Add items INVENTORY.addItem( "Rusted Dagger", 20, 1, "Dagger_4.png", "Meele", new String[] {"Damage=10", "Attack Speed=1"}); INVENTORY.addItem("Iron Helmet", 150, 1, "Head_2.png", "H_armor", new String[] {"Armor=5"}); // Assign Stats STATS.LEVEL = 2; HEALTH = 150; MAX_HEALTH = 150; // Add Gold this.GOLD = (int) Math.round(Math.random() * 15 + 2); // Use Item INVENTORY.ItemEffect(0, this); INVENTORY.ItemEffect(1, this); // Add Ai EAI = new EnemyAI(this); } else if (CLASS.equals("Prisoner")) { INVENTORY.addItem("Key", 0, 1, "Key.png", "Key", new String[] {}); // NAI= new NPCAI(this); } }
public void actionPerformed(ActionEvent e) { if (e.getSource() == b) { JFrame frame2 = new JFrame(); JFileChooser chooser = new JFileChooser("."); int option = chooser.showOpenDialog( frame2); // parentComponent must a component like JFrame, JDialog... if (option == JFileChooser.APPROVE_OPTION) { File selectedFile = chooser.getSelectedFile(); path = selectedFile.getAbsolutePath(); } try { loadImage(); } catch (IOException ioe) { System.out.println(ioe.getMessage()); } } else if (e.getSource() == c) { gaussianBlur(); // filtered = grayscale.filter(filtered, null); sobel(); // thinImage(); nonMax(); float lowThreshold = 2.5f; float highThreshold = 7.5f; int low = Math.round(lowThreshold * MAGNITUDE_SCALE); int high = Math.round(highThreshold * MAGNITUDE_SCALE); performHysteresis(low, high); thresholdEdges(); writeEdges(data); Hough(); ImageIcon icon1 = new ImageIcon(res); lbl1.setIcon(icon1); ImageIcon icon2 = new ImageIcon(filtered); lbl2.setIcon(icon2); filterBtn.setSelected(true); } if (e.getSource() == findCircles) { try { accSize = Integer.parseInt(inputCircles.getText()); res = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = res.getGraphics(); g.drawImage(img, 0, 0, null); g.dispose(); findMaxima(); ImageIcon icon1 = new ImageIcon(res); lbl1.setIcon(icon1); } catch (Exception err) { System.out.println("Not a valid integer"); } } else if (e.getSource() == filterBtn) { ImageIcon icon2 = new ImageIcon(filtered); lbl2.setIcon(icon2); } else if (e.getSource() == sobelBtn) { ImageIcon icon2 = new ImageIcon(sobel); lbl2.setIcon(icon2); } else if (e.getSource() == nonMaxBtn) { ImageIcon icon2 = new ImageIcon(nonMax); lbl2.setIcon(icon2); } else if (e.getSource() == accumulator) { buildAccumulator(whichRadius.getValue()); } }
public void Hough() { // for polar we need accumulator of 180degress * the longest length in the image // rmax = (int)Math.sqrt(width*width + height*height); System.out.println("w: " + width + " h: " + height + " rmax: " + rmax); acc = new int[width * height][accRMax]; int rOffset; for (int x = 0; x < height; x++) { for (int y = 0; y < width; y++) { rOffset = 0; for (int r = rmin; r < rmax; r += offset) { acc[x * width + y][rOffset++] = 0; } } } System.out.println("accumulating"); int x0, y0; double t; int[] val = new int[1]; for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { // if ((nonMax.getRaster().getPixel(x, y, val)[0])== -1) { if ((data[y * width + x] & 0xFF) == 255) { rOffset = 0; for (int r = rmin; r < rmax; r += offset) { for (int theta = 0; theta < 360; theta += 2) { t = (theta * 3.14159265) / 180; x0 = (int) Math.round(x - r * Math.cos(t)); y0 = (int) Math.round(y - r * Math.sin(t)); if (x0 < width && x0 > 0 && y0 < height && y0 > 0) { acc[x0 + (y0 * width)][rOffset] += 1; } } rOffset++; } } } } // now normalise to 255 and put in format for a pixel array int max = 0; // Find max acc value System.out.println("Finding Max"); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { rOffset = 0; for (int r = rmin; r < rmax; r += offset) { if (acc[x + (y * width)][rOffset] > max) { max = acc[x + (y * width)][rOffset]; } rOffset++; } } } // Normalise all the values int value; System.out.println("Normalising"); for (int x = 0; x < width; x++) { for (int y = 0; y < height; y++) { rOffset = 0; for (int r = rmin; r < rmax; r += offset) { value = (int) (((double) acc[x + (y * width)][rOffset] / (double) max) * 255.0); acc[x + (y * width)][rOffset] = 0xff000000 | (value << 16 | value << 8 | value); rOffset++; } } } findMaxima(); System.out.println("done"); }
/** * Draw one pixel at (x, y). * * @param x the x-coordinate of the pixel * @param y the y-coordinate of the pixel */ private static void pixel(double x, double y) { offscreen.fillRect((int) Math.round(scaleX(x)), (int) Math.round(scaleY(y)), 1, 1); }
public void drawPixels(ImageProcessor ip) { int saveWidth = ip.getLineWidth(); if (getStrokeWidth() > 1f) ip.setLineWidth((int) Math.round(getStrokeWidth())); double offset = getOffset(0.5); if (xSpline != null) { ip.moveTo( x + (int) (Math.round(xSpline[0]) + offset), y + (int) Math.round(ySpline[0] + offset)); for (int i = 1; i < splinePoints; i++) ip.lineTo( x + (int) (Math.round(xSpline[i]) + offset), y + (int) Math.round(ySpline[i] + offset)); if (type == POLYGON || type == FREEROI || type == TRACED_ROI) ip.lineTo( x + (int) (Math.round(xSpline[0]) + offset), y + (int) Math.round(ySpline[0] + offset)); } else if (xpf != null) { ip.moveTo(x + (int) (Math.round(xpf[0]) + offset), y + (int) Math.round(ypf[0] + offset)); for (int i = 1; i < nPoints; i++) ip.lineTo(x + (int) (Math.round(xpf[i]) + offset), y + (int) Math.round(ypf[i] + offset)); if (type == POLYGON || type == FREEROI || type == TRACED_ROI) ip.lineTo(x + (int) (Math.round(xpf[0]) + offset), y + (int) Math.round(ypf[0] + offset)); } else { ip.moveTo(x + xp[0], y + yp[0]); for (int i = 1; i < nPoints; i++) ip.lineTo(x + xp[i], y + yp[i]); if (type == POLYGON || type == FREEROI || type == TRACED_ROI) ip.lineTo(x + xp[0], y + yp[0]); } ip.setLineWidth(saveWidth); updateFullWindow = true; }
public void setAnimationDuration(int animationDuration) { effectTime = (animationDuration < 1000) ? 1000 : animationDuration; dt = Math.round(effectTime * deltaAngle / 180); }
class SpecialPanel extends JPanel { SlideAnimation owner; BufferedImage firstImage; BufferedImage secondImage; Component component1; Component component2; float angle = 0; public float beginAngle = 0; public float endAngle = 360; float deltaAngle = 0.5f; float effectTime = 2000; long dt = Math.round(effectTime * deltaAngle / 180); int counter = 0; long totalDrawTime = 0; public boolean needToStartThread = false; SpecialPanel(SlideAnimation owner, BufferedImage firstImage, BufferedImage secondImage) { this.owner = owner; this.firstImage = firstImage; this.secondImage = secondImage; angle = beginAngle; setOpaque(false); } SpecialPanel(SlideAnimation owner, Component component1, Component component2) { this.owner = owner; this.component1 = component1; this.component2 = component2; angle = beginAngle; setOpaque(false); } public void setAnimationDuration(int animationDuration) { effectTime = (animationDuration < 1000) ? 1000 : animationDuration; dt = Math.round(effectTime * deltaAngle / 180); } void startThread(float val1, float val2) { counter = 0; totalDrawTime = 0; this.beginAngle = val1; this.endAngle = val2; if (endAngle < beginAngle) deltaAngle = -Math.abs(deltaAngle); else deltaAngle = Math.abs(deltaAngle); angle = beginAngle; final Runnable repaint = new Runnable() { // [email protected] Arnaud Masson public void run() { repaint(); getToolkit().sync(); } }; Thread t = new Thread( new Runnable() { public void run() { float absDeltaAngle = Math.abs(deltaAngle); long startTime = System.currentTimeMillis(); long initTime = System.currentTimeMillis(); while (true) { long time = System.currentTimeMillis(); angle += deltaAngle * (time - startTime) / dt; // idea [email protected] Arnaud Masson startTime = time; if (((angle >= endAngle - deltaAngle / 2) && (deltaAngle > 0)) || ((angle <= endAngle - deltaAngle / 2) && (deltaAngle < 0))) { angle = endAngle; if (Math.abs(angle - 360) < absDeltaAngle / 2) angle = 0; if (Math.abs(angle - 180) < absDeltaAngle / 2) angle = 180; repaint(); if (counter != 0) System.out.println( "total count " + counter + " time " + (System.currentTimeMillis() - initTime) + " average time " + (totalDrawTime / counter)); break; } if (angle >= 360) angle = 0; try { Thread.sleep(dt); repaint(); getToolkit().sync(); // SwingUtilities.invokeAndWait(repaint); //idea [email protected] Arnaud // Masson } catch (Throwable tt) { } } if (owner != null) owner.rotationFinished(); synchronized (SpecialPanel.this) { if (component1 != null) firstImage = null; if (component2 != null) secondImage = null; } } }); t.start(); } public void update(Graphics g) { paint(g); } public synchronized void paint(Graphics g) { if (needToStartThread) { totalDrawTime = 0; counter = 0; needToStartThread = false; startThread(beginAngle, endAngle); if (firstImage == null) { firstImage = createImageFromComponent(component1); } if (secondImage == null) { secondImage = createImageFromComponent(component2); } } if (firstImage == null || secondImage == null) return; Graphics2D g2d = (Graphics2D) g; int ww = firstImage.getWidth(); int hh = firstImage.getHeight(); { BufferedImage currImage = null; int[] currPixels = null; int w = firstImage.getWidth(); int offset = (int) (w * angle / 180); if (offset < 0) offset = 0; if (offset > w) offset = w; long beforeDraw = System.currentTimeMillis(); g2d.drawImage(firstImage, null, 0, 0); g2d.drawImage(secondImage, null, w - offset, 0); totalDrawTime += (System.currentTimeMillis() - beforeDraw); counter++; } } BufferedImage createImageFromComponent(Component comp) { BufferedImage retImage = null; if (comp == null) return retImage; try { GraphicsEnvironment genv = GraphicsEnvironment.getLocalGraphicsEnvironment(); GraphicsDevice gd = genv.getDefaultScreenDevice(); GraphicsConfiguration gc = gd.getDefaultConfiguration(); java.awt.image.ColorModel cm = gc.getColorModel(); boolean hasAlpha = cm.hasAlpha(); int cw = comp.getSize().width; int ch = comp.getSize().height; if (hasAlpha) { retImage = gc.createCompatibleImage(cw, ch); } else { retImage = new BufferedImage(cw, ch, BufferedImage.TYPE_INT_ARGB); } if (retImage == null) return retImage; Graphics og = retImage.getGraphics(); comp.paint(og); og.dispose(); } catch (Throwable t) { } return retImage; } }
private int scy(int py) { return (int) Math.round(py * scaley); }
private int scx(int px) { return (int) Math.round(px * scalex); }