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:GrafosTroleBus.java

public GrafosTroleBus() {
    setLayout(new BorderLayout());

    //definir puntos de trolebus (latitude y longitude) @autor sa

    map.put("RECREO", new String[] { "-0.2516682", "-78.521524" }); //Recreo                       
    map.put("P14", new String[] { "-0.2445098", "-78.51902" }); //Villaflora
    map.put("P15", new String[] { "-0.2396436", "-78.51698" }); //Chimbacalle N-S                      
    map.put("P16", new String[] { "-0.2378458", "-78.515976" }); //Chimbacalle S-N
    map.put("P17", new String[] { "-0.2356805", "-78.514816" }); //Colina
    map.put("P18", new String[] { "-0.234052", "-78.514237" }); //Jefferson Perez
    map.put("P19", new String[] { "-0.2312856", "-78.513627" }); //Recoleta N-S        
    map.put("P20", new String[] { "-0.2307005", "-78.513051" }); //Recoleta S-N
    map.put("P21", new String[] { "-0.2263919", "-78.513011" }); //P21 Cumanda N-S
    map.put("P22", new String[] { "-0.226424", "-78.512803" }); //P22 Cumanda S-N
    map.put("P23", new String[] { "-0.2234658", "-78.512542" }); //P23 Santo Domingo
    map.put("P24", new String[] { "-0.2185857", "-78.508601" }); //P24 Plaza del Teatro N-S
    map.put("P25", new String[] { "-0.219605", "-78.50813" }); //P25 Plaza del Teatro S-N        
    map.put("P26", new String[] { "-0.2177808", "-78.505977" }); //P26 Hermano Miguel
    map.put("P27", new String[] { "-0.2169088", "-78.50521" }); //P27 Banco Central
    map.put("P28", new String[] { "-0.214267", "-78.502999" }); //P28 La Alameda S-N
    map.put("P29", new String[] { "-0.2137705", "-78.50293" }); //P29 La Alameda N-S                       
    map.put("P30", new String[] { "-0.2084939", "-78.500255" }); //P30 Ejido N-S
    map.put("P31", new String[] { "-0.2088076", "-78.500032" }); //P31 Ejido S-N
    map.put("P32", new String[] { "-0.2047989", "-78.4988" }); //P32 La Mariscal N-S
    map.put("P33", new String[] { "-0.2041972", "-78.498491" }); //P33 La Mariscal S-N
    map.put("P34", new String[] { "-0.2009718", "-78.49715" }); //P34 Santa Clara S-N
    map.put("P35", new String[] { "-0.201056", "-78.496979" }); //P35 Santa Clara N-S
    map.put("P36", new String[] { "-0.1986325", "-78.496141" }); //P36 La Colon S-N
    map.put("P37", new String[] { "-0.1978432", "-78.495563" }); //P37 La Colon N-S
    map.put("P38", new String[] { "-0.1921587", "-78.493445" }); //P38 Cuero y Caicedo S-N
    map.put("P39", new String[] { "-0.1915098", "-78.493001" }); //P39 Cuero y Caicedo N-S                        
    map.put("P40", new String[] { "-0.1889467", "-78.492149" }); //P40 Mariana de Jess S-N
    map.put("P41", new String[] { "-0.1875567", "-78.491303" }); //P41 Mariana de Jesus N-S
    map.put("P42", new String[] { "-0.1853693", "-78.490878" }); //P42 El Floron S-N
    map.put("P43", new String[] { "-0.1846687", "-78.490403" }); //P43 El Floron N-S        
    map.put("P44", new String[] { "-0.1817679", "-78.489808" }); //P44 Carolina S-N
    map.put("P45", new String[] { "-0.1810849", "-78.489336" }); //P45 Carolina N-S
    map.put("P46", new String[] { "-0.1787274", "-78.488954" }); //P46 Estadio S-N
    map.put("P47", new String[] { "-0.1780172", "-78.488621" }); //P47 Estadio N-S
    map.put("P48", new String[] { "-0.172087", "-78.487589" }); //P48 La Y S-N
    map.put("P49", new String[] { "-0.1713146", "-78.487277" }); //P49 La Y N-S        
    map.put("LA Y", new String[] { "-0.1635504", "-78.485374" }); //Estacin La Y                              

    nodoList = new ArrayList<String>(map.keySet());

    // create a simple graph for the demo        
    graph = new DirectedSparseMultigraph<String, Number>();
    createVertices();/*from w w w . j  av  a 2s . co  m*/
    createEdges();

    ImageIcon mapIcon = null;
    String imageLocation = "/mapa_quito.png";

    try {
        mapIcon = new ImageIcon(getClass().getResource(imageLocation));

        ImageWidth = mapIcon.getIconWidth();
        ImageHeight = mapIcon.getIconHeight();

    } catch (Exception ex) {
        System.err.println("Can't load \"" + imageLocation + "\"");
    }

    final ImageIcon icon = mapIcon;

    Dimension layoutSize = new Dimension(ImageWidth, ImageHeight);

    Layout<String, Number> layout = new StaticLayout<String, Number>(graph,
            new ChainedTransformer<String, Point2D>(new Transformer[] { new CityTransformer(map),
                    new LatLonPixelTransformer(new Dimension(ImageWidth, ImageHeight)) }));

    layout.setSize(layoutSize);
    vv = new VisualizationViewer<String, Number>(layout, new Dimension(MonitorWidth, MonitorHeight));

    if (icon != null) {
        vv.addPreRenderPaintable(new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform oldXform = g2d.getTransform();
                AffineTransform lat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.LAYOUT).getTransform();
                AffineTransform vat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.VIEW).getTransform();
                AffineTransform at = new AffineTransform();
                at.concatenate(g2d.getTransform());
                at.concatenate(vat);
                at.concatenate(lat);
                g2d.setTransform(at);
                g.drawImage(icon.getImage(), 0, 0, icon.getIconWidth(), icon.getIconHeight(), vv);
                g2d.setTransform(oldXform);
            }

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

    vv.getRenderer().setVertexRenderer(new GradientVertexRenderer<String, Number>(Color.white, Color.red,
            Color.white, Color.blue, vv.getPickedVertexState(), false));

    // 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);

    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    add(panel);
    final AbstractModalGraphMouse graphMouse = new DefaultModalGraphMouse<Object, Object>();
    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();

    vv.scaleToLayout(scaler);

    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);
    add(controls, BorderLayout.SOUTH);
}

