Example usage for java.awt Graphics fillRect

List of usage examples for java.awt Graphics fillRect

Introduction

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

Prototype

public abstract void fillRect(int x, int y, int width, int height);

Source Link

Document

Fills the specified rectangle.

Usage

From source file:Main.java

public static void fillInverseVerGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dx = (double) w / (double) steps;
    int x1 = x;/*from w w w.j  a  va  2 s .co m*/
    for (int i = 0; i < steps; i++) {
        int x2 = x + (int) Math.round((double) i * dx);
        g.setColor(colors[colors.length - i - 1]);
        if (i == (steps - 1)) {
            g.fillRect(x1, y, x + w - x1, h);
        } else {
            g.fillRect(x1, y, x2 - x1, h);
        }
        x1 = x2;
    }
}

From source file:Main.java

/**
 * Paints string with underlined character at the specified index.
 *
 * @param g               graphics context
 * @param text            painted text/*  www  .j  a  v a 2 s  .com*/
 * @param underlinedIndex underlined character index
 * @param x               text X coordinate
 * @param y               text Y coordinate
 */
public static void drawStringUnderlineCharAt(final Graphics g, final String text, final int underlinedIndex,
        final int x, final int y) {
    // Painting string
    drawString(g, text, x, y);

    // Painting character underline
    if (underlinedIndex >= 0 && underlinedIndex < text.length()) {
        final FontMetrics fm = g.getFontMetrics();
        g.fillRect(x + fm.stringWidth(text.substring(0, underlinedIndex)), y + fm.getDescent() - 1,
                fm.charWidth(text.charAt(underlinedIndex)), 1);
    }
}

From source file:com.klwork.common.utils.WebUtils.java

/**
 * ???//www  . j  a  va2  s  .c o m
 * @param request
 * @param response
 */
public static void VerificationCode(HttpServletRequest request, HttpServletResponse response) throws Exception {
    response.setHeader("Pragma", "No-cache");
    response.setHeader("Cache-Control", "no-cache");
    response.setDateHeader("Expires", 0);
    // 
    int width = 60, height = 20;
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);

    // ?
    Graphics g = image.getGraphics();

    // ??
    Random random = new Random();

    // 
    g.setColor(getRandColor(200, 250));
    g.fillRect(0, 0, width, height);

    // 
    g.setFont(new Font("Times New Roman", Font.PLAIN, 18));

    // 
    // g.setColor(new Color());
    // g.drawRect(0,0,width-1,height-1);

    // ?155?????
    g.setColor(getRandColor(160, 200));
    for (int i = 0; i < 155; i++) {
        int x = random.nextInt(width);
        int y = random.nextInt(height);
        int xl = random.nextInt(12);
        int yl = random.nextInt(12);
        g.drawLine(x, y, x + xl, y + yl);
    }

    String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQUSTUVWXYZ0123456789";
    // ????(4?)
    String sRand = "";
    for (int i = 0; i < 4; i++) {
        int start = random.nextInt(base.length());
        String rand = base.substring(start, start + 1);
        sRand = sRand.concat(rand);
        // ??
        g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));
        g.drawString(rand, 13 * i + 6, 16);
    }

    // ??SESSION
    request.getSession().setAttribute("entrymrand", sRand);

    // 
    g.dispose();
    OutputStream out = response.getOutputStream();
    // ?
    ImageIO.write(image, "JPEG", out);

    out.flush();
    out.close();
}

From source file:Main.java

public static void fillHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dy = (double) h / (double) (steps);
    if (dy <= 3.001) {
        int y1 = y;
        for (int i = 0; i < steps; i++) {
            int y2 = y + (int) Math.round((double) i * dy);
            g.setColor(colors[i]);//  w  w  w  . j  a  v  a2s .  c om
            if (i == (steps - 1)) {
                g.fillRect(x, y1, w, y + h - y1);
            } else {
                g.fillRect(x, y1, w, y2 - y1);
            }
            y1 = y2;
        }
    } else {
        smoothFillHorGradient(g, colors, x, y, w, h);
    }
}

From source file:Main.java

