Search in sources :

Example 96 with java.awt.image

use of java.awt.image in project JFramework by gugumall.

the class JUtilImage method zoomToSizeIfLarger.

/**
 * 将srcFile按比例缩放,使其较长一边的长度等于longerSideSize,并保存为destFile
 *
 * @param srcFile
 * @param destFile
 * @param longerSideSize
 * @param imageFormat
 * @return
 * @throws Exception
 */
public void zoomToSizeIfLarger(File srcFile, File destFile, int longerSideSize, String imageFormat) throws Exception {
    if (!chkImageFormat(imageFormat)) {
        throw new Exception("图片类型不合法");
    }
    Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
    src = new ImageIcon(src).getImage();
    double oldWidth = (double) src.getWidth(this);
    double oldHeight = (double) src.getHeight(this);
    if (oldWidth <= longerSideSize && oldHeight <= longerSideSize) {
        JDFSFile.saveStream(new FileInputStream(srcFile), destFile.getAbsolutePath());
        return;
    }
    double scale = 1;
    if (oldWidth > oldHeight) {
        scale = longerSideSize / oldWidth;
    } else {
        scale = longerSideSize / oldHeight;
    }
    zoom(srcFile, destFile, scale, imageFormat);
}
Also used : ImageIcon(javax.swing.ImageIcon) IIOImage(javax.imageio.IIOImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) GifImage(com.gif4j.GifImage) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 97 with java.awt.image

use of java.awt.image in project JFramework by gugumall.

the class JUtilImage method zoom.

/**
 * 将srcFile按比例scale缩放,长宽比例不变,并保存为destFile
 *
 * @param srcFile
 * @param destFile
 * @param scale
 * @param imageFormat
 * @return
 * @throws Exception
 */
public void zoom(File srcFile, File destFile, double scale, String imageFormat) throws Exception {
    if (!chkImageFormat(imageFormat)) {
        throw new Exception("图片类型不合法");
    }
    Image src = Toolkit.getDefaultToolkit().getImage(srcFile.getAbsolutePath());
    src = new ImageIcon(src).getImage();
    double oldWidth = (double) src.getWidth(this);
    double oldHeight = (double) src.getHeight(this);
    int newWidth = JUtilMath.toInt(oldWidth * scale);
    int newHeight = JUtilMath.toInt(oldHeight * scale);
    if (imageFormat.equalsIgnoreCase(FORMAT_GIF) || srcFile.getName().toLowerCase().endsWith(".gif")) {
        resizeGifImage(srcFile, destFile, newWidth, newHeight, true);
        return;
    }
    BufferedImage tag = new BufferedImage(newWidth, newHeight, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2d = tag.createGraphics();
    // 设置透明
    if (imageFormat.equals(JUtilImage.FORMAT_PNG)) {
        tag = g2d.getDeviceConfiguration().createCompatibleImage(newWidth, newHeight, Transparency.TRANSLUCENT);
    }
    g2d.dispose();
    g2d = tag.createGraphics();
    // 设置透明 end
    // 绘制缩小后的图
    g2d.drawImage(src, 0, 0, newWidth, newHeight, this);
    // 如果父目录不存在,则创建目录
    if (!destFile.getParentFile().exists()) {
        destFile.getParentFile().mkdirs();
    }
    // 输出到文件流
    FileOutputStream os = new FileOutputStream(destFile);
    ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName(imageFormat).next();
    ImageOutputStream ios = ImageIO.createImageOutputStream(os);
    writer.setOutput(ios);
    ImageWriteParam param = new JPEGImageWriteParam(Locale.getDefault());
    param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
    param.setCompressionQuality(quality);
    writer.write(null, new IIOImage(tag, null, null), param);
    writer.dispose();
    ios.flush();
    ios.close();
    os.close();
}
Also used : ImageIcon(javax.swing.ImageIcon) ImageWriter(javax.imageio.ImageWriter) IIOImage(javax.imageio.IIOImage) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) GifImage(com.gif4j.GifImage) ImageWriteParam(javax.imageio.ImageWriteParam) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) IOException(java.io.IOException) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) IIOImage(javax.imageio.IIOImage) FileOutputStream(java.io.FileOutputStream) JPEGImageWriteParam(javax.imageio.plugins.jpeg.JPEGImageWriteParam) ImageOutputStream(javax.imageio.stream.ImageOutputStream)

