Tutorial Membuat Pemandangan Pada Pemrograman Grafik Java

Hallo semuanya! Saya Agung Jayasukma berasal dari SMK Negeri 1 Cimahi kelas XII RPL – A. Saat ini saya akan memberikan tutorial Membuat Pemandangan Pemgrograman Grafik Java. Pembuatan aplikasi ini bertujuan untuk memberikan inspirasi terhadap programmer maupun user yang ingin mencoba membuat aplikasi pemandangan ini. Oleh karena itu, Saya akan mengambil tema pemandangan supaya terlihat indah ketika user pertama kali melihat aplikasi ini.

Persiapan

Pertama kamu harus mempersiapkan dulu berbagai hal yang diperlukan:

  • Netbeans
  • Library LWJGL
  • Mockup design pemandangan

Jika semua sudah siap, kini saatnya kamu memulai tutorial berikut ini.

Membuat Mockup Design

Ada beberapa hal yang harus diperhatikan ketika membuat mockup, yaitu sumbu X dan sumbu Y. Karena sumbu X dan Y ini mempengaruhi sebuah titik dimana titik tersebut merupakan awal dimana kita akan membuat sebuah objek dari gambar pemandangan. Dan supaya untuk mempermudah menggambar, disarankan untuk memakai CorelDraw karena kita bisa langsung melihat secara langsung titik sumbu X dan Y nya.

Membuat Projek Baru

Bukalah Netbeans kalian dan buatlah Project baru dengan nama terserah Anda. Lalu, jika sudah maka kita akan membuat codingan di dalam class java yang sudah disediakan.

Import beberapa Importer

Masukkanlah import di class javanya

import java.util.logging.Level;
import java.util.logging.Logger;
import org.lwjgl.LWJGLException;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;
import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.util.glu.GLU.gluOrtho2D;

Membuat Layar Aplikasi

Buatlah layar aplikasnya

public int DISPLAY_HEIGHT = 500;
public int DISPLAY_WIDTH = 1150;

Masukkan script objek-objek

Masukkanlah script setiap objek objek seperti pohon, gunung, danau dan lain lain di class javanya

public void render() {
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
glColor3d(0.00, 0.31, 0.45);
background();
glColor3d(0.13, 0.55, 0.70);
danau();
glColor3d(0.53, 0.71, 0.78);
gunung0();
glColor3d(0.60, 0.77, 0.83);
gunung1();
glColor3d(0.69, 0.85, 0.91);
gunung4();
glColor3d(0.58, 0.70, 0.75);
shadowgunung4();
glColor3d(0.47, 0.65, 0.72);
tanahpohon1();
glColor3d(0.69, 0.85, 0.91);
gunung3();
glColor3d(0.58, 0.70, 0.75);
shadowgunung3();
glColor3d(0.58, 0.76, 0.83);
tanahpohon3();
glColor3d(0.69, 0.85, 0.91);
gunung2();
glColor3d(0.58, 0.70, 0.75);
shadowgunung2();
glColor3d(0.51, 0.70, 0.77);
tanahpohon2();
glColor3d(0.04, 0.19, 0.26);
gunungdepan();
glColor3d(0.00, 0.31, 0.45);
pulau();

glFlush();
}

public void background() {

glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(1150, 0);
glVertex2f(1150, 500);
glVertex2f(0, 500);

glEnd();

}

public void danau() {
glBegin(GL_POLYGON);
glVertex2f(0, 0);
glVertex2f(0, 187);
glVertex2f(1150, 187);
glVertex2f(1150, 0);
glEnd();

}

public void pulau() {
glBegin(GL_POLYGON);
glVertex2f(0, 143);
glVertex2f(306, 97);
glVertex2f(0, 93);
glEnd();

}

public void gunung0() {
glBegin(GL_POLYGON);
glVertex2f(114, 192);
glVertex2f(126, 205);
glVertex2f(211, 259);
glVertex2f(280, 192);
glEnd();

}

public void gunung1() {
glBegin(GL_POLYGON);
glVertex2f(188, 192);
glVertex2f(294, 274);
glVertex2f(440, 187);
glEnd();

}

public void tanahpohon1() {
glBegin(GL_POLYGON);
glVertex2f(0, 194);
glVertex2f(223, 210);
glVertex2f(337, 187);
glVertex2f(0, 187);
glEnd();

}

public void tanahpohon2() {
glBegin(GL_POLYGON);
glVertex2f(294, 187);
glVertex2f(377, 205);
glVertex2f(692, 210);
glVertex2f(720, 187);
glEnd();

}