From source file:net.rptools.tokentool.ui.TokenCompositionPanel.java

private void paintOverlay(Graphics g, Dimension size) {
    if (composedOverlayImage == null) {
        composedOverlayImage = TokenCompositor.translateOverlay(overlayImage, 1);
        fireCompositionChanged();//from  w w w .  j a v  a  2 s .  c  o m
    }
    if (composedOverlayImage != null) {
        int width = (Integer) TokenTool.getFrame().getControlPanel().getWidthSpinner().getValue();
        int height = (Integer) TokenTool.getFrame().getControlPanel().getHeightSpinner().getValue();
        int x = (size.width - width) / 2;
        int y = (size.height - height) / 2;

        if (overlayBounds != null) {
            if (overlayBounds.width != width || overlayBounds.height != height) {
                fireCompositionChanged();
            }
        }

        g.drawImage(composedOverlayImage, x, y, width, height, this);

        overlayBounds = new Rectangle(x, y, width, height);
    }
}

From source file:com.net2plan.gui.utils.topologyPane.jung.JUNGCanvas.java

public void updateBackgroundImage(final ImageIcon icon, final int x, final int y) {
    if (paintableAssociatedToBackgroundImage != null)
        vv.removePreRenderPaintable(paintableAssociatedToBackgroundImage);
    paintableAssociatedToBackgroundImage = null;
    if (icon != null) {
        this.paintableAssociatedToBackgroundImage = new VisualizationViewer.Paintable() {
            public void paint(Graphics g) {
                Graphics2D g2d = (Graphics2D) g;
                AffineTransform oldXform = g2d.getTransform();
                AffineTransform lat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.LAYOUT).getTransform();
                AffineTransform vat = vv.getRenderContext().getMultiLayerTransformer()
                        .getTransformer(Layer.VIEW).getTransform();
                AffineTransform at = new AffineTransform();
                at.concatenate(g2d.getTransform());
                at.concatenate(vat);/* w w  w.ja  v a  2 s  .  c om*/
                at.concatenate(lat);
                g2d.setTransform(at);
                g.drawImage(icon.getImage(), x, y, icon.getIconWidth(), icon.getIconHeight(), vv);
                g2d.setTransform(oldXform);
            }

            public boolean useTransform() {
                return false;
            }
        };
        vv.addPreRenderPaintable(paintableAssociatedToBackgroundImage);
    }
}