public static void fillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dy = (double) h / (double) steps;
    if (dy <= 3.001) {
        int y1 = y;
        for (int i = 0; i < steps; i++) {
            int y2 = y + (int) Math.round((double) i * dy);
            g.setColor(colors[colors.length - i - 1]);
            if (i == (steps - 1)) {
                g.fillRect(x, y1, w, y + h - y1);
            } else {
                g.fillRect(x, y1, w, y2 - y1);
            }/*from www.  jav  a 2s.  c  o  m*/
            y1 = y2;
        }
    } else {
        smoothFillInverseHorGradient(g, colors, x, y, w, h);
    }

}

From source file:net.sf.webphotos.tools.Thumbnail.java

private static boolean save(Image thumbnail, String nmArquivo) {
    try {/*from w  w w . ja v  a  2 s  .  co  m*/
        // This code ensures that all the
        // pixels in the image are loaded.
        Image temp = new ImageIcon(thumbnail).getImage();

        // Create the buffered image.
        BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
                BufferedImage.TYPE_INT_RGB);

        // Copy image to buffered image.
        Graphics g = bufferedImage.createGraphics();

        // Clear background and paint the image.
        g.setColor(Color.white);
        g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
        g.drawImage(temp, 0, 0, null);
        g.dispose();

        // write the jpeg to a file
        File file = new File(nmArquivo);
        // Recria o arquivo se existir
        if (file.exists()) {
            Util.out.println("Redefinindo a Imagem: " + nmArquivo);
            file.delete();
            file = new File(nmArquivo);
        }

        // encodes image as a JPEG file
        ImageIO.write(bufferedImage, IMAGE_FORMAT, file);

        return true;
    } catch (IOException ioex) {
        ioex.printStackTrace(Util.err);
        return false;
    }
}

From source file:rega.genotype.ui.util.GenotypeLib.java

public static void scalePNG(File in, File out, double perc) throws IOException {
    Image i = ImageIO.read(in);
    Image resizedImage = null;/*from   w w w .  j  a  va2s .  c  om*/

    int newWidth = (int) (i.getWidth(null) * perc / 100.0);
    int newHeight = (int) (i.getHeight(null) * perc / 100.0);

    resizedImage = i.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);

    // This code ensures that all the pixels in the image are loaded.
    Image temp = new ImageIcon(resizedImage).getImage();

    // Create the buffered image.
    BufferedImage bufferedImage = new BufferedImage(temp.getWidth(null), temp.getHeight(null),
            BufferedImage.TYPE_INT_RGB);

    // Copy image to buffered image.
    Graphics g = bufferedImage.createGraphics();

    // Clear background and paint the image.
    g.setColor(Color.white);
    g.fillRect(0, 0, temp.getWidth(null), temp.getHeight(null));
    g.drawImage(temp, 0, 0, null);
    g.dispose();

    // Soften.
    float softenFactor = 0.05f;
    float[] softenArray = { 0, softenFactor, 0, softenFactor, 1 - (softenFactor * 4), softenFactor, 0,
            softenFactor, 0 };
    Kernel kernel = new Kernel(3, 3, softenArray);
    ConvolveOp cOp = new ConvolveOp(kernel, ConvolveOp.EDGE_NO_OP, null);
    bufferedImage = cOp.filter(bufferedImage, null);

    ImageIO.write(bufferedImage, "png", out);
}

From source file:Main.java

public static void smoothFillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    Graphics2D g2D = (Graphics2D) g;
    Paint savedPaint = g2D.getPaint();
    int steps = colors.length;
    double dy = (double) h / (double) steps;
    int y1 = y;//from w  ww.  j a  v a2s . c o m
    for (int i = 0; i < steps; i++) {
        int y2 = y + (int) Math.round((double) i * dy);
        g.setColor(colors[colors.length - i - 1]);
        if (i == (steps - 1)) {
            g2D.setPaint(null);
            g2D.setColor(colors[colors.length - i - 1]);
            g.fillRect(x, y1, w, y + h - y1);
        } else {
            g2D.setPaint(new GradientPaint(0, y1, colors[colors.length - i - 1], 0, y2,
                    colors[colors.length - i - 2]));
            g.fillRect(x, y1, w, y2 - y1);
        }
        y1 = y2;
    }
    g2D.setPaint(savedPaint);
}