public void tanahpohon3() {
glBegin(GL_POLYGON);
glVertex2f(584, 187);
glVertex2f(784, 227);
glVertex2f(870, 213);
glVertex2f(1078, 204);
glVertex2f(1078, 187);
glEnd();

}

public void gunung2() {
glBegin(GL_POLYGON);
glVertex2f(316, 187);
glVertex2f(344, 218);
glVertex2f(552, 351);
glVertex2f(720, 187);
glEnd();

}

public void shadowgunung2() {
glBegin(GL_POLYGON);
glVertex2f(552, 351);
glVertex2f(648, 187);
glVertex2f(720, 187);
glEnd();

}

public void gunung3() {
glBegin(GL_POLYGON);
glVertex2f(612, 187);
glVertex2f(652, 254);
glVertex2f(786, 333);
glVertex2f(984, 187);
glEnd();

}

public void shadowgunung3() {
glBegin(GL_POLYGON);
glVertex2f(904, 187);
glVertex2f(786, 333);
glVertex2f(984, 187);
glEnd();

}

public void gunung4() {
glBegin(GL_POLYGON);
glVertex2f(904, 187);
glVertex2f(933, 225);
glVertex2f(1098, 301);
glVertex2f(1150, 261);
glVertex2f(1150, 187);
glEnd();

}

public void gunungdepan() {
glBegin(GL_POLYGON);
glVertex2f(542, 0);
glVertex2f(666, 85);
glVertex2f(746, 91);
glVertex2f(858, 147);
glVertex2f(968, 163);
glVertex2f(1032, 205);
glVertex2f(1150, 250);
glVertex2f(1150, 0);
glEnd();

}

public void shadowgunung4() {
glBegin(GL_POLYGON);
glVertex2f(1128, 187);
glVertex2f(1098, 301);
glVertex2f(1150, 261);
glVertex2f(1150, 187);
glEnd();

}

public void tanah1() {
glBegin(GL_POLYGON);
glVertex2f(325, 0);
glVertex2f(0, 181);
glVertex2f(0, 195);
glVertex2f(396, 0);
glEnd();

}

public void tanah2() {
glBegin(GL_POLYGON);
glVertex2f(539, 0);
glVertex2f(0, 195);
glVertex2f(0, 205);
glVertex2f(637, 0);
glEnd();
}

public void rumput3() {
glBegin(GL_POLYGON);
glVertex2f(627, 0);
glVertex2f(0, 205);
glVertex2f(676, 0);
glEnd();

}

public void pasir() {
glBegin(GL_POLYGON);
glVertex2f(1150, 68);
glVertex2f(683, 131);
glVertex2f(1150, 96);
glEnd();
}

public void pasir2() {
glBegin(GL_POLYGON);
glVertex2f(1150, 123);
glVertex2f(950, 149);
glVertex2f(1150, 134);
glEnd();
}

public void rumput4() {
glBegin(GL_POLYGON);
glVertex2f(973, 0);
glVertex2f(0, 228);
glVertex2f(1150, 68);
glVertex2f(1150, 0);
glEnd();

}

public void rumput5() {
glBegin(GL_POLYGON);
glVertex2f(1150, 123);
glVertex2f(950, 149);
glVertex2f(1150, 207);
glEnd();
}

public void bayanganrumput1() {
glBegin(GL_POLYGON);
glVertex2f(1004, 165);
glVertex2f(1108, 195);
glVertex2f(1084, 176);
glEnd();
}

public void bayanganair1() {
glBegin(GL_POLYGON);
glColor3d(0.33, 0.55, 0.57);
glVertex2f(1150, 192);
glVertex2f(0, 226);
glVertex2f(1150, 207);
glEnd();
}

public void bayanganair2() {
//ini bayangan yg pohon
glBegin(GL_POLYGON);
glColor3d(0.33, 0.55, 0.57);
glVertex2f(625, 212);
glVertex2f(637, 173);
glVertex2f(648, 212);
glEnd();
}

public void bayanganair3() {
//ini bayangan yg rumah
glBegin(GL_POLYGON);
glColor3d(0.33, 0.55, 0.57);
glVertex2f(527, 213);
glVertex2f(541, 195);
glVertex2f(551, 213);
glEnd();

//ini bayangan yg rumah
glBegin(GL_POLYGON);
glColor3d(1.00, 1.00, 1.00);
glVertex2f(541, 195);
glVertex2f(552, 213);
glVertex2f(611, 213);
glVertex2f(602, 195);
glEnd();
}