From source file:org.cloudml.ui.graph.Visu.java

public void createFrame() {
    final VisualizationViewer<Vertex, Edge> vv = v.getVisualisationViewer();

    vv.getRenderContext().setVertexIconTransformer(new Transformer<Vertex, Icon>() {
        public Icon transform(final Vertex v) {
            return new Icon() {

                public int getIconHeight() {
                    return 40;
                }/*  w  w w . j a v a2s  . co m*/

                public int getIconWidth() {
                    return 40;
                }

                public void paintIcon(java.awt.Component c, Graphics g, int x, int y) {
                    ImageIcon img;
                    if (v.getType() == "node") {
                        img = new ImageIcon(this.getClass().getResource("/server.png"));
                    } else if (v.getType() == "platform") {
                        img = new ImageIcon(this.getClass().getResource("/dbms.png"));
                    } else {
                        img = new ImageIcon(this.getClass().getResource("/soft.png"));
                    }
                    ImageObserver io = new ImageObserver() {

                        public boolean imageUpdate(Image img, int infoflags, int x, int y, int width,
                                int height) {
                            // TODO Auto-generated method stub
                            return false;
                        }
                    };
                    g.drawImage(img.getImage(), x, y, getIconHeight(), getIconWidth(), io);

                    if (!vv.getPickedVertexState().isPicked(v)) {
                        g.setColor(Color.black);
                    } else {
                        g.setColor(Color.red);

                        properties.setModel(new CPIMTable(v));
                        runtimeProperties.setModel(new CPSMTable(v));
                    }
                    g.drawString(v.getName(), x - 10, y + 50);
                }
            };
        }
    });

    // create a frame to hold the graph
    final JFrame frame = new JFrame();
    Container content = frame.getContentPane();
    final GraphZoomScrollPane panel = new GraphZoomScrollPane(vv);
    content.add(panel, BorderLayout.CENTER);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    final ModalGraphMouse gm = new DefaultModalGraphMouse<Integer, Number>();
    vv.setGraphMouse(gm);

    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 save = new JButton("save");
    save.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showDialog(frame, "save");
            File result = fc.getSelectedFile();
            JsonCodec codec = new JsonCodec();
            OutputStream streamResult;
            try {
                streamResult = new FileOutputStream(result);
                codec.save(dmodel, streamResult);
            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }
    });
    JButton saveImage = new JButton("save as image");
    saveImage.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showDialog(frame, "save");
            File result = fc.getSelectedFile();
            JsonCodec codec = new JsonCodec();
            v.writeJPEGImage(result);
        }
    });
    //WE NEED TO UPDATE THE FACADE AND THE GUI
    JButton load = new JButton("load");
    load.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            JFileChooser fc = new JFileChooser();
            int returnVal = fc.showDialog(frame, "load");
            File result = fc.getSelectedFile();
            JsonCodec codec = new JsonCodec();
            try {
                InputStream stream = new FileInputStream(result);
                Deployment model = (Deployment) codec.load(stream);
                dmodel = model;
                v.setDeploymentModel(dmodel);
                ArrayList<Vertex> V = v.drawFromDeploymentModel();
                nodeTypes.removeAll();
                nodeTypes.setModel(fillList());

                properties.setModel(new CPIMTable(V.get(0)));
                runtimeProperties.setModel(new CPSMTable(V.get(0)));

                CommandFactory fcommand = new CommandFactory();
                CloudMlCommand load = fcommand.loadDeployment(result.getPath());
                cml.fireAndWait(load);

            } catch (FileNotFoundException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

        }
    });
    JButton minus = new JButton("-");
    minus.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            scaler.scale(vv, 1 / 1.1f, vv.getCenter());
        }
    });
    JButton deploy = new JButton("Deploy!");
    deploy.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent e) {
            //cad.deploy(dmodel);
            System.out.println("deploy");
            CommandFactory fcommand = new CommandFactory();
            CloudMlCommand deploy = fcommand.deploy();
            cml.fireAndWait(deploy);
        }
    });

    //right panel
    JPanel intermediary = new JPanel();
    intermediary.setLayout(new BoxLayout(intermediary, BoxLayout.PAGE_AXIS));
    intermediary.setBorder(BorderFactory.createLineBorder(Color.black));

    JLabel jlCPIM = new JLabel();
    jlCPIM.setText("CPIM");

    JLabel jlCPSM = new JLabel();
    jlCPSM.setText("CPSM");

    properties = new JTable(null);
    //properties.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JTableHeader h = properties.getTableHeader();
    JPanel props = new JPanel();
    props.setLayout(new BorderLayout());
    props.add(new JScrollPane(properties), BorderLayout.CENTER);
    props.add(h, BorderLayout.NORTH);

    runtimeProperties = new JTable(null);
    //runtimeProperties.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
    JTableHeader h2 = runtimeProperties.getTableHeader();
    JPanel runProps = new JPanel();
    runProps.setLayout(new BorderLayout());
    runProps.add(h2, BorderLayout.NORTH);
    runProps.add(new JScrollPane(runtimeProperties), BorderLayout.CENTER);

    intermediary.add(jlCPIM);
    intermediary.add(props);
    intermediary.add(jlCPSM);
    intermediary.add(runProps);

    content.add(intermediary, BorderLayout.EAST);

    //Left panel
    JPanel selection = new JPanel();
    JLabel nodes = new JLabel();
    nodes.setText("Types");
    selection.setLayout(new BoxLayout(selection, BoxLayout.PAGE_AXIS));
    nodeTypes = new JList(fillList());
    nodeTypes.setLayoutOrientation(JList.VERTICAL);
    nodeTypes.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
    nodeTypes.setVisibleRowCount(10);
    JScrollPane types = new JScrollPane(nodeTypes);
    types.setPreferredSize(new Dimension(150, 80));
    selection.add(nodes);
    selection.add(types);

    content.add(selection, BorderLayout.WEST);

    ((DefaultModalGraphMouse<Integer, Number>) gm)
            .add(new MyEditingGraphMousePlugin(0, vv, v.getGraph(), nodeTypes, dmodel));
    JPanel controls = new JPanel();
    controls.add(plus);
    controls.add(minus);
    controls.add(save);
    controls.add(saveImage);
    controls.add(load);
    controls.add(deploy);
    controls.add(((DefaultModalGraphMouse<Integer, Number>) gm).getModeComboBox());
    content.add(controls, BorderLayout.SOUTH);

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

