Java AWT CubicCurve2D create

Description

Java AWT CubicCurve2D create

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.CubicCurve2D;
import java.util.Random;

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

public class Main extends JPanel {
  Random rnd = new Random();

  @Override// w  w w  .ja v a 2s. c  om
  public void paintComponent(Graphics g) {
    super.paintComponent(g);

    Graphics2D g2d = (Graphics2D) g;
    g2d.setBackground(Color.WHITE);
    g2d.clearRect(0, 0, getParent().getWidth(), getParent().getHeight());
    // antialising
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    //CubicCurve2D
    CubicCurve2D cubicCurve = new CubicCurve2D.Float(
            50, 75,            // start pt (x1,y1)
            50+30, 75-100,     // control pt1
            50+60, 75+100,     // control pt2 
            50+90, 75          // end pt (x2,y2)
    );
    
    g2d.draw(cubicCurve); 
  }

  public static void main(String[] args) {
    // create frame for Main
    JFrame frame = new JFrame("java2s.com");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Main Main = new Main();
    frame.add(Main);
    frame.setSize(300, 210);
    frame.setVisible(true);
  }
}



PreviousNext

Related