Example 98 with java.awt.image

use of java.awt.image in project JFramework by gugumall.

the class ScreenSnapshot method main.

public static void main(String[] args) throws MalformedURLException, IOException, URISyntaxException, AWTException {
    // d打开一个网页
    // Desktop.getDesktop().browse(new URL("http://www.google.com").toURI());
    // 此方法仅适用于JdK1.6及以上版本
    Robot robot = new Robot();
    robot.delay(6000);
    Dimension d = new Dimension(Toolkit.getDefaultToolkit().getScreenSize());
    int width = (int) d.getWidth();
    int height = (int) d.getHeight();
    // 最大化浏览器
    robot.keyRelease(KeyEvent.VK_F11);
    robot.delay(2000);
    Image image = robot.createScreenCapture(new Rectangle(0, 0, width, height));
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = bi.createGraphics();
    g.drawImage(image, 0, 0, width, height, null);
    // 保存图片
    ImageIO.write(bi, "jpg", new File("c:/google.jpg"));
}
Also used : Graphics(java.awt.Graphics) Rectangle(java.awt.Rectangle) Dimension(java.awt.Dimension) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) Robot(java.awt.Robot) File(java.io.File) BufferedImage(java.awt.image.BufferedImage)

Example 99 with java.awt.image

use of java.awt.image in project JSettlers2 by jdmonin.

the class SOCBoardPanel method drawBoard.

/**
 * Draw the whole board, including pieces and tooltip ({@link #hilight}, {@link #hoverTip}) if applicable.
 * The basic board without pieces is drawn just once, then buffered.
 * If the board layout changes (at start of game, for example),
 * call {@link #flushBoardLayoutAndRepaint()} to clear the buffered copy.
 *
 * @see #drawBoardEmpty(Graphics)
 */
