Example usage for java.awt Graphics setColor

List of usage examples for java.awt Graphics setColor

Introduction

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

Prototype

public abstract void setColor(Color c);

Source Link

Document

Sets this graphics context's current color to the specified color.

Usage

From source file:net.geoprism.dashboard.DashboardMap.java

/**
 * Generate an image replicating the users map in the browser.
 * /*from  w  w w  . j  a  v a  2  s  .  c  om*/
 * @outFileFormat - Allowed types (png, gif, jpg, bmp)
 * @mapBounds - JSON constructed as {"bottom":"VALUE", "top":"VALUE", "left":"VALUE", "right":"VALUE"}
 * @mapSize - JSON constructed as {"width":"VALUE", "height":"VALUE"}
 * @activeBaseMap = JSON constructed as {"LAYER_SOURCE_TYPE":"VALUE"}
 */
@Override
public InputStream generateMapImageExport(String outFileFormat, String mapBounds, String mapSize,
        String activeBaseMap) {
    InputStream inStream = null;
    int width;
    int height;

    // Get dimensions of the map window (<div>)
    try {
        JSONObject mapSizeObj = new JSONObject(mapSize);
        width = mapSizeObj.getInt("width");
        height = mapSizeObj.getInt("height");
    } catch (JSONException e) {
        String error = "Could not parse map size.";
        throw new ProgrammingErrorException(error, e);
    }

    // Setup the base canvas to which we will add layers and map elements
    BufferedImage base = null;
    Graphics mapBaseGraphic = null;
    try {
        if (outFileFormat.toLowerCase().equals("png") || outFileFormat.toLowerCase().equals("gif")) {
            base = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        } else if (outFileFormat.equals("jpg") || outFileFormat.toLowerCase().equals("bmp")) {
            base = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        }

        // Create the base canvas that all other map elements will be draped on top of
        mapBaseGraphic = base.getGraphics();
        mapBaseGraphic.setColor(Color.white);
        mapBaseGraphic.fillRect(0, 0, width, height);
        mapBaseGraphic.drawImage(base, 0, 0, null);

        // Ordering the layers from the default map
        DashboardLayer[] orderedLayers = this.getOrderedLayers();

        // Add layers to the base canvas
        BufferedImage layerCanvas = getLayersExportCanvas(width, height, orderedLayers, mapBounds);

        // Get base map
        String baseType = null;
        try {
            JSONObject activeBaseObj = new JSONObject(activeBaseMap);
            baseType = activeBaseObj.getString("LAYER_SOURCE_TYPE");
        } catch (JSONException e) {
            String error = "Could not active base map JSON.";
            throw new ProgrammingErrorException(error, e);
        }

        // Get bounds of the map
        if (baseType.length() > 0) {
            String bottom;
            String top;
            String right;
            String left;
            try {
                JSONObject mapBoundsObj = new JSONObject(mapBounds);
                bottom = mapBoundsObj.getString("bottom");
                top = mapBoundsObj.getString("top");
                right = mapBoundsObj.getString("right");
                left = mapBoundsObj.getString("left");
            } catch (JSONException e) {
                String error = "Could not parse map bounds.";
                throw new ProgrammingErrorException(error, e);
            }

            BufferedImage baseMapImage = this.getBaseMapCanvas(width, height, left, bottom, right, top,
                    baseType);

            if (baseMapImage != null) {
                mapBaseGraphic.drawImage(baseMapImage, 0, 0, null);
            }
        }

        // Offset the layerCanvas so that it is center
        int widthOffset = (int) ((width - layerCanvas.getWidth()) / 2);
        int heightOffset = (int) ((height - layerCanvas.getHeight()) / 2);

        mapBaseGraphic.drawImage(layerCanvas, widthOffset, heightOffset, null);

        // Add legends to the base canvas
        BufferedImage legendCanvas = getLegendExportCanvas(width, height);
        mapBaseGraphic.drawImage(legendCanvas, 0, 0, null);
    } finally {
        ByteArrayOutputStream outStream = null;
        try {
            outStream = new ByteArrayOutputStream();
            ImageIO.write(base, outFileFormat, outStream);
            inStream = new ByteArrayInputStream(outStream.toByteArray());
        } catch (IOException e) {
            String error = "Could not write map image to the output stream.";
            throw new ProgrammingErrorException(error, e);
        } finally {
            if (outStream != null) {
                try {
                    outStream.close();
                } catch (IOException e) {
                    String error = "Could not close stream.";
                    throw new ProgrammingErrorException(error, e);
                }
            }
        }

        if (mapBaseGraphic != null) {
            mapBaseGraphic.dispose();
        }
    }

    return inStream;
}

