/* * Create a BufferedImage for AWT components. * * @param component AWT component to create image from * @param fileName name of file to be created or null * @return image the image for the given region * @exception AWTException see Robot class constructors * @exception IOException if an error occurs during writing */ public static BufferedImage createImage(Component component, String fileName) throws AWTException, IOException { Point p = new Point(0, 0); SwingUtilities.convertPointToScreen(p, component); Rectangle region = component.getBounds(); region.x = p.x; region.y = p.y; return ScreenCapture.createImage(region, fileName); }
/** ***************** Main to run the game ****************** */ public static void main(String[] args) { // Use EDT, was running into exceptions before so I added this SwingUtilities.invokeLater( new Runnable() { @Override public void run() { new TicTac(); } }); }
public void propertyChange(PropertyChangeEvent evt) { if (DisplayOptions.isUpdateUIEvent(evt)) { SwingUtilities.updateComponentTreeUI(this); gradientPanel.remove(drawable); JPanel newpanel = createGradientPanel(); contentPane.remove(gradientPanel); gradientPanel = newpanel; gradientPanel.add(drawable, BorderLayout.CENTER); contentPane.add(gradientPanel, BorderLayout.CENTER); } }
public static void main(String[] args) { // read arguments and respond correctly File in; File out; String tileDirectory; if (args.length == 0 || args.length > 2) { in = null; out = null; tileDirectory = ""; System.err.println("Incorrect number of arguments. Required: Filename (tileset path)"); System.exit(0); } else if (args.length == 1) { // load old file in = new File(args[0]); out = in; Scanner s = null; try { s = new Scanner(in); } catch (FileNotFoundException e) { System.out.println("Could not find input file."); System.exit(0); } tileDirectory = s.nextLine(); } else { in = null; out = new File(args[0]); tileDirectory = args[1]; try { File f = new File(tileDirectory); if (!f.isDirectory()) { throw new IOException("Tileset does not exist"); } } catch (IOException e) { System.err.println(e); System.out.println("This error is likely thanks to an invalid tileset path"); System.exit(0); } } MapBuilder test = new MapBuilder("Map Editor", in, out, tileDirectory); // Build GUI SwingUtilities.invokeLater( new Runnable() { public void run() { test.CreateAndDisplayGUI(); } }); }
/** simply dump status info to the textarea */ private void sout(final String s) { Runnable soutRunner = new Runnable() { public void run() { if (ttaStatus.getText().equals("")) { ttaStatus.setText(s); } else { ttaStatus.setText(ttaStatus.getText() + "\n" + s); } } }; if (ThreadUtils.isInEDT()) { soutRunner.run(); } else { SwingUtilities.invokeLater(soutRunner); } }
// Thread's run method aimed at creating a bitmap asynchronously public void run() { Drawing drawing = new Drawing(); PicText rawPicText = new PicText(); String s = ((PicText) element).getText(); rawPicText.setText(s); drawing.add( rawPicText); // bug fix: we must add a CLONE of the PicText, otherwise it loses it former // parent... (then pb with the view ) drawing.setNotparsedCommands( "\\newlength{\\jpicwidth}\\settowidth{\\jpicwidth}{" + s + "}" + CR_LF + "\\newlength{\\jpicheight}\\settoheight{\\jpicheight}{" + s + "}" + CR_LF + "\\newlength{\\jpicdepth}\\settodepth{\\jpicdepth}{" + s + "}" + CR_LF + "\\typeout{JPICEDT INFO: \\the\\jpicwidth, \\the\\jpicheight, \\the\\jpicdepth }" + CR_LF); RunExternalCommand.Command commandToRun = RunExternalCommand.Command.BITMAP_CREATION; // RunExternalCommand command = new RunExternalCommand(drawing, contentType,commandToRun); boolean isWriteTmpTeXfile = true; String bitmapExt = "png"; // [pending] preferences String cmdLine = "{i}/unix/tetex/create_bitmap.sh {p} {f} " + bitmapExt + " " + fileDPI; // [pending] preferences ContentType contentType = getContainer().getContentType(); RunExternalCommand.isGUI = false; // System.out, no dialog box // [pending] debug RunExternalCommand command = new RunExternalCommand(drawing, contentType, cmdLine, isWriteTmpTeXfile); command .run(); // synchronous in an async. thread => it's ok (anyway, we must way until the LaTeX // process has completed) if (wantToComputeLatexDimensions) { // load size of text: try { File logFile = new File(command.getTmpPath(), command.getTmpFilePrefix() + ".log"); BufferedReader reader = null; try { reader = new BufferedReader(new FileReader(logFile)); } catch (FileNotFoundException fnfe) { System.out.println("Cannot find log file! " + fnfe.getMessage()); System.out.println(logFile); } catch (IOException ioex) { System.out.println("Log file IO exception"); ioex.printStackTrace(); } // utile ? System.out.println("Log file created! file=" + logFile); getDimensionsFromLogFile(reader, (PicText) element); syncStringLocation(); // update dimensions syncBounds(); syncFrame(); SwingUtilities.invokeLater( new Thread() { public void run() { repaint(null); } }); // repaint(null); // now that dimensions are available, we force a repaint() [pending] // smart-repaint ? } catch (Exception e) { e.printStackTrace(); } } if (wantToGetBitMap) { // load image: try { File bitmapFile = new File(command.getTmpPath(), command.getTmpFilePrefix() + "." + bitmapExt); this.image = ImageIO.read(bitmapFile); System.out.println( "Bitmap created! file=" + bitmapFile + ", width=" + image.getWidth() + "pixels, height=" + image.getHeight() + "pixels"); if (image == null) return; syncStringLocation(); // sets strx, stry, and dimensions of text syncBounds(); // update the AffineTransform that will be applied to the bitmap before displaying on screen PicText te = (PicText) element; text2ModelTr.setToIdentity(); // reset PicPoint anchor = te.getCtrlPt(TextEditable.P_ANCHOR, ptBuf); text2ModelTr.rotate(getRotation(), anchor.x, anchor.y); // rotate along P_ANCHOR ! text2ModelTr.translate(te.getLeftX(), te.getTopY()); text2ModelTr.scale( te.getWidth() / image.getWidth(), -(te.getHeight() + te.getDepth()) / image.getHeight()); // [pending] should do something special to avoid dividing by 0 or setting a rescaling // factor to 0 [non invertible matrix] (java will throw an exception) syncFrame(); SwingUtilities.invokeLater( new Thread() { public void run() { repaint(null); } }); // repaint(null); // now that bitmap is available, we force a repaint() [pending] // smart-repaint ? } catch (Exception e) { e.printStackTrace(); } } }
public void actionPerformed(ActionEvent e) { int tempW = mapWidth; int tempH = mapHeight; int targetW; int targetH; try { targetW = Integer.parseInt(widthField.getText()); targetH = Integer.parseInt(heightField.getText()); if (targetH <= 0 || targetW <= 0) { JOptionPane.showMessageDialog(null, "Both x and y must be above 0."); } else { // shrink width if necessary while (targetW < mapWidth) { for (int i = mapHeight * mapWidth - 1; i >= 0; i -= mapWidth) { map.remove(i); } mapWidth--; mapScroll.revalidate(); map.repaint(); backEnd.removeColumn(backEnd.getWidth() - 1); } // shrink height if necessary while (targetH < mapHeight) { for (int i = 1; i <= mapWidth; i++) { map.remove(mapHeight * mapWidth - i); } mapHeight--; // map.setLayout(new GridLayout(mapHeight,mapWidth)); map.repaint(); backEnd.removeRow(backEnd.getHeight() - 1); } // Grow if necessary if (targetW > mapWidth || targetH > mapHeight) { // add new rows and columns, then rebuild and re-add display int[] mov = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}; while (targetW > mapWidth) { // add new column to backEnd List<Tile> tList = new ArrayList<Tile>(); for (int i = 0; i < mapHeight; i++) { Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc); t.setPreferredSize( new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); tList.add(t); } backEnd.addColumn(tList); mapWidth++; } while (targetH > mapHeight) { // add new row to backEnd List<Tile> tList = new ArrayList<Tile>(); for (int i = 0; i < mapWidth; i++) { Tile t = new Tile(currTileImg, "Test", 0, 0, mov, "none", true, currTileLoc); t.setPreferredSize( new Dimension(TILE_SIZE * DISPLAY_SCALE, TILE_SIZE * DISPLAY_SCALE)); t.addMouseListener(new MapButtonListener()); tList.add(t); } backEnd.addRow(tList); mapHeight++; } GridBagConstraints gbc = new GridBagConstraints(); for (int i = 0; i < mapHeight; i++) { gbc.gridy = i; for (int j = 0; j < mapWidth; j++) { gbc.gridx = j; map.add(backEnd.getTile(j, i), gbc); } } map.revalidate(); map.repaint(); parentPanel.revalidate(); parentPanel.repaint(); ((MapBuilder) SwingUtilities.getWindowAncestor(parentPanel)).pack(); } } } catch (NumberFormatException f) { JOptionPane.showMessageDialog(null, "Both x and y must be valid integers."); } }