From source file:com.hexidec.ekit.component.RelativeImageView.java

/**
  * Paints the image.//from  w w w .  j  a  v a  2 s . co m
  *
  * @param g the rendering surface to use
  * @param a the allocated region to render into
  * @see View#paint
  */
public void paint(Graphics g, Shape a) {
    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}

From source file:org.pmedv.core.components.RelativeImageView.java

/**
 * Paints the image.//from  w  w w  .j a v  a 2  s .c  om
 * 
 * @param g
 *            the rendering surface to use
 * @param a
 *            the allocated region to render into
 * @see View#paint
 */
public void paint(Graphics g, Shape a) {

    Color oldColor = g.getColor();
    fBounds = a.getBounds();
    int border = getBorder();
    int x = fBounds.x + border + getSpace(X_AXIS);
    int y = fBounds.y + border + getSpace(Y_AXIS);
    int width = fWidth;
    int height = fHeight;
    int sel = getSelectionState();

    // If no pixels yet, draw gray outline and icon
    if (!hasPixels(this)) {
        g.setColor(Color.lightGray);
        g.drawRect(x, y, width - 1, height - 1);
        g.setColor(oldColor);
        loadImageStatusIcons();
        Icon icon = ((fImage == null) ? sMissingImageIcon : sPendingImageIcon);
        if (icon != null) {
            icon.paintIcon(getContainer(), g, x, y);
        }
    }

    // Draw image
    if (fImage != null) {
        g.drawImage(fImage, x, y, width, height, this);
    }

    // If selected exactly, we need a black border & grow-box
    Color bc = getBorderColor();
    if (sel == 2) {
        // Make sure there's room for a border
        int delta = 2 - border;
        if (delta > 0) {
            x += delta;
            y += delta;
            width -= delta << 1;
            height -= delta << 1;
            border = 2;
        }
        bc = null;
        g.setColor(Color.black);
        // Draw grow box
        g.fillRect(x + width - 5, y + height - 5, 5, 5);
    }

    // Draw border
    if (border > 0) {
        if (bc != null) {
            g.setColor(bc);
        }
        // Draw a thick rectangle:
        for (int i = 1; i <= border; i++) {
            g.drawRect(x - i, y - i, width - 1 + i + i, height - 1 + i + i);
        }
        g.setColor(oldColor);
    }
}

