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:edu.ku.brc.af.prefs.GenericPrefsPanel.java

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

    if (shadeColor != null) {
        Dimension size = getSize();
        g.setColor(shadeColor);
        g.fillRect(0, 0, size.width, size.height);
    }//ww  w. j av  a2  s  .c  o m
}

From source file:edu.mit.fss.tutorial.part4.ControlPanel.java

@Override
public void paint(Graphics g) {
    // Calls the super-class paint method for the default background.
    super.paint(g);

    // Set color to gray and draw the x-axis (Y=0) and y=axis (X=0).
    g.setColor(Color.gray);
    g.drawLine(0, getHeight() / 2, getWidth(), getHeight() / 2);
    g.drawLine(getWidth() / 2, 0, getWidth() / 2, getHeight());

    // Make sure no changes to elements can take place while painting.
    synchronized (elements) {
        // For each surface element...
        for (SurfaceElement e : elements) {
            if (e instanceof MobileElement) {
                // If it is a MobileElement, use blue color.
                g.setColor(Color.blue);
            } else {
                // Otherwise use black color.
                g.setColor(Color.black);
            }//  w w  w  .  j  a  va 2s .  com
            // Determine the screen location for the element.
            int[] location = getScreenLocation(e.getPosition());

            // Fill an oval at the location (offset by half the oval size).
            g.fillOval(location[0] - ELEMENT_SIZE / 2, location[1] - ELEMENT_SIZE / 2, ELEMENT_SIZE,
                    ELEMENT_SIZE);

            // Draw the element name to the right of the oval, offset by
            // 2 pixels to the right and half the oval size vertically.
            g.drawString(e.getName(), location[0] + ELEMENT_SIZE / 2 + 2,
                    location[1] + ELEMENT_SIZE / 2 + ELEMENT_SIZE / 2);
        }
    }
}

From source file:contactangle.ImageControl.java

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

    if (img != null)
        g.drawImage(img, 0, 0, this);
    else//from   w w w.  j  av  a2s. co m
        g.drawString("NO IMAGE", 0, 15);

    g.setColor(Color.RED);

    if (x1 < x2 && y1 < y2)
        g.drawRect(x1, y1, x2 - x1, y2 - y1);
    else if (x1 >= x2 && y1 < y2)
        g.drawRect(x2, y1, x1 - x2, y2 - y1);
    else if (x1 < x2 && y1 >= y2)
        g.drawRect(x1, y2, x2 - x1, y1 - y2);
    else
        g.drawRect(x2, y2, x1 - x2, y1 - y2);

    if (valid && img != null) {

        if (x1 >= x2) {
            int temp = x1;
            x1 = x2;
            x2 = temp;
        }
        if (y1 >= y2) {
            int temp = y1;
            y1 = y2;
            y2 = temp;
        }

        choosenPoints = new ArrayList<Point>();
        for (int y = y1; y < y2; y++) {
            for (int x = x1; x < x2; x++) {
                int pixelData = img.getRGB(x, y);
                if (pixelData == -1)
                    choosenPoints.add(new Point(x, y));
            }
        }

        SimpleRegression reg = new SimpleRegression();
        for (Point p : choosenPoints) {
            reg.addData(p.x, p.y);
        }

        int firstX = choosenPoints.get(0).x;
        int firstY = choosenPoints.get(0).y;
        double slope = reg.getSlope();
        g.setColor(Color.GREEN);
        g.drawLine(firstX, firstY, firstX + (70), firstY + (int) (slope * (70)));
        g.drawLine(firstX, firstY, firstX - (70), firstY - (int) (slope * (70)));

        double contactDegrees = (Math.atan(reg.getSlope()) / (2 * Math.PI)) * 360.0;

        DecimalFormat d = new DecimalFormat("##.###");

        g.drawString("Contact Angle = ", 25, 25);
        g.drawString(d.format(contactDegrees) + " degrees", 25, 38);
    }

}

From source file:fr.ign.cogit.simplu3d.rjmcmc.generic.visitor.FilmVisitor.java

/**
 * Permet d'enregistrer une image  partir de l'cran Ne fonctionne qu'avec
 * l'IHM actuel (Offset ncessaire) Ne prends pas compte de l'existance d'un
 * fichier de mme nom//  w  w  w. ja  v a  2s  .  com
 * 
 * @param path
 *            le dossier dans lequel l'impr ecran sera supprime
 * @param fileName
 *            le nom du fichier
 * @return indique si la capture s'est effectue avec succs
 */
