1. Yang pertama perlu kita lakukan adalah
kita buat file.java
2. Lalu kita isikan dengan scriptnya, sebagai
berikut :
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package javaapplication15;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JApplet;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Riza
*/
public class javaapplication15 extends JApplet {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
JFrame frame = new JFrame();
frame.setTitle("judul");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new javaapplication15();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}
public void init() {
JPanel panel = new Panel2D();
getContentPane().add(panel);
}
}
class Panel2D extends JPanel {
public Panel2D() {
setPreferredSize(new Dimension(900, 850));
setBackground(Color.BLACK);
}
public void paintComponent(Graphics g) {
Dimension d = getSize();
int maxX = d.width - 1;
int maxY = d.height - 1;
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
// tulis coding awal disini
g2.setColor(Color.GRAY);
int xPoli[] = {0,300,900,900};
int yPoli[] = {690,400,400,690};
g2.fillPolygon(xPoli, yPoli, xPoli.length);
g2.setColor(Color.lightGray);
int xPole[] = {300,900,900,300};
int yPole[] = {390,390,0,0};
g2.fillPolygon(xPole, yPole, xPole.length);
g2.setColor(Color.blue);
int xPola[] = {0,300,300,0};
int yPola[] = {690,400,0,0};
g2.fillPolygon(xPola, yPola, xPola.length);
g2.setColor(Color.YELLOW);
Font font = new Font ("",Font.ROMAN_BASELINE,20);
g2.setFont(font);
g2.drawString("kebersihan sebagian dari iman",450 ,80);
g2.setColor(Color.ORANGE);
int xPolu[] = {310,480,480,310};
int yPolu[] = {180,180,90,90};
g2.fillPolygon(xPolu, yPolu, xPolu.length);
g2.setColor(Color.white);
int xPelu[] = {500,600,500,600};
int yPelu[] = {180,180,90,90};
g2.fillPolygon(xPelu, yPelu, xPelu.length);
g2.setColor(Color.ORANGE);
int xPoles[] = {650,850,850,650};
int yPoles[] = {180,180,90,90};
g2.fillPolygon(xPoles, yPoles, xPoles.length);
g2.setColor(Color.GREEN);
g2.setStroke(new BasicStroke (10.0f));
g2.drawLine(290, 490, 295, 390);
g2.setColor(Color.red);
g2.fillOval(180, 185, 130, 130);
g2.fillOval(280, 185, 130, 130);
g2.fillOval(280, 280, 130, 130);
g2.fillOval(180, 280, 130, 130);
g2.setColor(Color.white);
g2.fillRect(280, 280, 40, 40);
g2.setColor(Color.black);
g2.fillOval(280, 280, 40, 40);
g2.setColor(Color.yellow);
g2.fillOval(210, 220, 90, 90);
g2.fillOval(290, 220, 90, 90);
g2.fillOval(210, 290, 90, 90);
g2.fillOval(290, 290, 90, 90);
g2.setColor(Color.black);
int xPoly[] = {210,380,350,240};
int yPoly[] = {480,480,580,580};
g2.fillPolygon(xPoly, yPoly, xPoly.length);
}
}
3. Run project F6
Semoga tutorial yang kami bagi ini dapat membantu sobat sekalian.
SELAMAT BELAJAR
0 Response to "Tutorial Grafika Komputer Menggunakan JAVA 2D X"
Post a Comment