From source file:edu.ku.brc.ui.ImageDisplay.java

@Override
protected void paintComponent(Graphics g) {
    super.paintComponent(g);

    boolean doScale = true;

    int w = getWidth();
    int h = getHeight();

    Image dspImg = image;// www. ja  va  2  s .  co m
    boolean doDisplayImage = (image != null && (!isNoAttachment && status == kImageOK)) || isLoading;
    if (isLoading) {
        doDisplayImage = true;
        dspImg = IconManager.getImage("Loading").getImage();
    }
    if (doDisplayImage && dspImg != null) {
        int imgW = dspImg.getWidth(null);
        int imgH = dspImg.getHeight(null);

        if (doScale && (imgW > w || imgH > h)) {
            double scaleW = 1.0;
            double scaleH = 1.0;
            double scale = 1.0;

            if (imgW > w) {
                scaleW = (double) w / imgW;
            }
            if (imgH > h) {
                scaleH = (double) h / imgH;
            }
            scale = Math.min(scaleW, scaleH);

            imgW = (int) (imgW * scale);
            imgH = (int) (imgH * scale);
        }

        int x = 0;
        int y = 0;

        if (imgW < w) {
            x = (w - imgW) / 2;
        }
        if (imgH < h) {
            y = (h - imgH) / 2;
        }
        g.drawImage(dspImg, x, y, imgW, imgH, null);

    } else if (doShowText) {
        GraphicsUtils.turnOnAntialiasedDrawing(g);
        String[] label = this.isNoAttachment ? (isFullImage ? noAttachmentStr : noThumnailStr)
                : loadingAttachmentStr;
        FontMetrics fm = g.getFontMetrics();
        int spacing = 2;
        int yOffset = (h - (fm.getHeight() + spacing) * label.length) / 2;
        if (yOffset < 0)
            yOffset = 0;
        int y = yOffset + fm.getHeight();
        for (String str : label) {
            g.drawString(str, (w - fm.stringWidth(str)) / 2, y);
            y += fm.getHeight() + 2;
        }
    }
}

From source file:net.rptools.maptool.client.ui.ChatTypingNotification.java

/**
 * This component is only made visible when there are notifications to be displayed. That means the first couple of
 * IF statements in this method are redundant since paintComponent() will not be called unless the component is
 * visible, and it will only be visible when there are notifications...
 *//*from w w  w  . j a va2  s  .  c om*/
