import java.awt.*;
import javax.swing.*;
import java.awt.geom.*;
public class Grafik extends JApplet {
public static void main(String s[]){
JFrame frame = new JFrame();
frame.setTitle("Dasar-Dasar Grafik 2D");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new Grafik();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
frame.setLocation(300,80);
}
public void init(){
JPanel panel =new Panel2D();
getContentPane().add(panel);
}
}
class Panel2D extends JPanel{
public Panel2D(){
setPreferredSize(new Dimension(600,600));
setBackground(Color.white);
}
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
GeneralPath bingkai = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
bingkai.moveTo(300,50);
bingkai.lineTo(550,300);
bingkai.lineTo(300,550);
bingkai.lineTo(50,300);
bingkai.lineTo(300,50);
Stroke stroke = new BasicStroke(30, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND);
g2.setStroke(stroke);
GradientPaint warnaGradien = new GradientPaint(125,100,Color.cyan,150,100,Color.yellow,true);
g2.setPaint(warnaGradien);
g2.fill(bingkai);
g2.draw(bingkai);
Ellipse2D.Double bunder = new Ellipse2D.Double(150,150,300,300);
g2.setColor(Color.YELLOW);
g2.fill(bunder);
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(3.0f));
g2.draw(bunder);
Rectangle2D.Double matakiri = new Rectangle2D.Double(210,240,70,35);
g2.setColor(Color.RED);
g2.fill(matakiri);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(3.0f));
g2.draw(matakiri);
Rectangle2D.Double matakanan = new Rectangle2D.Double(320,240,70,35);
g2.setColor(Color.RED);
g2.fill(matakanan);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(3.0f));
g2.draw(matakanan);
g2.drawLine(390,245,430,230);
g2.drawLine(170,230,210,245);
g2.draw(new Arc2D.Double(280,245,40,20,0,180, Arc2D.OPEN));
float[] dashPattern;
float dashPhase = 0.0f;
dashPattern = new float[2];
dashPattern[0] = 6.0f;
dashPattern[1] = 3.0f;
Rectangle2D.Double matakiri2 = new Rectangle2D.Double(220,250,50,15);
g2.setColor(Color.GREEN);
g2.fill(matakiri2);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,2.0f, dashPattern, dashPhase));
g2.draw(matakiri2);
Rectangle2D.Double matakanan2 = new Rectangle2D.Double(330,250,50,15);
g2.setColor(Color.GREEN);
g2.fill(matakanan2);
g2.setColor(Color.blue);
g2.setStroke(new BasicStroke(3.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL,2.0f, dashPattern, dashPhase));
g2.draw(matakanan2);
Ellipse2D.Double hidung = new Ellipse2D.Double(280,320,40,20);
g2.setColor(Color.BLACK);
g2.fill(hidung);
g2.setColor(Color.BLUE);
g2.setStroke(new BasicStroke(3.0f));
g2.draw(hidung);
GeneralPath mulut = new GeneralPath(GeneralPath.WIND_EVEN_ODD);
mulut.moveTo(250,370);
mulut.lineTo(300,390);
mulut.lineTo(350,370);
Stroke stroke2 = new BasicStroke(3, BasicStroke.CAP_ROUND, BasicStroke.JOIN_MITER);
g2.setStroke(stroke2);
g2.draw(mulut);
g2.drawString("By (G) Mazter_Ghozy", 485,590);
}
}
Hasilnya seperti ini..