Tugas 6 : Membuat Lukisan kapal 2D java


Di dalam bahasa pemrograman java kita bisa membuat grafik baik 2d maupun yang 3d.,
contohnya gambar di bawah ini :

untuk Source codenya disimpan dalam file  lukisan_kapal.java :


package tugas6;

import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
/**
* @author Haris
*/
public class lukisan_kapal extends JApplet {

public static void main(String s[]) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JApplet applet = new lukisan_kapal();
applet.init();
frame.getContentPane().add(applet);
frame.pack();
frame.setVisible(true);
}

public void init() {
JPanel panel = new Pan();
getContentPane().add(panel);
}

class Pan extends JPanel {

public Pan() {
setPreferredSize(new Dimension(1300, 600));
}

public void paintComponent(Graphics g) {
super.paintComponent(g);

Graphics2D g1 = (Graphics2D) g;
Graphics2D g2 = (Graphics2D) g;
// nama dan nim
kotak(g2);
GradientPaint paint1 = new GradientPaint(100, 100, new Color(130, 36, 2), 250, 100, new Color(253, 149, 51));
Font font = new Font("Comic Sans ms", Font.BOLD, 25);
g1.setPaint(paint1);
g1.setFont(font);
g1.drawString("Haris Budi Erwanto", 50, 500);
g1.drawString("( 10-65-0006 )", 65, 550);

garisdda(g2, 0, 350, 325, 350);
garisdda(g2, 625, 350, 1300, 350);

garisdda(g2, 350, 400, 600, 400);
garisdda(g2, 600, 400, 650, 300);
garisdda(g2, 300, 300, 350, 400);
garisdda(g2, 300, 300, 650, 300);

kotak_brass(g2, 350, 250, 600, 300);
bunder(g2, 1150, 100, 75);
body(g2);
garisdda(g2, 950, 100, 1050, 100);
garisdda(g2, 960, 25, 1060, 50);
garisdda(g2, 960, 175, 1060, 150);
garisdda(g2, 980, 250, 1080, 175);
budermidpoint(g2, 375, 275, 20);
budermidpoint(g2, 430, 275, 20);
budermidpoint(g2, 485, 275, 20);
budermidpoint(g2, 540, 275, 20);
awane(g2, -10, 50);
awane(g2, 90, 1);
awane(g2, 210, 50);
awane(g2, 310, 1);
awane(g2, 410, 50);
awane(g2, 510, 1);
awane(g2, 610, 50);
awane(g2, 710, 1);
}

public void body(Graphics2D g) {
GeneralPath gp = new GeneralPath();
GradientPaint p = new GradientPaint(0, 350, Color.gray, 50, 30, Color.black);
g.setPaint(p);

gp.moveTo(300, 300);
gp.lineTo(650, 300);
gp.lineTo(600, 400);
gp.lineTo(350, 400);
gp.closePath();
g.fill(gp);
}

public void kotak_brass(Graphics2D gr, int x_awal, int y_awal, int x_akhir, int y_akhir) {
GeneralPath gp = new GeneralPath();
Shape s = new Rectangle2D.Double(x_awal, y_awal, 250, 50);
GradientPaint p = new GradientPaint(0, 350, Color.green, 50, 30, Color.black);
gr.setPaint(p);
gr.fill(s);
int Dx = Math.abs(x_akhir - x_awal);
int Dy = Math.abs(y_akhir - y_awal);
int parameter = (2 * Dy) - Dx;
String status = "Kotak_BM";
this.repaint();
double g = x_awal, h = y_awal;
while (x_awal < x_akhir) {
if (parameter < 0) {
parameter += (2 * Dy);
} else {
y_awal++;
parameter = parameter + (2 * Dy) - (2 * Dx);
}
gp.moveTo(x_awal, y_awal);
x_awal++;
}
gp.lineTo(g, y_awal);
gp.lineTo(g, h);
gp.lineTo(x_awal, h);
gp.lineTo(x_awal, y_awal);
gp.closePath();
gr.draw(gp);}

public void garisdda(Graphics2D g, int x1, int y1, int x2, int y2) {

double m = (double) (y2 - y1) / (x2 - x1);
double y = (double) y1;
int iy;
for (int x = x1; x <= x2; x++) {
iy = (int) Math.round(y);
g.drawLine(x, iy, x, iy);
y += m;
}
}
}

public void kotak(Graphics2D g) {
Shape s = new Rectangle2D.Double(0, 0, 1300, 350);
GradientPaint p = new GradientPaint(0, 350, new Color(0, 255, 255), 50, 30, new Color(70, 51, 204));
g.setPaint(p);
g.fill(s);
Shape s1 = new Rectangle2D.Double(0, 350, 1300, 250);
GradientPaint r = new GradientPaint(0, 350, Color.white, 1300, 600, Color.blue);
g.setPaint(r);
g.fill(s1);
}

void budermidpoint(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
g.setColor(Color.red);
g.fill(s);
int x = 0, y = r, p = 1 - r;
int xctr = x1, yctr = y1;
g.drawLine(xctr + x, yctr + y, xctr + x, yctr + y);
g.drawLine(xctr - x, yctr + y, xctr - x, yctr + y);
g.drawLine(xctr + x, yctr - y, xctr + x, yctr - y);
g.drawLine(xctr - x, yctr - y, xctr - x, yctr - y);
g.drawLine(xctr + y, yctr + x, xctr + y, yctr + x);
g.drawLine(xctr - y, yctr + x, xctr - y, yctr + x);
g.drawLine(xctr + y, yctr - x, xctr + y, yctr - x);
g.drawLine(xctr - y, yctr - x, xctr - y, yctr - x);
while (x < y) {
x++;
if (p < 0) {
p += 2 * x + 1;
} else {
y--;
p += 2 * (x - y) + 1;
}
}
}

public void bunder(Graphics2D g, int x1, int y1, int r) {
Shape s = new Ellipse2D.Double(x1 - r, y1 - r, r * 2, r * 2);
GradientPaint p = new GradientPaint(x1, y1, Color.orange, x1 + 200, y1 + 50, Color.red);
g.setPaint(p);
g.fill(s);
}

public void awane(Graphics2D g2, int x, int y) {
GeneralPath gp = new GeneralPath();
gp.moveTo(30 + x, 80 + y);
gp.curveTo(-5 + x, 60 + y, 22 + x, 15 + y, 40 + x, 42 + y);
gp.curveTo(37 + x, 31 + y, 53 + x, 31 + y, 58 + x, 43 + y);
gp.curveTo(60 + x, 7 + y, 116 + x, 4 + y, 100 + x, 34 + y);
gp.curveTo(150 + x, 29 + y, 150 + x, 89 + y, 120 + x, 90 + y);
gp.curveTo(100 + x, 120 + y, 95 + x, 97 + y, 87 + x, 100 + y);
gp.curveTo(71 + x, 120 + y, 35 + x, 100 + y, 35 + x, 70 + y);
gp.closePath();
gp.closePath();
g2.setPaint(new GradientPaint(0 + x, 20 + y, Color.lightGray, 0 + x, 150 + y, Color.white));
g2.fill(gp);
g2.setColor(Color.white);
g2.draw(gp);
}

}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s