@Override
protected void paintComponent(Graphics g) {
    //      System.out.println("Chat panel is painting itself...");
    if (AppPreferences.getTypingNotificationDuration() == 0) {
        return;
    }
    LinkedMap chatTypers = MapTool.getFrame().getChatNotificationTimers().getChatTypers();
    if (chatTypers == null || chatTypers.isEmpty()) {
        return;
    }
    Boolean showBackground = AppPreferences.getChatNotificationShowBackground();

    Graphics2D statsG = (Graphics2D) g.create();

    Font boldFont = AppStyle.labelFont.deriveFont(Font.BOLD);
    Font font = AppStyle.labelFont;
    FontMetrics valueFM = g.getFontMetrics(font);
    FontMetrics keyFM = g.getFontMetrics(boldFont);

    int PADDING7 = 7;
    int PADDING3 = 3;
    int PADDING2 = 2;

    BufferedImage img = AppStyle.panelTexture;
    int rowHeight = Math.max(valueFM.getHeight(), keyFM.getHeight());

    setBorder(null);
    int width = AppStyle.miniMapBorder.getRightMargin() + AppStyle.miniMapBorder.getLeftMargin();
    int height = getHeight() - PADDING2 + AppStyle.miniMapBorder.getTopMargin()
            + AppStyle.miniMapBorder.getBottomMargin();

    statsG.setFont(font);
    SwingUtil.useAntiAliasing(statsG);
    Rectangle bounds = new Rectangle(AppStyle.miniMapBorder.getLeftMargin(),
            height - getHeight() - AppStyle.miniMapBorder.getTopMargin(), getWidth() - width,
            getHeight() - AppStyle.miniMapBorder.getBottomMargin() - AppStyle.miniMapBorder.getTopMargin()
                    + PADDING2);

    int y = bounds.y + rowHeight;
    rowHeight = Math.max(rowHeight, AppStyle.chatImage.getHeight());

    setSize(getWidth(), ((chatTypers.size() * (PADDING3 + rowHeight)) + AppStyle.miniMapBorder.getTopMargin()
            + AppStyle.miniMapBorder.getBottomMargin()));

    if (showBackground) {
        g.drawImage(img, 0, 0, getWidth(), getHeight() + PADDING7, this);
        AppStyle.miniMapBorder.paintAround(statsG, bounds);
    }
    Rectangle rightRow = new Rectangle(AppStyle.miniMapBorder.getLeftMargin() + PADDING7,
            AppStyle.miniMapBorder.getTopMargin() + PADDING7, AppStyle.chatImage.getWidth(),
            AppStyle.chatImage.getHeight());

    Set<?> keySet = chatTypers.keySet();
    @SuppressWarnings("unchecked")
    Set<String> playerTimers = (Set<String>) keySet;
    for (String playerNamer : playerTimers) {
        if (showBackground) {
            statsG.setColor(new Color(249, 241, 230, 140));
            statsG.fillRect(bounds.x + PADDING3, y - keyFM.getAscent(),
                    (bounds.width - PADDING7 / 2) - PADDING3, rowHeight);
            statsG.setColor(new Color(175, 163, 149));
            statsG.drawRect(bounds.x + PADDING3, y - keyFM.getAscent(),
                    (bounds.width - PADDING7 / 2) - PADDING3, rowHeight);
        }
        g.drawImage(AppStyle.chatImage, bounds.x + 5, y - keyFM.getAscent(), (int) rightRow.getWidth(),
                (int) rightRow.getHeight(), this);

        // Values
        statsG.setColor(MapTool.getFrame().getChatTypingLabelColor());
        statsG.setFont(boldFont);
        statsG.drawString(I18N.getText("msg.commandPanel.liveTyping", playerNamer),
                bounds.x + AppStyle.chatImage.getWidth() + PADDING7 * 2, y + 5);

        y += PADDING2 + rowHeight;
    }
    if (showBackground) {
        AppStyle.shadowBorder.paintWithin(statsG, bounds);
    } else {
        setOpaque(false);
    }
}

From source file:com.jcraft.weirdx.XPixmap.java

@SuppressWarnings("unused")
void copyArea(XPixmap dst, GC gc, int sx, int sy, int dx, int dy, int w, int h) {
    if ((width - sx) < w)
        w = width - sx;/* w  w w . ja  va2  s .  c om*/
    if ((dst.width - dx) < w)
        w = dst.width - dx;

    if ((height - sy) < h)
        h = height - sy;
    if ((dst.height - dy) < h)
        h = dst.height - dy;

    int s = sy * width + sx;
    int d = dy * dst.width + dx;

    Graphics g = dst.getGraphics();
    Image ii = getImage(gc, sx, sy, w, h);

    if (sx == 0 && sy == 0 && w == width && h == height) {
        g.drawImage(ii, dx, dy, width, height, Screen.screen[0].root.ddxwindow);
    } else {
        java.awt.Shape tmp = g.getClip();
        g.setClip(dx, dy, w, h);
        g.drawImage(ii, dx - sx, dy - sy, width, height, Screen.screen[0].root.ddxwindow);

        if (tmp == null) {
            g.setClip(0, 0, dst.width, dst.height);
        } else {
            g.setClip(tmp);
        }
    }
    dst.draw(dx, dy, w, h);

    if (ii != getImage()) {
        ii.flush();
    }
}

From source file:com.t3.client.ui.ChatTypingNotification.java

