1. Latar Belakang Grafik vektor banyak sekali digunakan pada pembuatan game-game 2 Dimensi dimana terdapat banyak method-method yang sudah disediakan oleh Java seperti ketika kita ingin menangani pengujian tabrakan, menampilkan bound berupa rectangle dan masih banyak lagi. Grafik vektor merupakan pelajaran dasar yang harus dipelajari sebelum memasuki pembahasan grafik bitmap. pada bab ini juga dibahas masalah tentang penanganan grafis full screen dimana game yang akan kita buat dapat dimainkan dalam satu layar penuh.
2. Tujuan Setelah mempelajari lab activity ini peserta diharapkan dapat:
Mengetahui fungsi dari class Shape.
Menggambar garis menggunakan class Graphics2D.
Mengetahui perbedaan antara Rectangle2D dan RoundRectangle2D.
Menggambar menggunakan Arc2D.
Memahami fungsi dari Polygon.
Membuat tampilan mode Full-Screen
Menjalankan dan menutup mode Full-screen
Versi 1.0
1|Page
3. Percobaan
Percobaan 1: Membuat Class PercobaanJava2D import javax.swing.JFrame; import java.awt.*; import java.awt.geom.*; public class PercobaanJava2D extends JFrame{ Shape Shape Shape Shape
garis, garis1; rect,rect1; round_rect,round_rect1; ellipse;
public static void main(String args[]){ PercobaanJava2D p=new PercobaanJava2D(); } PercobaanJava2D(){ setTitle("Percobaan Java2D"); garis = new Line2D.Float(50,50,150,150); // Menggunkan Point2D untuk menggambar garis Point2D awal=new Point2D.Float(200,50); Point2D akhir=new Point2D.Float(300,150); garis1 = new Line2D.Float(awal, akhir); round_rect = new RoundRectangle2D.Float(320,50,100,100,20,20); round_rect1 = new RoundRectangle2D.Float(430,50,100,100,20,20); rect = new Rectangle2D.Float(50,200,100,100); rect1 = new Rectangle2D.Float(200,200,100,100); ellipse = new Ellipse2D.Float(330,200,200,100); setSize(600,400); setVisible(true); setDefaultCloseOperation(this.EXIT_ON_CLOSE); } public void paint(Graphics g){ Graphics2D g2d=(Graphics2D)g; g2d.draw(garis); g2d.draw(garis1); g2d.setColor(Color.BLUE); g2d.draw(rect); g2d.fill(rect1); g2d.setColor(Color.red); g2d.draw(round_rect); g2d.fill(round_rect1); g2d.setColor(Color.YELLOW); g2d.fill(ellipse);
Versi 1.0 } }
2|Page
Percobaan 2 : Class PercobaanArc2d import javax.swing.JFrame; import java.awt.*; import java.awt.geom.*; public class PercobaanArc2d extends JFrame { Shape arc1,arc2,arc3,arc4,arc5,arc6; public static void main(String args[]){ PercobaanArc2d p=new PercobaanArc2d(); } PercobaanArc2d(){ setTitle("Percobaan ARC 2 Dimensi"); arc1 = new Arc2D.Float(20,50,100,100,30,270,Arc2D.PIE); arc2 = new Arc2D.Float(120,50,100,100,30,270,Arc2D.OPEN); arc3 = new Arc2D.Float(220,50,100,100,30,270,Arc2D.CHORD); arc4 = new Arc2D.Float(20,170,100,100,30,270,Arc2D.PIE); arc5 = new Arc2D.Float(120,170,100,100,30,270,Arc2D.OPEN); arc6 = new Arc2D.Float(220,170,100,100,30,270,Arc2D.CHORD); setSize(400,300); setVisible(true); setDefaultCloseOperation(this.EXIT_ON_CLOSE); } public void paint(Graphics g){ Graphics2D g2d=(Graphics2D)g; g2d.setColor(Color.ORANGE); g2d.draw(arc1); g2d.draw(arc2); g2d.draw(arc3); g2d.setColor(Color.red); g2d.fill(arc4); g2d.setColor(Color.blue); g2d.fill(arc5); g2d.setColor(Color.yellow); g2d.fill(arc6); } }
Versi 1.0
3|Page
Percobaan 3 : Pembuatan class PercobaanPoligon import javax.swing.JFrame; import java.awt.*; import java.awt.geom.*; public class PercobaanPoligon extends JFrame { Polygon poligon,poligon1; public static void main(String args[]){ PercobaanPoligon p=new PercobaanPoligon(); } PercobaanPoligon(){ setTitle("Percobaan Poligon"); int titikX[] = {200,100,130,270,300}; int titikY[] = {50,150,270,270,150}; poligon= new Polygon(titikX, titikY, titikX.length); for(int i=0;i
Versi 1.0
4|Page
Percobaan 4 : membuat class ScreenManager dan class TestFullScreen import java.awt.*; import javax.swing.JFrame; public class ScreenManager { private GraphicsDevice device; public ScreenManager() { GraphicsEnvironment environment = GraphicsEnvironment.getLocalGraphicsEnvironment(); device = environment.getDefaultScreenDevice(); } private static final DisplayMode Mode_Pilihan[] = { new DisplayMode(1024, 768, 32, 0), new DisplayMode(1024, 768, 24, 0), new DisplayMode(1024, 768, 16, 0), new DisplayMode(800, 600, 32, 0), new DisplayMode(800, 600, 24, 0), new DisplayMode(800, 600, 16, 0), new DisplayMode(640, 480, 32, 0), new DisplayMode(640, 480, 24, 0), new DisplayMode(640, 480, 16, 0) }; public DisplayMode setResolusiLayar(int nilai){ if(nilai >=9)nilai=0; DisplayMode sistemMode[] = device.getDisplayModes(); for (int j = 0; j < sistemMode.length; j++) { if (perikasDisplayMode(Mode_Pilihan[nilai], sistemMode[j])) { return Mode_Pilihan[nilai]; } } return null; } public DisplayMode getCurrentDisplayMode() { return device.getDisplayMode(); } public boolean perikasDisplayMode(DisplayMode mode1,DisplayMode mode2) { if (mode1.getWidth() != mode2.getWidth() || mode1.getHeight() != mode2.getHeight()) { return false; } if (mode1.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && mode2.getBitDepth() != DisplayMode.BIT_DEPTH_MULTI && mode1.getBitDepth() != mode2.getBitDepth()) { return false; }
Versi 1.0
5|Page
if (mode1.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && mode2.getRefreshRate() != DisplayMode.REFRESH_RATE_UNKNOWN && mode1.getRefreshRate() != mode2.getRefreshRate()) { return false; } return true; } public void setFullScreen(DisplayMode displayMode,JFrame frame) { if(device.isFullScreenSupported()){ frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setUndecorated(true); frame.setResizable(false); device.setFullScreenWindow(frame); if(displayMode != null && device.isDisplayChangeSupported()){ try { device.setDisplayMode(displayMode); } catch (IllegalArgumentException ex) { } } }else{ System.out.println("System Operasi tidak mendukung Mode Fullscreen"); } } public void tutupScreen() { Window window = device.getFullScreenWindow(); if (window != null) { window.dispose(); } device.setFullScreenWindow(null); } }
Versi 1.0
6|Page
import java.awt.*; import javax.swing.ImageIcon; public class TestFullScreen{ public static void main(String args[]) { TestFullScreen test = null; test = new TestFullScreen(); test.cobaFullScreen(); } private ScreenManager screen; public void cobaFullScreen() { screen = new ScreenManager(); try { DisplayMode displayMode = screen.setResolusiLayar(7); screen.setFullScreen(displayMode,new PercobaanJava2D()); } finally { // screen.tutupScreen(); } } }
Versi 1.0
7|Page
Kunci Jawaban: Percobaan 1:
Percobaan 2:
Versi 1.0
8|Page
Percobaan 3
Versi 1.0
9|Page