Java AWT RoundRectangle2D class

Description

Java AWT RoundRectangle2D class

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.TexturePaint;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class Main extends JPanel {
  @Override/*  ww  w. j  a va 2s  . c o  m*/
  public void paintComponent(Graphics g) {
    super.paintComponent(g);
    Graphics2D g2d = (Graphics2D) g; // cast g to Graphics2D

    // draw 2D rounded rectangle with a buffered background
    BufferedImage buffImage = new BufferedImage(10, 10, BufferedImage.TYPE_INT_RGB);

    // obtain Graphics2D from buffImage and draw on it
    Graphics2D gg = buffImage.createGraphics();
    gg.setColor(Color.YELLOW);
    gg.fillRect(0, 0, 10, 10);
    gg.setColor(Color.BLACK);
    gg.drawRect(1, 1, 6, 6);

    // paint buffImage onto the JFrame
    g2d.setPaint(new TexturePaint(buffImage, new Rectangle(10, 10)));
    g2d.fill(new RoundRectangle2D.Double(155, 30, 75, 100, 50, 50));
  }
  public static void main(String[] args) {
    JFrame frame = new JFrame("Drawing 2D shapes");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Main Main = new Main();
    frame.add(Main);
    frame.setSize(425, 200);
    frame.setVisible(true);
  }
}



PreviousNext

Related