From source file:com.openbravo.pos.util.JRViewer400.java

protected void drawPageError(Graphics grx) {
    PrintPageFormat pageFormat = getPageFormat();
    grx.setColor(Color.white);
    grx.fillRect(0, 0, pageFormat.getPageWidth() + 1, pageFormat.getPageHeight() + 1);
}

From source file:replicatorg.app.ui.panels.ControlPanel.java

public ControlPanel() {
    super(Base.getMainWindow(), Dialog.ModalityType.MODELESS);
    initComponents();/* w  w w.  jav a2s.c o  m*/
    Base.writeLog("Control panel opened...", this.getClass());
    setTextLanguage();
    super.centerOnScreen();
    super.enableDrag();
    evaluateInitialConditions();

    this.tempPanel.setLayout(new GridBagLayout());
    this.tempPanel.add(this.makeChart());

    //Sets legend colors
    BufferedImage image = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    Graphics g = image.getGraphics();
    g.setColor(t0MeasuredColor);
    g.fillRect(0, 0, 10, 10);
    Icon icon1 = new ImageIcon(image);

    this.colorCurrentTemp.setIcon(icon1);
    this.colorCurrentTemp.setText("");

    BufferedImage image2 = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);
    Graphics g2 = image2.getGraphics();
    g2.setColor(t0TargetColor);
    g2.fillRect(0, 0, 10, 10);
    Icon icon2 = new ImageIcon(image2);

    this.colorTargetTemp.setIcon(icon2);
    this.colorTargetTemp.setText("");

    if (connectedPrinter == PrinterInfo.BEETHEFIRST) {
        jSliderBlowerSpeed.setMaximum(1);
        jSliderBlowerSpeed.setMinorTickSpacing(0);
        jSliderBlowerSpeed.setMajorTickSpacing(1);

        jSliderExtruderSpeed.setEnabled(false);
    }

    this.addWindowListener(new WindowAdapter() {

        @Override
        public void windowOpened(WindowEvent e) {
            consoleInput.requestFocus();
        }

        @Override
        public void windowClosed(WindowEvent e) {
            if (movButtonHoldDown != null && movButtonHoldDown.isRunning()) {
                movButtonHoldDown.stop();
            }
            driver.dispatchCommand("M1110 S0", COM.NO_RESPONSE);

            if (loggingTemperature == false) {
                disposeThread.cancel();
            }
            inputValidationThread.cancel();
            getInitialValuesThread.cancel();
        }
    });
}

From source file:com.projity.contrib.calendar.JXXMonthView.java

private void drawDay(boolean colored, boolean flagged, boolean today, Graphics g, String text, int x, int y) {
    Color defaultColor = g.getColor();
    Font oldFont = getFont();//from  ww  w  . jav  a  2s. c o  m
    if (colored) {
        g.setColor(Color.LIGHT_GRAY);
        g.fillRect(_bounds.x, _bounds.y, _bounds.width, _bounds.height);
        g.setColor(defaultColor);
    }
    if (today)
        g.setColor(Color.BLUE);
    if (flagged) {
        g.setColor(Color.RED);
        g.setFont(_derivedFont);
    }
    g.drawString(text, x, y);
    g.setColor(defaultColor);
    g.setFont(oldFont);
}

From source file:CopyAreaPerformance.java