public boolean screenCapture(String path, String fileName, InterfaceMap3D iMap3D) {

    try {

        ChartPanel v = StatsVisitor.CHARTSINGLETON;

        int xSup = 0;
        int ySup = 0;

        boolean hasStats = (v != null);

        if (hasStats) {

            xSup = v.getSize().width;
            ySup = v.getSize().height;

        }

        int xSize = iMap3D.getSize().width + xSup;
        int ySize = Math.max(iMap3D.getSize().height, ySup);

        BufferedImage bufImage = new BufferedImage(xSize, ySize, BufferedImage.TYPE_INT_RGB);

        Graphics g = bufImage.createGraphics();

        g.setColor(Color.white);

        // g.drawRect(0, 0, xSize, ySize);

        g.fillRect(0, 0, xSize, ySize);

        iMap3D.getCanvas3D().paint(g);

        if (hasStats) {

            g.drawImage(v.getChart().createBufferedImage(xSup, ySup), iMap3D.getSize().width,
                    (ySize - ySup) / 2, null);

        }

        File fichier = new File(path, fileName);
        if (fichier.exists()) {
            System.out.println("Fail");
            return false;
        } else {
            ImageIO.write(bufImage, "jpg", fichier);
            return true;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:ColorSwatch.java

/**
 * Paints this Icon into the provided graphics context.
 *//*  w  w  w  .j  a va  2  s .c  om*/
public void paintIcon(Component c, Graphics g, int x, int y) {
    if (ourSwatchIsVoid)
        return;

    Color oldColor = g.getColor();

    if (ourSwatchIsMultiColor) {
        g.setColor(Color.white);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
        g.setColor(ourBorderColor);
        for (int i = 0; i < ourSwatchSize; i += 2) {
            g.drawLine(x + i, y, x + i, y + ourSwatchSize);
        }
    }

    else if (ourSwatchColor != null) {
        g.setColor(ourSwatchColor);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    else {
        g.setColor(Color.white);
        g.fillRect(x, y, ourSwatchSize, ourSwatchSize);
        g.setColor(ourBorderColor);
        g.drawLine(x, y, x + ourSwatchSize, y + ourSwatchSize);
        g.drawLine(x, y + ourSwatchSize, x + ourSwatchSize, y);
    }

    if (ourBorderPainted) {
        g.setColor(ourBorderColor);
        g.drawRect(x, y, ourSwatchSize, ourSwatchSize);
    }

    g.setColor(oldColor);
}

From source file:ArrowIcon.java

protected void paintArrowBevel(Graphics g, Color base, int x, int y) {
    int len = size;
    int xx = x;/*  ww w  .java2 s . c  om*/
    int yy = y;
    Color c2 = deriveColorHSB(base, 0f, 0f, (-DB) * (size / 2));
    while (len >= 2) {
        c2 = deriveColorHSB(c2, 0f, 0f, DB);
        g.setColor(c2);
        g.fillRect(xx, yy, 1, 1);
        g.fillRect(xx + len - 1, yy, 1, 1);
        len -= 2;
        xx++;
        yy++;
    }

}

From source file:AccessibleScrollDemo.java

public void paintComponent(Graphics g) {
    // Fill me with dirty brown/orange.
    g.setColor(new Color(230, 163, 4));
    g.fillRect(0, 0, getWidth(), getHeight());
}

From source file:MouseDrag.java

public void paint(Graphics g) {
    int w = curX - startX, h = curY - startY;
    Dimension d = getSize();//from www.j a v  a2  s  . c o  m
    g.drawImage(curImage, 0, 0, d.width, d.height, this);
    if (startX < 0 || startY < 0)
        return;
    System.err.println("paint:drawRect @[" + startX + "," + startY + "] size " + w + "x" + h);
    g.setColor(Color.red);
    g.fillRect(startX, startY, w, h);
}

From source file:FileTree3.java

public void paintComponent(Graphics g) {
    Color bColor = getBackground();
    Icon icon = getIcon();/* www .j  av a  2s .  co  m*/

    g.setColor(bColor);
    int offset = 0;
    if (icon != null && getText() != null)
        offset = (icon.getIconWidth() + getIconTextGap());
    g.fillRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);

    if (m_selected) {
        g.setColor(m_borderSelectionColor);
        g.drawRect(offset, 0, getWidth() - 1 - offset, getHeight() - 1);
    }

    super.paintComponent(g);
}

From source file:AlphaTest.java

/**
 * Plots the Alpha function into the graph area of the plot.
 *//*  w  w  w  .  j  a  v a  2 s .c om*/
protected void drawAlpha(Graphics g, long lMaxTime) {
    g.setColor(Color.blue);

    float value = 0;

    m_Alpha.setStartTime(0);

    double x1 = 0;
    double y1 = 0;
    double x2 = 0;
    double y2 = 0;

    for (long lTime = 0; lTime <= lMaxTime; lTime += m_kTimeStep) {
        value = m_Alpha.value(lTime);

        x2 = lTime * m_ScaleX;
        y2 = value * m_nGraphMaxHeight;

        drawGraphLine(g, x1, y1, x2, y2);

        x1 = x2;
        y1 = y2;
    }

    g.setColor(Color.black);
}