public void bayanganair4() {
//ini bayangan yg pohon
glBegin(GL_POLYGON);
glColor3d(0.33, 0.55, 0.57);
glVertex2f(500, 212);
glVertex2f(515, 166);
glVertex2f(521, 189);
glVertex2f(518, 199);
glVertex2f(524, 200);
glVertex2f(529, 212);
glEnd();
}

public void rumput6() {
glBegin(GL_POLYGON);
glVertex2f(1150, 222);
glVertex2f(0, 227);
glVertex2f(327, 252);
glVertex2f(443, 236);
glVertex2f(516, 260);
glVertex2f(765, 237);
glVertex2f(1150, 233);
glEnd();
}

public void pasir3() {
glBegin(GL_POLYGON);
glVertex2f(1150, 192);
glVertex2f(0, 227);
glVertex2f(1150, 222);
glEnd();
}

public void pasirjalan1() {
glBegin(GL_POLYGON);
glVertex2f(589, 222);
glVertex2f(575, 241);
glVertex2f(584, 249);
glVertex2f(591, 249);
glVertex2f(588, 243);
glVertex2f(625, 222);
glEnd();
}

public void rumah1() {
//ini bagian tembok kiri
glBegin(GL_POLYGON);
glColor3d(0.73, 0.25, 0.20);
glVertex2f(523, 250);
glVertex2f(523, 275);
glVertex2f(557, 275);
glVertex2f(557, 250);
glEnd();
}

public void rumah2() {
//ini bagian tembok atap
glBegin(GL_POLYGON);
glColor3d(0.73, 0.25, 0.20);
glVertex2f(523, 275);
glVertex2f(541, 298);
glVertex2f(554, 275);
glEnd();
}

public void rumah3() {
//ini bagian tembok depan
glBegin(GL_POLYGON);
glColor3d(0.73, 0.25, 0.20);
glVertex2f(554, 250);
glVertex2f(544, 275);
glVertex2f(613, 275);
glVertex2f(613, 250);
glEnd();
}

public void rumah4() {
//ini bagian tembok depan2
glBegin(GL_POLYGON);
glColor3d(0.92, 0.29, 0.22);
glVertex2f(557, 250);
glVertex2f(557, 268);
glVertex2f(611, 268);
glVertex2f(611, 250);
glEnd();
}

public void jendela() {
//jendela 1
glBegin(GL_POLYGON);
glColor3d(0.73, 0.25, 0.20);
glVertex2f(568, 256);
glVertex2f(568, 268);
glVertex2f(577, 268);
glVertex2f(577, 256);
glEnd();

//jendela 2
glBegin(GL_POLYGON);
glColor3d(0.73, 0.25, 0.20);
glVertex2f(588, 256);
glVertex2f(588, 268);
glVertex2f(597, 268);
glVertex2f(597, 256);
glEnd();
}

public void atap() {
//atap yang putih
glBegin(GL_POLYGON);
glColor3d(0.73, 0.74, 0.75);
glVertex2f(521, 274);
glVertex2f(541, 298);
glVertex2f(542, 297);
glVertex2f(524, 274);
glEnd();

//atap yang abu
glBegin(GL_POLYGON);
glColor3d(1.00, 1.00, 1.00);
glVertex2f(557, 270);
glVertex2f(541, 298);
glVertex2f(600, 298);
glVertex2f(615, 270);
glEnd();
}

public void bayanganrumah() {
glBegin(GL_POLYGON);
glColor3d(0.21, 0.30, 0.19);
glVertex2f(478, 247);
glVertex2f(517, 260);
glVertex2f(524, 260);
glVertex2f(587, 249);
glEnd();
}

public void pohon7() {
//Ini Pohon yang dibelakang rumah

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(508, 254);
glVertex2f(513, 272);
glVertex2f(520, 254);
glEnd();

glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(514, 262);
glVertex2f(488, 286);
glVertex2f(493, 301);
glVertex2f(500, 302);
glVertex2f(496, 312);
glVertex2f(514, 370);
glVertex2f(521, 350);
glVertex2f(518, 339);
glVertex2f(524, 341);
glVertex2f(540, 286);
glEnd();
}

public void pohon8() {

//pohon di halaman rumah kanan 1
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(622, 249);
glVertex2f(634, 299);
glVertex2f(622, 249);
glEnd();
}

public void pohon9() {

//pohon di halaman rumah kanan 2
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(668, 232);
glVertex2f(684, 275);
glVertex2f(695, 232);
glEnd();
}

