Example usage for java.awt Graphics drawImage

List of usage examples for java.awt Graphics drawImage

Introduction

In this page you can find the example usage for java.awt Graphics drawImage.

Prototype

public abstract boolean drawImage(Image img, int x, int y, int width, int height, ImageObserver observer);

Source Link

Document

Draws as much of the specified image as has already been scaled to fit inside the specified rectangle.

Usage

From source file:SplashScreen.java

/**
 * Paint the splash screen window.//from  www. ja v a 2 s . c  om
 * 
 * @param graphics  The graphics instance.
 */
public void paint(Graphics graphics) {
    System.out.println("paint");
    if (image_ == null)
        return;
    graphics.drawImage(image_, 0, 0, width_, height_, this);
}

From source file:WaitingImageObserver.java

/**
 * The workerthread. Simply draws the image to a BufferedImage's
 * Graphics-Object and waits for the AWT to load the image.
 *//*from   www .j ava2 s .c  o  m*/
public synchronized void waitImageLoaded() {

    if (this.lock == false) {
        return;
    }

    final BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
    final Graphics g = img.getGraphics();

    while (this.lock) {
        if (g.drawImage(this.image, 0, 0, img.getWidth(this), img.getHeight(this), this)) {
            return;
        }

        try {
            wait(500);
        } catch (InterruptedException e) {
            System.out.println("WaitingImageObserver.waitImageLoaded(): InterruptedException thrown" + e);
        }
    }
}

From source file:JuliaSet2.java

public void paintComponent(Graphics g) {
    g.drawImage(image, 0, 0, getWidth(), getHeight(), this);
}

From source file:MyCanvas.java

public void paint(Graphics g) {
    Image img1 = Toolkit.getDefaultToolkit().getImage("yourFile.gif");

    int width = img1.getWidth(this);
    int height = img1.getHeight(this);

    int scale = 2;
    int w = scale * width;
    int h = scale * height;
    // explicitly specify width (w) and height (h)
    g.drawImage(img1, 10, 10, (int) w, (int) h, this);

}

From source file:edu.umn.cs.spatialHadoop.operations.GeometricPlot.java

/**
 * Combines images of different datasets into one image that is displayed
 * to users./*  w ww  .ja  v  a  2  s. com*/
 * This method is called from the web interface to display one image for
 * multiple selected datasets.
 * @param fs The file system that contains the datasets and images
 * @param files Paths to directories which contains the datasets
 * @param includeBoundaries Also plot the indexing boundaries of datasets
 * @return An image that is the combination of all datasets images
 * @throws IOException
 */
public static BufferedImage combineImages(Configuration conf, Path[] files, boolean includeBoundaries,
        int width, int height) throws IOException {
    BufferedImage result = null;
    // Retrieve the MBRs of all datasets
    Rectangle allMbr = new Rectangle(Double.MAX_VALUE, Double.MAX_VALUE, -Double.MAX_VALUE, -Double.MAX_VALUE);
    for (Path file : files) {
        Rectangle mbr = FileMBR.fileMBR(file, new OperationsParams(conf));
        allMbr.expand(mbr);
    }

    // Adjust width and height to maintain aspect ratio
    if ((allMbr.x2 - allMbr.x1) / (allMbr.y2 - allMbr.y1) > (double) width / height) {
        // Fix width and change height
        height = (int) ((allMbr.y2 - allMbr.y1) * width / (allMbr.x2 - allMbr.x1));
    } else {
        width = (int) ((allMbr.x2 - allMbr.x1) * height / (allMbr.y2 - allMbr.y1));
    }
    result = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    for (Path file : files) {
        FileSystem fs = file.getFileSystem(conf);
        if (fs.getFileStatus(file).isDir()) {
            // Retrieve the MBR of this dataset
            Rectangle mbr = FileMBR.fileMBR(file, new OperationsParams(conf));
            // Compute the coordinates of this image in the whole picture
            mbr.x1 = (mbr.x1 - allMbr.x1) * width / allMbr.getWidth();
            mbr.x2 = (mbr.x2 - allMbr.x1) * width / allMbr.getWidth();
            mbr.y1 = (mbr.y1 - allMbr.y1) * height / allMbr.getHeight();
            mbr.y2 = (mbr.y2 - allMbr.y1) * height / allMbr.getHeight();
            // Retrieve the image of this dataset
            Path imagePath = new Path(file, "_data.png");
            if (!fs.exists(imagePath))
                throw new RuntimeException("Image " + imagePath + " not ready");
            FSDataInputStream imageFile = fs.open(imagePath);
            BufferedImage image = ImageIO.read(imageFile);
            imageFile.close();
            // Draw the image
            Graphics graphics = result.getGraphics();
            graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(), (int) mbr.getHeight(),
                    null);
            graphics.dispose();

            if (includeBoundaries) {
                // Plot also the image of the boundaries
                // Retrieve the image of the dataset boundaries
                imagePath = new Path(file, "_partitions.png");
                if (fs.exists(imagePath)) {
                    imageFile = fs.open(imagePath);
                    image = ImageIO.read(imageFile);
                    imageFile.close();
                    // Draw the image
                    graphics = result.getGraphics();
                    graphics.drawImage(image, (int) mbr.x1, (int) mbr.y1, (int) mbr.getWidth(),
                            (int) mbr.getHeight(), null);
                    graphics.dispose();
                }
            }
        }
    }

    return result;
}