/**
 * This component is only made visible when there are notifications to be displayed. That means the first couple of
 * IF statements in this method are redundant since paintComponent() will not be called unless the component is
 * visible, and it will only be visible when there are notifications...
 *//*from www.  j a  v a2s. c o m*/
@Override
protected void paintComponent(Graphics g) {
    //      System.out.println("Chat panel is painting itself...");
    if (AppPreferences.getTypingNotificationDuration() != 0) {
        LinkedMap<String, Long> chatTypers = TabletopTool.getFrame().getChatNotificationTimers()
                .getChatTypers();
        if (chatTypers == null || chatTypers.isEmpty()) {
            return;
        }
        Boolean showBackground = AppPreferences.getChatNotificationShowBackground();

        Graphics2D statsG = (Graphics2D) g.create();

        Font boldFont = AppStyle.labelFont.deriveFont(Font.BOLD);
        Font font = AppStyle.labelFont;
        FontMetrics valueFM = g.getFontMetrics(font);
        FontMetrics keyFM = g.getFontMetrics(boldFont);

        int PADDING7 = 7;
        int PADDING3 = 3;
        int PADDING2 = 2;

        BufferedImage img = AppStyle.panelTexture;
        int rowHeight = Math.max(valueFM.getHeight(), keyFM.getHeight());

        setBorder(null);
        int width = AppStyle.miniMapBorder.getRightMargin() + AppStyle.miniMapBorder.getLeftMargin();
        int height = getHeight() - PADDING2 + AppStyle.miniMapBorder.getTopMargin()
                + AppStyle.miniMapBorder.getBottomMargin();

        statsG.setFont(font);
        SwingUtil.useAntiAliasing(statsG);
        Rectangle bounds = new Rectangle(AppStyle.miniMapBorder.getLeftMargin(),
                height - getHeight() - AppStyle.miniMapBorder.getTopMargin(), getWidth() - width,
                getHeight() - AppStyle.miniMapBorder.getBottomMargin() - AppStyle.miniMapBorder.getTopMargin()
                        + PADDING2);

        int y = bounds.y + rowHeight;
        rowHeight = Math.max(rowHeight, AppStyle.chatImage.getHeight());

        setSize(getWidth(), ((chatTypers.size() * (PADDING3 + rowHeight))
                + AppStyle.miniMapBorder.getTopMargin() + AppStyle.miniMapBorder.getBottomMargin()));

        if (showBackground) {
            g.drawImage(img, 0, 0, getWidth(), getHeight() + PADDING7, this);
            AppStyle.miniMapBorder.paintAround(statsG, bounds);
        }
        Rectangle rightRow = new Rectangle(AppStyle.miniMapBorder.getLeftMargin() + PADDING7,
                AppStyle.miniMapBorder.getTopMargin() + PADDING7, AppStyle.chatImage.getWidth(),
                AppStyle.chatImage.getHeight());

        Set<?> keySet = chatTypers.keySet();
        @SuppressWarnings("unchecked")
        Set<String> playerTimers = (Set<String>) keySet;
        Color c1 = new Color(249, 241, 230, 140);
        Color c2 = new Color(175, 163, 149);
        for (String playerNamer : playerTimers) {
            if (showBackground) {
                statsG.setColor(c1);
                statsG.fillRect(bounds.x + PADDING3, y - keyFM.getAscent(),
                        (bounds.width - PADDING7 / 2) - PADDING3, rowHeight);
                statsG.setColor(c2);
                statsG.drawRect(bounds.x + PADDING3, y - keyFM.getAscent(),
                        (bounds.width - PADDING7 / 2) - PADDING3, rowHeight);
            }
            g.drawImage(AppStyle.chatImage, bounds.x + 5, y - keyFM.getAscent(), (int) rightRow.getWidth(),
                    (int) rightRow.getHeight(), this);

            // Values
            statsG.setColor(TabletopTool.getFrame().getChatTypingLabelColor());
            statsG.setFont(boldFont);
            statsG.drawString(I18N.getText("msg.commandPanel.liveTyping", playerNamer),
                    bounds.x + AppStyle.chatImage.getWidth() + PADDING7 * 2, y + 5);

            y += PADDING2 + rowHeight;
        }
        if (showBackground) {
            AppStyle.shadowBorder.paintWithin(statsG, bounds);
        } else {
            setOpaque(false);
        }
    }
}