From source file:Main.java

public static void drawImage(BufferedImage image, Graphics g, Component parent) {
    Dimension size = parent.getSize();
    Color background = parent.getBackground();
    if (image != null && size.width > 0) {
        double ratio = (double) image.getHeight(null) / image.getWidth(null);

        int effectiveWidth = 1;
        int effectiveHeight = (int) ratio;

        while (effectiveHeight < size.height && effectiveWidth < size.width) {
            effectiveWidth++;//from  w w w  .j  a v  a 2 s  .c  o  m
            effectiveHeight = (int) (ratio * effectiveWidth);
        }

        g.setColor(background);
        g.fillRect(0, 0, size.width, size.height);

        int cornerx = Math.abs((size.width - effectiveWidth) / 2);
        int cornery = Math.abs((size.height - effectiveHeight) / 2);
        g.drawImage(image, cornerx, cornery, effectiveWidth + cornerx, effectiveHeight + cornery, 0, 0,
                image.getWidth(null), image.getHeight(null), null);
    }
}

From source file:smanilov.mandelbrot.compute.Computer.java

/**
 * Creates a thread that draws points from the toDoList.
 * @param width The maximum x coordinate.
 * @param height The maximum y coordinate.
 * @return/*w  w  w . ja  v  a  2  s  . c  om*/
 */
private static Thread createShaderThread(final Image drawing, final Color foregroundColor,
        final Color backgroundColor, final ReentrantLock drawingLock, final int scale, final Point2D center) {
    Thread shaderThread = new Thread() {

        @Override
        public void run() {
            System.out.println("Shader: [START]");
            int id = currentDrawingId;
            int currentIterations = iterations;
            super.run();

            int width = drawing.getWidth(null);
            int height = drawing.getHeight(null);

            Point pixelCenter = new Point(width / 2, height / 2);

            while (active) {
                // TODO: remove busy-wait
                while (true) {
                    queueLock.lock();
                    if (toDoList.size() == 0) {
                        queueLock.unlock();
                        break;
                    }
                    Point p = toDoList.poll();
                    int i = p.x;
                    int j = p.y;
                    queueLock.unlock();

                    double k = 0;

                    double aliasInterval = 1.0 / antiAliasing;
                    for (int aliasx = 0; aliasx < antiAliasing; ++aliasx) {
                        for (int aliasy = 0; aliasy < antiAliasing; ++aliasy) {
                            double x = i - 0.5 + aliasInterval / 2 + aliasInterval * aliasx;
                            double y = j - 0.5 + aliasInterval / 2 + aliasInterval * aliasy;
                            Complex c = toComplex(x, y, pixelCenter, scale, center);
                            Complex z = new Complex(c.getReal(), c.getImaginary());
                            k += 1.0;
                            for (int aliask = 1; aliask < currentIterations; ++aliask, k += 1.0) {
                                if (id != currentDrawingId)
                                    return;
                                z = z.multiply(z).add(c);
                                if (z.abs() > 2)
                                    break;
                            }
                        }
                    }

                    k /= antiAliasing * antiAliasing;
                    if (Math.ceil(k) == currentIterations) {
                        drawingLock.lock();
                        Graphics g = drawing.getGraphics();
                        Color color = mixColors(foregroundColor, backgroundColor, k + 1 - currentIterations);

                        g.setColor(color);
                        g.fillRect(i, j, 1, 1);
                        drawingLock.unlock();
                    } else {
                        drawingLock.lock();
                        Graphics g = drawing.getGraphics();
                        Color color = mixColors(backgroundColor, foregroundColor,
                                (double) k / currentIterations);
                        g.setColor(color);
                        g.fillRect(i, j, 1, 1);
                        drawingLock.unlock();
                    }
                    nDrawnLock.lock();
                    ++nDrawn;
                    nDrawnLock.unlock();
                }
            }
            long interval = System.currentTimeMillis() - startTime;
            System.out.println("Shader: [END after " + interval + " ms]");
            saveImage(drawing, drawingLock);
        }
    };
    return shaderThread;
}