Example usage for java.awt Graphics2D draw

List of usage examples for java.awt Graphics2D draw

Introduction

In this page you can find the example usage for java.awt Graphics2D draw.

Prototype

public abstract void draw(Shape s);

Source Link

Document

Strokes the outline of a Shape using the settings of the current Graphics2D context.

Usage

From source file:Main.java

static public void main(String args[]) throws Exception {
    int width = 200, height = 200;
    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);

    Graphics2D ig2 = bi.createGraphics();
    ig2.fillRect(0, 0, width - 1, height - 1);

    BasicStroke stroke = new BasicStroke(10, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
    ig2.setPaint(Color.lightGray);
    ig2.setStroke(stroke);//from w ww. ja  v  a  2  s .  c o  m
    ig2.draw(new Ellipse2D.Double(0, 0, 100, 100));

    ImageIO.write(bi, "GIF", new File("a.gif"));
}

From source file:org.jfree.graphics2d.demo.BufferedImageDemo.java

/**
 * Starting point for the demo.//from   w ww .j  a v a 2s  .  c om
 * 
 * @param args  ignored.
 * 
 * @throws IOException 
 */
public static void main(String[] args) throws IOException {
    BufferedImage image = new BufferedImage(600, 400, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g2 = image.createGraphics();
    ImageIcon icon = new ImageIcon(BufferedImageDemo.class.getResource("jfree_chart_1.jpg"));
    g2.rotate(Math.PI / 12);
    g2.setStroke(new BasicStroke(2.0f));
    g2.setPaint(Color.WHITE);
    g2.fill(new Rectangle(0, 0, 600, 400));
    g2.setPaint(Color.RED);
    g2.draw(new Rectangle(0, 0, 600, 400));
    g2.drawImage(icon.getImage(), 10, 20, null);
    ImageIO.write(image, "png", new File("image-test.png"));
}

From source file:com.oculusinfo.ml.spark.unsupervised.TestDPMeans.java

/**
 * @param args//from  w ww  .j  a va2 s. co  m
 */
public static void main(String[] args) {
    int k = 5;

    try {
        FileUtils.deleteDirectory(new File("output/clusters"));
        FileUtils.deleteDirectory(new File("output/centroids"));
    } catch (IOException e1) {
        /* ignore (*/ }

    genTestData(k);

    JavaSparkContext sc = new JavaSparkContext("local", "OculusML");
    SparkDataSet ds = new SparkDataSet(sc);
    ds.load("test.txt", new InstanceParser());

    DPMeansClusterer clusterer = new DPMeansClusterer(80, 10, 0.001);
    clusterer.setOutputPaths("output/centroids", "output/clusters");

    clusterer.registerFeatureType("point", MeanNumericVectorCentroid.class, new EuclideanDistance(1.0));

    clusterer.doCluster(ds);

    try {
        final List<double[]> instances = readInstances();

        final Color[] colors = { Color.red, Color.blue, Color.green, Color.magenta, Color.yellow, Color.black,
                Color.orange, Color.cyan, Color.darkGray, Color.white };

        TestDPMeans t = new TestDPMeans();
        t.add(new JComponent() {
            private static final long serialVersionUID = 7920802321066846416L;

            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                for (double[] inst : instances) {
                    int color = (int) inst[0];
                    g.setColor(colors[color]);

                    Ellipse2D l = new Ellipse2D.Double(inst[1], inst[2], 5, 5);
                    g2.draw(l);
                }
            }
        });

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(400, 400);
        t.setVisible(true);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:com.oculusinfo.ml.spark.unsupervised.TestThresholdClusterer.java

/**
 * @param args//  w w  w  .  j  a v a2 s.c o  m
 */
public static void main(String[] args) {
    int k = 5;

    try {
        FileUtils.deleteDirectory(new File("output/clusters"));
        FileUtils.deleteDirectory(new File("output/centroids"));
    } catch (IOException e1) {
        /* ignore (*/ }

    genTestData(k);

    JavaSparkContext sc = new JavaSparkContext("local", "OculusML");
    SparkDataSet ds = new SparkDataSet(sc);
    ds.load("test.txt", new InstanceParser());

    ThresholdClusterer clusterer = new ThresholdClusterer(80);
    clusterer.setOutputPaths("output/centroids", "output/clusters");

    clusterer.registerFeatureType("point", MeanNumericVectorCentroid.class, new EuclideanDistance(1.0));

    clusterer.doCluster(ds);

    try {
        final List<double[]> instances = readInstances();

        final Color[] colors = { Color.red, Color.blue, Color.green, Color.magenta, Color.yellow, Color.black,
                Color.orange, Color.cyan, Color.darkGray, Color.white };

        TestThresholdClusterer t = new TestThresholdClusterer();
        t.add(new JComponent() {
            private static final long serialVersionUID = -5597119848880912541L;

            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                for (double[] inst : instances) {
                    int color = (int) inst[0];
                    g.setColor(colors[color]);

                    Ellipse2D l = new Ellipse2D.Double(inst[1], inst[2], 5, 5);
                    g2.draw(l);
                }
            }
        });

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(400, 400);
        t.setVisible(true);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    URL url = new URL("http://www.java2s.com/style/download.png");

    final BufferedImage originalImage = ImageIO.read(url);
    int width = originalImage.getWidth();
    int height = originalImage.getHeight();
    final BufferedImage textImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    Graphics2D g = textImage.createGraphics();

    FontRenderContext frc = g.getFontRenderContext();
    Font font = new Font("Arial", Font.BOLD, 50);
    GlyphVector gv = font.createGlyphVector(frc, "java2s.com");

    int xOff = 0;
    int yOff = 50;

    Shape shape = gv.getOutline(xOff, yOff);
    g.setClip(shape);/*from   ww w.j  a va2s.  com*/
    g.drawImage(originalImage, 0, 0, null);

    g.setStroke(new BasicStroke(2f));
    g.setColor(Color.BLACK);
    g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    g.draw(shape);
    g.dispose();

    ImageIO.write(textImage, "png", new File("cat-text.png"));

    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(textImage)));
        }
    });
}