From source file:MouseDragClip.java

public void paint(Graphics g) {
    int w = curX - startX, h = curY - startY;
    Dimension d = getSize();//  w w w. ja  v  a 2s  . c  om
    if (!inDrag) { // probably first time through(?)
        g.drawImage(curImage, 0, 0, d.width, d.height, this);
        return;
    }
    System.err.println("paint:drawRect @[" + startX + "," + startY + "] size " + w + "x" + h);
    // Restore the old background, if previous selection
    if (oldStartX != -1) {
        g.setClip(oldStartX, oldStartY, oldWidth + 1, oldHeight + 1);
        g.drawImage(curImage, 0, 0, d.width, d.height, this);
        oldStartX = -1;
    }
    // Restore the background from previous motions of current drag
    if (oldX != -1) {
        g.setClip(startX, startY, w, h);
        g.drawImage(curImage, 0, 0, d.width + 1, d.height + 1, this);
    }
    // Draw the new rectangle
    g.setClip(0, 0, d.width, d.height);
    g.setColor(Color.red);
    g.drawRect(startX, startY, w, h);
    oldX = curX;
    oldY = curY;
}

From source file:bigdata.explorer.nutch.grapview.WebGraphZoomScrollPane.java

/**
 * create an instance of a simple graph with controls to
 * demo the zoom features.//ww w .ja v a2  s. c  om
 * 
 */