public void pohon10() {

//pohon di halaman rumah kanan 3
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(781, 229);
glVertex2f(794, 262);
glVertex2f(803, 229);
glEnd();
}

public void pohon11() {

//pohon di halaman rumah kanan 4
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(810, 223);
glVertex2f(826, 275);
glVertex2f(838, 223);
glEnd();
}

public void pohon12() {

//pohon di halaman rumah kiri 1
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(370, 232);
glVertex2f(386, 282);
glVertex2f(400, 232);
glEnd();
}

public void pohon13() {

//pohon di halaman rumah kiri 2
glBegin(GL_POLYGON);
glColor3d(0.24, 0.32, 0.25);
glVertex2f(273, 246);
glVertex2f(263, 272);
glVertex2f(253, 246);
glEnd();
}

public void rumput7() {
//rumput yang dibelakang rumah

glBegin(GL_POLYGON);
glVertex2f(1150, 227);
glVertex2f(0, 222);
glVertex2f(0, 313);
glVertex2f(1150, 265);
glEnd();

}

public void rumput8() {
//rumput yang dibelakang rumah

glBegin(GL_POLYGON);
glVertex2f(1150, 263);
glVertex2f(787, 269);
glVertex2f(311, 301);
glVertex2f(1150, 273);
glEnd();

}

public void rumput9() {
//rumput yang dibelakang rumah

glBegin(GL_POLYGON);
glVertex2f(367, 298);
glVertex2f(267, 285);
glVertex2f(155, 308);
glEnd();

}

public void rumput10() {
//rumput yang dibelakang rumah

glBegin(GL_POLYGON);
glVertex2f(0, 261);
glVertex2f(0, 310);
glVertex2f(139, 309);
glEnd();

}

public void pasirjalan2() {
//pasir yang dibelakang rumah

glBegin(GL_POLYGON);
glVertex2f(0, 234);
glVertex2f(99, 273);
glVertex2f(39, 296);
glVertex2f(66, 313);
glVertex2f(69, 313);
glVertex2f(54, 297);
glVertex2f(111, 272);
glVertex2f(0, 228);
glEnd();

}

public void gunungijo() {

glBegin(GL_POLYGON);
glVertex2f(1150, 274);
glVertex2f(1133, 272);
glVertex2f(1021, 354);
glVertex2f(1150, 327);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(878, 298);
glVertex2f(814, 334);
glVertex2f(921, 314);
glEnd();

}

public void gunungijo2() {

glBegin(GL_POLYGON);
glVertex2f(1132, 273);
glVertex2f(839, 283);
glVertex2f(1021, 354);
glEnd();

glBegin(GL_POLYGON);
glVertex2f(909, 281);
glVertex2f(705, 287);
glVertex2f(814, 334);
glEnd();
}

public void gunungijo3() {

glBegin(GL_POLYGON);
glVertex2f(689, 288);
glVertex2f(572, 292);
glVertex2f(603, 304);
glVertex2f(621, 296);
glVertex2f(659, 310);
glEnd();
}

public void gunungbiru() {
//gunung 1
glBegin(GL_POLYGON);
glVertex2f(1150, 272);
glVertex2f(592, 283);
glVertex2f(988, 453);
glEnd();

//gunung 2
glBegin(GL_POLYGON);
glVertex2f(1034, 278);
glVertex2f(474, 295);
glVertex2f(736, 423);
glEnd();

}

public void pohon1() {

//Ini Bayangannya
glBegin(GL_POLYGON);
glColor3d(0.21, 0.30, 0.19);
glVertex2f(739, 34);
glVertex2f(680, 17);
glVertex2f(564, 44);
glVertex2f(672, 49);
glVertex2f(722, 36);
glEnd();

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(722, 34);
glVertex2f(730, 63);
glVertex2f(739, 34);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(730, 48);
glVertex2f(680, 97);
glVertex2f(686, 121);
glVertex2f(705, 116);
glVertex2f(691, 138);
glVertex2f(731, 270);
glVertex2f(750, 210);
glVertex2f(740, 188);
glVertex2f(753, 197);
glVertex2f(783, 97);
glEnd();

}