From source file:com.oculusinfo.ml.spark.unsupervised.TestKMeans.java

/**
 * @param args/* w  ww.j  a  v a2 s . c  o m*/
 */
public static void main(String[] args) {
    int k = 5;

    try {
        FileUtils.deleteDirectory(new File("output/clusters"));
        FileUtils.deleteDirectory(new File("output/centroids"));
    } catch (IOException e1) {
        /* ignore (*/ }

    genTestData(k);

    JavaSparkContext sc = new JavaSparkContext("local", "OculusML");
    SparkDataSet ds = new SparkDataSet(sc);
    ds.load("test.txt", new SparkInstanceParser() {
        private static final long serialVersionUID = 1L;

        @Override
        public Tuple2<String, Instance> call(String line) throws Exception {
            Instance inst = new Instance();

            String tokens[] = line.split(",");

            NumericVectorFeature v = new NumericVectorFeature("point");

            double x = Double.parseDouble(tokens[0]);
            double y = Double.parseDouble(tokens[1]);
            v.setValue(new double[] { x, y });

            inst.addFeature(v);

            return new Tuple2<String, Instance>(inst.getId(), inst);
        }
    });

    KMeansClusterer clusterer = new KMeansClusterer(k, 10, 0.001, "output/centroids", "output/clusters");

    clusterer.registerFeatureType("point", MeanNumericVectorCentroid.class, new EuclideanDistance(1.0));

    clusterer.doCluster(ds);

    try {
        final List<double[]> instances = readInstances();

        final Color[] colors = { Color.red, Color.blue, Color.green, Color.magenta, Color.yellow, Color.black,
                Color.orange, Color.cyan, Color.darkGray, Color.white };

        TestKMeans t = new TestKMeans();
        t.add(new JComponent() {
            private static final long serialVersionUID = 2059497051387104848L;

            public void paintComponent(Graphics g) {
                Graphics2D g2 = (Graphics2D) g;
                g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

                for (double[] inst : instances) {
                    int color = (int) inst[0];
                    g.setColor(colors[color]);

                    Ellipse2D l = new Ellipse2D.Double(inst[1], inst[2], 5, 5);
                    g2.draw(l);
                }
            }
        });

        t.setDefaultCloseOperation(EXIT_ON_CLOSE);
        t.setSize(400, 400);
        t.setVisible(true);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

From source file:akori.AKORI.java

public static void main(String[] args) throws IOException, InterruptedException {
    System.out.println("esto es AKORI");

    URL = "http://www.mbauchile.cl";
    PATH = "E:\\NetBeansProjects\\AKORI\\";
    NAME = "mbauchile.png";
    // Extrar DOM tree

    Document doc = Jsoup.connect(URL).timeout(0).get();

    // The Firefox driver supports javascript 
    WebDriver driver = new FirefoxDriver();
    driver.manage().window().maximize();
    System.out.println(driver.manage().window().getSize().toString());
    System.out.println(driver.manage().window().getPosition().toString());
    int xmax = driver.manage().window().getSize().width;
    int ymax = driver.manage().window().getSize().height;

    // Go to the URL page
    driver.get(URL);/*from  www.  j  a v  a2  s. c  o  m*/

    File screen = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
    FileUtils.copyFile(screen, new File(PATH + NAME));

    BufferedImage img = ImageIO.read(new File(PATH + NAME));
    //Graphics2D graph = img.createGraphics();

    BufferedImage img1 = new BufferedImage(xmax, ymax, BufferedImage.TYPE_INT_ARGB);
    Graphics2D graph1 = img.createGraphics();
    double[][] matrix = new double[ymax][xmax];
    BufferedReader in = new BufferedReader(new FileReader("et.txt"));
    String linea;
    double max = 0;
    graph1.drawImage(img, 0, 0, null);
    HashMap<String, Integer> lista = new HashMap<String, Integer>();
    int count = 0;
    for (int i = 0; (linea = in.readLine()) != null && i < 10000; ++i) {
        String[] datos = linea.split(",");
        int x = (int) Double.parseDouble(datos[0]);
        int y = (int) Double.parseDouble(datos[2]);
        long time = Double.valueOf(datos[4]).longValue();
        if (x >= xmax || y >= ymax)
            continue;
        if (time < 691215)
            continue;
        if (time > 705648)
            break;
        if (lista.containsKey(x + "," + y))
            lista.put(x + "," + y, lista.get(x + "," + y) + 1);
        else
            lista.put(x + "," + y, 1);
        ++count;
    }
    System.out.println(count);
    in.close();
    Iterator iter = lista.entrySet().iterator();
    Map.Entry e;
    for (String key : lista.keySet()) {
        Integer i = lista.get(key);
        if (max < i)
            max = i;
    }
    System.out.println(max);
    max = 0;
    while (iter.hasNext()) {
        e = (Map.Entry) iter.next();
        String xy = (String) e.getKey();
        String[] datos = xy.split(",");
        int x = Integer.parseInt(datos[0]);
        int y = Integer.parseInt(datos[1]);
        matrix[y][x] += (int) e.getValue();
        double aux;
        if ((aux = normalMatrix(matrix, y, x, ((int) e.getValue()) * 4)) > max) {
            max = aux;
        }
        //normalMatrix(matrix,x,y,20);
        if (matrix[y][x] > max)
            max = matrix[y][x];
    }
    int A, R, G, B, n;
    for (int i = 0; i < xmax; ++i) {
        for (int j = 0; j < ymax; ++j) {
            if (matrix[j][i] != 0) {
                n = (int) Math.round(matrix[j][i] * 100 / max);
                R = Math.round((255 * n) / 100);
                G = Math.round((255 * (100 - n)) / 100);
                B = 0;
                A = Math.round((255 * n) / 100);
                ;
                if (R > 255)
                    R = 255;
                if (R < 0)
                    R = 0;
                if (G > 255)
                    G = 255;
                if (G < 0)
                    G = 0;
                if (R < 50)
                    A = 0;
                graph1.setColor(new Color(R, G, B, A));
                graph1.fillOval(i, j, 1, 1);
            }
        }
    }
    //graph1.dispose();

    ImageIO.write(img, "png", new File("example.png"));
    System.out.println(max);

    graph1.setColor(Color.RED);
    // Extraer elementos
    Elements e1 = doc.body().getAllElements();
    int i = 1;
    ArrayList<String> tags = new ArrayList<String>();
    for (Element temp : e1) {

        if (tags.indexOf(temp.tagName()) == -1) {
            tags.add(temp.tagName());

            List<WebElement> query = driver.findElements(By.tagName(temp.tagName()));
            for (WebElement temp1 : query) {
                Point po = temp1.getLocation();
                Dimension d = temp1.getSize();
                if (d.width <= 0 || d.height <= 0 || po.x < 0 || po.y < 0)
                    continue;
                System.out.println(i + " " + temp.nodeName());
                System.out.println("  x: " + po.x + " y: " + po.y);
                System.out.println("  width: " + d.width + " height: " + d.height);
                graph1.draw(new Rectangle(po.x, po.y, d.width, d.height));
                ++i;
            }
        }
    }

    graph1.dispose();
    ImageIO.write(img, "png", new File(PATH + NAME));

    driver.quit();

}

From source file:Main.java

public static BufferedImage getStrokedImage(BufferedImage bi, Shape shape, int strokeWidth) {
    int w = bi.getWidth();
    int h = bi.getHeight();
    BufferedImage bib = new BufferedImage(w, h, bi.getType());
    Graphics2D g = bib.createGraphics();

    BasicStroke bs = new BasicStroke(strokeWidth);
    g.setStroke(bs);/*from  www  .ja  v a2  s .c  om*/
    Rectangle rect = new Rectangle(0, 0, w, h);
    TexturePaint tp = new TexturePaint(bi, rect);
    g.setPaint(tp);
    g.draw(shape);

    g.dispose();
    return bib;
}

From source file:Main.java

/**
 * Draws a shape with the specified rotation about <code>(x, y)</code>.
 *
 * @param g2  the graphics device (<code>null</code> not permitted).
 * @param shape  the shape (<code>null</code> not permitted).
 * @param angle  the angle (in radians).
 * @param x  the x coordinate for the rotation point.
 * @param y  the y coordinate for the rotation point.
 *///from w w  w  .java  2  s.  c om
public static void drawRotatedShape(final Graphics2D g2, final Shape shape, final double angle, final float x,
        final float y) {

    final AffineTransform saved = g2.getTransform();
    final AffineTransform rotate = AffineTransform.getRotateInstance(angle, x, y);
    g2.transform(rotate);
    g2.draw(shape);
    g2.setTransform(saved);

}

From source file:PaintUtils.java

/** Paints 3 different strokes around a shape to indicate focus.
 * The widest stroke is the most transparent, so this achieves a nice
 * "glow" effect.//from   w  w  w .  ja v  a2  s.c  o m
 * <P>The catch is that you have to render this underneath the shape,
 * and the shape should be filled completely.
 * 
 * @param g the graphics to paint to
 * @param shape the shape to outline
 * @param biggestStroke the widest stroke to use.
 */
public static void paintFocus(Graphics2D g, Shape shape, int biggestStroke) {
    Color focusColor = getFocusRingColor();
    Color[] focusArray = new Color[] {
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 255),
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 170),
            new Color(focusColor.getRed(), focusColor.getGreen(), focusColor.getBlue(), 110) };
    g.setStroke(new BasicStroke(biggestStroke));
    g.setColor(focusArray[2]);
    g.draw(shape);
    g.setStroke(new BasicStroke(biggestStroke - 1));
    g.setColor(focusArray[1]);
    g.draw(shape);
    g.setStroke(new BasicStroke(biggestStroke - 2));
    g.setColor(focusArray[0]);
    g.draw(shape);
    g.setStroke(new BasicStroke(1));
}