public WebGraphZoomScrollPane() throws IOException {

    // create a simple graph for the demo
    graph = NutchGraphLoader.loadFullGraph("/home/kamir/ANALYSIS/Nutch/YellowMED_CORE");

    ImageIcon sandstoneIcon = null;
    String imageLocation = "/images/Sandstone.jpg";
    try {
        sandstoneIcon = new ImageIcon(getClass().getResource(imageLocation));
    } catch (Exception ex) {
        System.err.println("Can't load \"" + imageLocation + "\"");
    }
    final ImageIcon icon = sandstoneIcon;
    vv = new VisualizationViewer<String, Number>(new KKLayout<String, Number>(graph));

    if (icon != null) {
        vv.addPreRenderPaintable(new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
                Dimension d = vv.getSize();
                g.drawImage(icon.getImage(), 0, 0, d.width, d.height, vv);
            }

            public boolean useTransform() {
                return false;
            }
        });
    }
    vv.addPostRenderPaintable(new VisualizationViewer.Paintable() {
        int x;
        int y;
        Font font;
        FontMetrics metrics;
        int swidth;
        int sheight;
        String str = "WebGraph Zoomer : Version 1.0.0";

        public void paint(Graphics g) {
            Dimension d = vv.getSize();
            if (font == null) {
                font = new Font(g.getFont().getName(), Font.BOLD, 30);
                metrics = g.getFontMetrics(font);
                swidth = metrics.stringWidth(str);
                sheight = metrics.getMaxAscent() + metrics.getMaxDescent();
                x = (d.width - swidth) / 2;
                y = (int) (d.height - sheight * 1.5);
            }
            g.setFont(font);
            Color oldColor = g.getColor();
            g.setColor(Color.lightGray);
            g.drawString(str, x, y);
            g.setColor(oldColor);
        }

        public boolean useTransform() {
            return false;
        }
    });

    vv.addGraphMouseListener(new TestGraphMouseListener<String>());
    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));
    vv.getRenderContext().setEdgeDrawPaintTransformer(new ConstantTransformer(Color.lightGray));
    vv.getRenderContext().setArrowFillPaintTransformer(new ConstantTransformer(Color.lightGray));
    vv.getRenderContext().setArrowDrawPaintTransformer(new ConstantTransformer(Color.lightGray));

    // add my listeners for ToolTips
    vv.setVertexToolTipTransformer(new ToStringLabeller<String>());
    vv.setEdgeToolTipTransformer(new Transformer<Number, String>() {
        public String transform(Number edge) {
            return "E" + graph.getEndpoints(edge).toString();
        }
    });

    vv.getRenderContext().setVertexLabelTransformer(new ToStringLabeller<String>());
    vv.getRenderer().getVertexLabelRenderer().setPositioner(new InsidePositioner());
    vv.getRenderer().getVertexLabelRenderer().setPosition(Renderer.VertexLabel.Position.AUTO);
    vv.setForeground(Color.lightGray);

    // create a frome to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<String, Number>();
    vv.setGraphMouse(graphMouse);

    vv.addKeyListener(graphMouse.getModeKeyListener());
    vv.setToolTipText("<html><center>Type 'p' for Pick mode<p>Type 't' for Transform mode");

    final ScalingControl scaler = new CrossoverScalingControl();

    JButton plus = new JButton("+");
    plus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1.1f, vv.getCenter());
        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });

    JButton reset = new JButton("reset");
    reset.addActionListener(new ActionListener() {

        public void actionPerformed(ActionEvent e) {
            vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.LAYOUT).setToIdentity();
            vv.getRenderContext().getMultiLayerTransformer().getTransformer(Layer.VIEW).setToIdentity();
        }
    });

    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(reset);
    content.add(controls, BorderLayout.SOUTH);

    frame.pack();
    frame.setVisible(true);
}

From source file:com.pureinfo.tgirls.servlet.TestServlet.java

private BufferedImage getImg(File _temp, float _i) throws IOException {
    BufferedImage output;//  w  w  w.  j  av a2s .com
    Image img = ImageIO.read(_temp);
    int width = img.getWidth(null);
    int height = img.getHeight(null);

    Float f = width * _i;
    width = f.intValue();
    f = height * _i;
    height = f.intValue();

    output = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
    Graphics g = output.getGraphics();
    g.drawImage(img, 0, 0, width, height, null);
    g.dispose();
    return output;
}

From source file:jurls.core.utils.MatrixImage.java

@Override
public void paint(Graphics g) {

    if (image == null) {
        g.setColor(Color.GRAY);//from  w w w .j  a  va 2s .  co  m
        g.fillRect(0, 0, getWidth(), getHeight());
    } else
        g.drawImage(image, 0, 0, getWidth(), getHeight(), null);

}

From source file:org.pentaho.reporting.libraries.base.util.WaitingImageObserver.java

/**
 * The workerthread. Simply draws the image to a BufferedImage's Graphics-Object and waits for the AWT to load the
 * image.// w  ww .j av  a 2 s. c  om
 */
public synchronized void waitImageLoaded() {

    if (this.lock == false) {
        return;
    }

    final BufferedImage img = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
    final Graphics g = img.getGraphics();

    try {
        while (this.lock && error == false) {
            lastUpdate = System.currentTimeMillis();
            if (g.drawImage(this.image, 0, 0, img.getWidth(this), img.getHeight(this), this)) {
                return;
            }

            try {
                wait(500);
            } catch (InterruptedException e) {
                LOGGER.info("WaitingImageObserver.waitImageLoaded(): InterruptedException thrown", e);
            }

            if (lock == false) {
                return;
            }

            if (maxLoadTime > 0 && lastUpdate < (System.currentTimeMillis() - maxLoadTime)) {
                error = true;
                lock = false;
                LOGGER.info("WaitingImageObserver.waitImageLoaded(): Image loading reached timeout.");
                return;
            }
        }
    } finally {
        g.dispose();
    }
}