public void pohon2() {

//Ini Bayangannya
glBegin(GL_POLYGON);
glColor3d(0.21, 0.30, 0.19);
glVertex2f(609, 63);
glVertex2f(569, 50);
glVertex2f(490, 68);
glVertex2f(563, 72);
glVertex2f(598, 63);
glEnd();

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(598, 62);
glVertex2f(604, 82);
glVertex2f(608, 62);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(603, 72);
glVertex2f(568, 105);
glVertex2f(573, 121);
glVertex2f(586, 117);
glVertex2f(577, 133);
glVertex2f(604, 223);
glVertex2f(617, 181);
glVertex2f(611, 167);
glVertex2f(619, 173);
glVertex2f(640, 105);
glEnd();

}

public void pohon3() {

//Ini Bayangannya
glBegin(GL_POLYGON);
glColor3d(0.21, 0.30, 0.19);
glVertex2f(348, 127);
glVertex2f(314, 117);
glVertex2f(247, 132);
glVertex2f(309, 135);
glVertex2f(338, 127);
glEnd();

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(337, 128);
glVertex2f(342, 146);
glVertex2f(348, 128);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(343, 136);
glVertex2f(317, 160);
glVertex2f(344, 246);
glVertex2f(369, 160);
glEnd();

}

public void pohon4() {
//Ini Bayangannya part 2
glBegin(GL_POLYGON);
glColor3d(0.53, 0.28, 0.20);
glVertex2f(196, 163);
glVertex2f(162, 152);
glVertex2f(95, 164);
glVertex2f(121, 167);
glEnd();

//Ini Bayangannya part 1
glBegin(GL_POLYGON);
glColor3d(0.21, 0.30, 0.19);
glVertex2f(196, 163);
glVertex2f(162, 152);
glVertex2f(121, 167);
glVertex2f(158, 170);
glVertex2f(187, 163);
glEnd();

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(185, 163);
glVertex2f(191, 181);
glVertex2f(196, 163);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(191, 171);
glVertex2f(166, 195);
glVertex2f(192, 281);
glVertex2f(217, 195);
glEnd();

}

public void pohon5() {

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(1052, 169);
glVertex2f(1052, 223);
glVertex2f(1057, 223);
glVertex2f(1057, 169);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(1056, 188);
glVertex2f(1030, 212);
glVertex2f(1056, 298);
glVertex2f(1082, 212);
glEnd();

}

public void pohon6() {

//Ini Batangnya
glBegin(GL_POLYGON);
glColor3d(0.11, 0.21, 0.08);
glVertex2f(1115, 190);
glVertex2f(1115, 245);
glVertex2f(1122, 245);
glVertex2f(1122, 190);
glEnd();

//Ini Pohonnya
glColor3d(0.24, 0.32, 0.25);
glBegin(GL_POLYGON);
glVertex2f(1118, 210);
glVertex2f(1093, 235);
glVertex2f(1097, 247);
glVertex2f(1107, 244);
glVertex2f(1099, 255);
glVertex2f(1118, 320);
glVertex2f(1128, 290);
glVertex2f(1124, 279);
glVertex2f(1130, 283);
glVertex2f(1144, 235);
glEnd();

}

Terakhir, Membuat aplikasi berjalan

Inilah script untuk aplikasi tersebut berjalan

public void create() throws LWJGLException {
Display.setDisplayMode(new DisplayMode(DISPLAY_WIDTH, DISPLAY_HEIGHT));
Display.setFullscreen(false);
Display.setTitle(“Menggambar Gunung”);
Display.create();
initGL();
resizeGL();
}

void initGL() {
glClearColor(0.0f, 0.7f, 1.0f, 1.0f);
glDisable(GL_DEPTH_TEST);
glDisable(GL_LIGHTING);
}

private void resizeGL() {
glViewport(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, DISPLAY_WIDTH, 0.0f, DISPLAY_HEIGHT);
glPushMatrix();
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glPushMatrix();
}

public void run() {
while (!Display.isCloseRequested()
&& !Keyboard.isKeyDown(Keyboard.KEY_ESCAPE)) {
if (Display.isVisible()) {
update();
render();
} else {
if (Display.isDirty()) {
render();
}
try {
Thread.sleep(100);
} catch (InterruptedException ex) {
}
}
Display.update();
Display.sync(60);
}
}

private void update() {
}

private void destroy() {
Mouse.destroy();
Keyboard.destroy();
Display.destroy();
}

public static void main(String[] args) {
PemandanganAgung b1 = new PemandanganAgung();
try {
b1.create();
b1.run();
} catch (LWJGLException ex) {
Logger.getLogger(PemandanganAgung.class.getName()).log(Level.SEVERE, null,
ex);
}
}

Hasilnya

Itulah dari tutorial saya, semoga bermanfaat dan bisa dikembangkan kembali 🙂