private void drawBoard(Graphics g) {
    Image ebb = emptyBoardBuffer;
    if (scaledMissedImage || ebb == null) {
        if (ebb == null) {
            ebb = createImage(scaledPanelW, scaledPanelH);
            emptyBoardBuffer = ebb;
        }
        drawnEmptyAt = System.currentTimeMillis();
        // drawBoardEmpty, drawHex will set this flag if missed
        scaledMissedImage = false;
        drawBoardEmpty(ebb.getGraphics());
        ebb.flush();
        if (scaledMissedImage && (scaledAt != 0) && (RESCALE_MAX_RETRY_MS < (drawnEmptyAt - scaledAt)))
            // eventually give up scaling it
            scaledMissedImage = false;
    }
    // draw ebb from local variable, not emptyBoardBuffer field, to avoid occasional NPE
    g.setPaintMode();
    g.drawImage(ebb, 0, 0, this);
    // ask for antialiasing if available
    if (g instanceof Graphics2D)
        ((Graphics2D) g).setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    final boolean xlat = (panelMarginX != 0) || (panelMarginY != 0);
    if (xlat)
        g.translate(panelMarginX, panelMarginY);
    final int gameState = game.getGameState();
    if (board.getRobberHex() != -1) {
        drawRobber(g, board.getRobberHex(), (gameState != SOCGame.PLACING_ROBBER), true);
    }
    if (board.getPreviousRobberHex() != -1) {
        drawRobber(g, board.getPreviousRobberHex(), (gameState != SOCGame.PLACING_ROBBER), false);
    }
    if (isLargeBoard) {
        int hex = ((SOCBoardLarge) board).getPirateHex();
        if (hex > 0) {
            drawRoadOrShip(g, hex, -2, (gameState == SOCGame.PLACING_PIRATE), false, false);
        }
        hex = ((SOCBoardLarge) board).getPreviousPirateHex();
        if (hex > 0) {
            drawRoadOrShip(g, hex, -3, (gameState == SOCGame.PLACING_PIRATE), false, false);
        }
    }
    /**
     * draw the roads and ships
     */
    if (!game.isGameOptionSet(SOCGameOption.K_SC_PIRI)) {
        for (SOCRoad r : board.getRoads()) {
            drawRoadOrShip(g, r.getCoordinates(), r.getPlayerNumber(), false, !(r instanceof SOCShip), false);
        }
    } else {
        for (int pn = 0; pn < game.maxPlayers; ++pn) {
            final SOCPlayer pl = game.getPlayer(pn);
            // count warships here, for efficiency, instead of calling SOCGame.isShipWarship for each one
            int numWarships = pl.getNumWarships();
            for (SOCRoad r : pl.getRoads()) {
                final boolean isShip = (r instanceof SOCShip);
                final boolean isWarship = isShip && (numWarships > 0);
                drawRoadOrShip(g, r.getCoordinates(), pn, false, !isShip, isWarship);
                if (isWarship)
                    // this works since warships begin with player's 1st-placed ship in getRoads()
                    --numWarships;
            }
            /**
             * draw the player's fortress, if any
             */
            SOCFortress fo = pl.getFortress();
            if (fo != null)
                drawFortress(g, fo, pn, false);
        }
    }
    /**
     * draw the settlements
     */
    for (SOCSettlement s : board.getSettlements()) {
        drawSettlement(g, s.getCoordinates(), s.getPlayerNumber(), false, false);
    }
    /**
     * draw the cities
     */
    for (SOCCity c : board.getCities()) {
        drawCity(g, c.getCoordinates(), c.getPlayerNumber(), false);
    }
    if (xlat)
        g.translate(-panelMarginX, -panelMarginY);
    /**
     * draw the current-player arrow after ("above") pieces,
     * but below any hilighted piece, in case of overlap at
     * edge of board. More likely on 6-player board for the
     * two players whose handpanels are vertically centered.
     */
    if (gameState != SOCGame.NEW) {
        drawArrow(g, game.getCurrentPlayerNumber(), game.getCurrentDice());
    }
    if (player != null) {
        if (xlat)
            g.translate(panelMarginX, panelMarginY);
        /**
         * Draw the hilight when in interactive mode;
         * No hilight when null player (before game started).
         * The "hovering" road/settlement/city are separately painted
         * in {@link soc.client.SOCBoardPanel.BoardToolTip#paint()}.
         */
        switch(mode) {
            case MOVE_SHIP:
                if (moveShip_fromEdge != 0)
                    drawRoadOrShip(g, moveShip_fromEdge, -1, false, false, moveShip_isWarship);
            case PLACE_ROAD:
            case PLACE_INIT_ROAD:
            case PLACE_FREE_ROAD_OR_SHIP:
                if (hilight != 0) {
                    drawRoadOrShip(g, hilight, playerNumber, true, !hilightIsShip, (moveShip_isWarship && (moveShip_fromEdge != 0)));
                }
                break;
            case PLACE_SETTLEMENT:
            case PLACE_INIT_SETTLEMENT:
                if (hilight > 0) {
                    drawSettlement(g, hilight, playerNumber, true, false);
                }
                break;
            case PLACE_CITY:
                if (hilight > 0) {
                    drawCity(g, hilight, playerNumber, true);
                }
                break;
            case PLACE_SHIP:
                if (hilight > 0) {
                    drawRoadOrShip(g, hilight, playerNumber, true, false, false);
                }
                break;
            case CONSIDER_LM_SETTLEMENT:
            case CONSIDER_LT_SETTLEMENT:
                if (hilight > 0) {
                    drawSettlement(g, hilight, otherPlayer.getPlayerNumber(), true, false);
                }
                break;
            case CONSIDER_LM_ROAD:
            case CONSIDER_LT_ROAD:
                if (hilight != 0) {
                    drawRoadOrShip(g, hilight, otherPlayer.getPlayerNumber(), false, true, false);
                }
                break;
            case CONSIDER_LM_CITY:
            case CONSIDER_LT_CITY:
                if (hilight > 0) {
                    drawCity(g, hilight, otherPlayer.getPlayerNumber(), true);
                }
                break;
            case PLACE_ROBBER:
                if (hilight > 0) {
                    drawRobber(g, hilight, true, true);
                }
                break;
            case PLACE_PIRATE:
                if (hilight > 0) {
                    drawRoadOrShip(g, hilight, -2, false, false, false);
                }
                break;
            case SC_FTRI_PLACE_PORT:
                drawBoard_SC_FTRI_placePort(g);
                break;
        }
        if (xlat)
            g.translate(-panelMarginX, -panelMarginY);
    }
    if (superText1 != null) {
        drawSuperText(g);
    }
    if (superTextTop != null) {
        drawSuperTextTop(g);
    }
}
Also used : SOCSettlement(soc.game.SOCSettlement) SOCBoardLarge(soc.game.SOCBoardLarge) SOCCity(soc.game.SOCCity) SOCShip(soc.game.SOCShip) SOCPlayer(soc.game.SOCPlayer) SOCFortress(soc.game.SOCFortress) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage) SOCRoad(soc.game.SOCRoad) Graphics2D(java.awt.Graphics2D)