protected void paintComponent(Graphics g) {
    long startTime = System.nanoTime();
    // prevVX is set to -10000 when first enabled
    if (useCopyArea && prevVX > -9999) {
        // Most of this code determines the proper areas to copy and clip
        int scrollX = viewX - prevVX;
        int scrollY = viewY - prevVY;
        int copyFromY, copyFromX;
        int clipFromY, clipFromX;
        if (scrollX == 0) {
            // vertical scroll
            if (scrollY < 0) {
                copyFromY = 0;/*from   ww  w . j a v a  2s  .c  o  m*/
                clipFromY = 0;
            } else {
                copyFromY = scrollY;
                clipFromY = getHeight() - scrollY;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(0, copyFromY, getWidth(), getHeight() - Math.abs(scrollY), 0, -scrollY);
            g.setClip(0, clipFromY, getWidth(), Math.abs(scrollY));
        } else {
            // horizontal scroll
            if (scrollX < 0) {
                copyFromX = 0;
                clipFromX = 0;
            } else {
                copyFromX = scrollX;
                clipFromX = getWidth() - scrollX;
            }
            // copy the old content, set the clip to the new area
            g.copyArea(copyFromX, 0, getWidth() - Math.abs(scrollX), getHeight(), -scrollX, 0);
            g.setClip(clipFromX, 0, Math.abs(scrollX), getHeight());
        }
    }
    // Track previous view position for next scrolling operation
    prevVX = viewX;
    prevVY = viewY;
    // Get the clip in case we need it later
    Rectangle clipRect = g.getClip().getBounds();
    int clipL = (int) (clipRect.getX());
    int clipT = (int) (clipRect.getY());
    int clipR = (int) (clipRect.getMaxX());
    int clipB = (int) (clipRect.getMaxY());
    g.setColor(Color.WHITE);
    g.fillRect(clipL, clipT, (int) clipRect.getWidth(), (int) clipRect.getHeight());
    for (int column = 0; column < 256; ++column) {
        int x = column * (SMILEY_SIZE + PADDING) - viewX;
        if (useClip) {
            if (x > clipR || (x + (SMILEY_SIZE + PADDING)) < clipL) {
                // trivial reject; outside to the left or right
                continue;
            }
        }
        for (int row = 0; row < 256; ++row) {
            int y = row * (SMILEY_SIZE + PADDING) - viewY;
            if (useClip) {
                if (y > clipB || (y + (SMILEY_SIZE + PADDING)) < clipT) {
                    // trivial reject; outside to the top or bottom
                    continue;
                }
            }
            Color faceColor = new Color(column, row, 0);
            drawSmiley(g, faceColor, x, y);
        }
    }
    long stopTime = System.nanoTime();
    System.out.println("Painted in " + ((stopTime - startTime) / 1000000) + " ms");
}

From source file:MyJava3D.java

public void drawFacet(Graphics graphics, GeometryArray geometryArray, int index, Point3d[] pointArray,
        int numPoints) {
    int intensity = computeIntensity(geometryArray, index, numPoints);

    if (drawBackface || intensity >= 1) {
        for (int n = 0; n < numPoints; n++) {
            xCoordArray[n] = (int) pointArray[n].x;
            yCoordArray[n] = (int) pointArray[n].y;
        }/*from w  w  w  .  j a  v a  2 s  . c  o m*/

        graphics.setColor(new Color(intensity, intensity, intensity));
        graphics.drawPolygon(xCoordArray, yCoordArray, numPoints);
    }
}

From source file:org.pmedv.blackboard.components.BoardEditor.java

public void updateStatusBar() {
    log.debug("editor update");
    StringBuffer text = new StringBuffer();
    if (snapToGrid)
        text.append(resources.getResourceByKey("mode.snap"));
    else//www  .  j a  v  a2 s.  c o  m
        text.append(resources.getResourceByKey("mode.nosnap"));
    text.append(" | ");
    if (editorMode.equals(EditorMode.SELECT))
        text.append(resources.getResourceByKey("mode.select"));
    else if (editorMode.equals(EditorMode.DRAW_LINE))
        text.append(resources.getResourceByKey("mode.line"));
    else if (editorMode.equals(EditorMode.DRAW_RECTANGLE))
        text.append(resources.getResourceByKey("mode.box"));
    else if (editorMode.equals(EditorMode.DRAW_ELLIPSE))
        text.append(resources.getResourceByKey("mode.ellipse"));
    else if (editorMode.equals(EditorMode.CHECK_CONNECTIONS))
        text.append(resources.getResourceByKey("mode.check"));
    else if (editorMode.equals(EditorMode.MOVE))
        text.append(resources.getResourceByKey("mode.move"));

    text.append(" | ");

    if (magnetic)
        text.append(resources.getResourceByKey("mode.magnetic"));

    BufferedImage image = new BufferedImage(16, 16, BufferedImage.TYPE_INT_ARGB);
    Graphics g = image.getGraphics();
    g.setColor(palette.getCurrentColor());
    g.fillRect(0, 0, 16, 16);
    win.getStatusLabel().setText(text.toString());
    win.getStatusLabel().setIcon(new ImageIcon(image));
    if (saveBoardCommand != null && fileState != null)
        saveBoardCommand.setEnabled(fileState.equals(FileState.DIRTY));
    refresh();
}

From source file:com.projity.contrib.calendar.JXXMonthView.java

/**
 * Paint the background for a selected day. The default is a filled
 * rectangle in the in the component's background color.
 *
 * @param g//www  .j a  v  a2  s . co  m
 *            Graphics object to paint to.
 * @param x
 *            x-coordinate of upper left corner.
 * @param y
 *            y-coordinate of upper left corner.
 * @param width
 *            width of bounding box for the day.
 * @param height
 *            height of bounding box for the day.
 */
protected void paintSelectedDayBackground(Graphics g, int x, int y, int width, int height) {
    //      g.setColor(getSelectedBackground());
    //      g.fillRect(x, y, width, height);
    //      PROJITY_MODIFICATION
    g.setColor(_todayBackgroundColor);
    g.drawRect(x, y, width - 1, height - 1);
}

From source file:edu.ku.brc.af.ui.forms.validation.ValFormattedTextFieldSingle.java

@Override
public void paint(Graphics g) {
    super.paint(g);

    String text = getText();//from   w w  w .ja va  2 s.  com

    //System.err.println("isViewOnly "+isViewOnly+"  isEnabled() "+isEnabled()+ "  ["+text+"]  "+(text.length() < bgStr.length()));
    if (!isViewOnly && needsUpdating && isEnabled() && text != null && text.length() < bgStr.length()) {
        FontMetrics fm = g.getFontMetrics();
        int w = fm.stringWidth(text);
        pnt = new Point(inner.left + w, inner.top + fm.getAscent());

        Rectangle r = g.getClipBounds();
        Dimension s = getSize();
        Insets i2 = getBorder().getBorderInsets(this);
        int x = i2.left - 1;
        int y = i2.top - 1;
        //int ww = s.width - i2.right + 1;
        int hh = s.height - i2.bottom + 1;

        String str = bgStr.substring(text.length(), bgStr.length());
        int bgW = fm.stringWidth(str);

        g.setClip(x + w, y, Math.min(x + bgW, g.getClipBounds().width - x), hh);

        g.setColor(textColor);
        g.drawString(str, pnt.x, pnt.y);

        g.setClip(r.x, r.y, r.width, r.height); // reset clip
    }

    //System.out.println(hashCode() + " isNew: " +isNew+"  valState: "+valState+"    isEnabled: "+isEnabled());
    // 3/2/09 - rods - removing !isNew from the condition
    //if (!isNew && valState == UIValidatable.ErrorType.Error && isEnabled())
    if (valState == UIValidatable.ErrorType.Error && isEnabled()) {
        UIHelper.drawRoundedRect((Graphics2D) g, isNew ? new Color(249, 249, 0) : valTextColor.getColor(),
                getSize(), 1);
    } else if (valState == UIValidatable.ErrorType.Incomplete && isEnabled()) {
        UIHelper.drawRoundedRect((Graphics2D) g, new Color(249, 249, 0), getSize(), 1);
    }
}

From source file:edu.ku.brc.specify.tasks.subpane.qb.QueryFieldPanel.java

@Override
public void paint(Graphics g) {
    // TODO Auto-generated method stub
    super.paint(g);

    if (selected) {
        //this block was copied from RolloverCommand.paintComp()

        g.setColor(RolloverCommand.getActiveColor());
        Insets insets = getInsets();
        insets.set(1, 1, 1, 1);//from ww w .ja v  a 2 s. com
        Dimension size = getSize();
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        RoundRectangle2D.Double rr = new RoundRectangle2D.Double(insets.left, insets.top,
                size.width - insets.right - insets.left, size.height - insets.bottom - insets.top, 10, 10);
        g2d.draw(rr);
        rr = new RoundRectangle2D.Double(insets.left + 1, insets.top + 1,
                size.width - insets.right - insets.left - 2, size.height - insets.bottom - insets.top - 2, 10,
                10);
        g2d.draw(rr);
    }
}