Example 100 with java.awt.image

use of java.awt.image in project JSettlers2 by jdmonin.

the class SOCBoardPanel method paint.

/**
 * Redraw the board using double buffering. Don't call this directly, use
 * {@link Component#repaint()} instead.
 *<P>
 * See {@link #drawBoard(Graphics)} for related painting methods.
 *<P>
 * To protect against bugs, paint contains a try-catch that will
 * print stack traces to the player chat print area.
 */
@Override
public void paint(Graphics g) {
    // Local var in case field becomes null in other thread during paint
    Image ibuf = buffer;
    try {
        if (ibuf == null) {
            ibuf = this.createImage(scaledPanelW, scaledPanelH);
            buffer = ibuf;
        }
        // previously an issue with java 1.4 piece enumerations.
        try {
            // Do the actual drawing
            drawBoard(ibuf.getGraphics());
        } catch (ConcurrentModificationException cme) {
            // try again soon
            repaint();
            return;
        }
        if (hoverTip.isVisible())
            hoverTip.paint(ibuf.getGraphics());
        ibuf.flush();
        g.drawImage(ibuf, 0, 0, this);
    } catch (Throwable th) {
        playerInterface.chatPrintStackTrace(th);
    }
}
Also used : ConcurrentModificationException(java.util.ConcurrentModificationException) Image(java.awt.Image) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Image (java.awt.Image)515 BufferedImage (java.awt.image.BufferedImage)263 ImageIcon (javax.swing.ImageIcon)127 Graphics2D (java.awt.Graphics2D)97 IOException (java.io.IOException)73 File (java.io.File)64 Point (java.awt.Point)53 URL (java.net.URL)51 Graphics (java.awt.Graphics)46 JLabel (javax.swing.JLabel)31 Dimension (java.awt.Dimension)28 Rectangle (java.awt.Rectangle)27 AffineTransform (java.awt.geom.AffineTransform)25 Color (java.awt.Color)24 Test (org.junit.Test)24 RenderedImage (java.awt.image.RenderedImage)21 ArrayList (java.util.ArrayList)21 Toolkit (java.awt.Toolkit)20 IIOImage (javax.imageio.IIOImage)18 MediaTracker (java.awt.MediaTracker)17