RE:: Project Uas (TUGAS GRAFIKA KOMPUTER (MEMBUAT ANIMASI BERGERAK)
Oke kali ini saya akan membuat gambar animasi bergerak di Java Netbeans AWT cara membuat project barunya silahkan lihat di postingan saya yang sebelumnya ...
oke ketikkan atau copas coding di bawah ini :
/*
* 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 orangbergerak;
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 java.awt.geom.Ellipse2D;
import java.awt.geom.GeneralPath;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
/**
*
* @author jOWLfVolks
*/
public class orangbergerak extends JPanel {
//Membuat variabel thread
Thread animasi, repaint;
int x=10,y=10,a=10, batas = 240;
int xOrang = -100, yOrang = 100, v = 1;
public orangbergerak() {
setPreferredSize(new Dimension(500, 300));
setFocusable(true);
setBackground(Color.black);
requestFocusInWindow();
initThread();
//untuk memulai thread
animasi.start();
repaint.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
drawText(g2);
drawOrang(g2, xOrang, yOrang, v);
}
//method untuk menampung thread
public void initThread() {
animasi = new Thread(new Runnable() {
public void run() {
while (true) {
// <editor-fold defaultstate="collapsed" desc="menggerakkan Orang">
if (xOrang < 500) {
if (v == 1) {
xOrang = xOrang + 10;
v = 2;
} else {
xOrang = xOrang + 10;
v = 1;
}
} else {
xOrang = -100;
}
// </editor-fold>
try {
//untuk mengatur kecepatan thread, semakin kecil nilai semakin cepat
Thread.sleep(150);
} catch (Exception e) {
}
}
}
});
repaint = new Thread(new Runnable() {
public void run() {
while (true) {
// <editor-fold defaultstate="collapsed" desc="Untuk mengacak warna">
if(a<batas){
a=a+15;
batas=240;
}else{
a=a-15;
batas=15;
}
// </editor-fold>
// <editor-fold defaultstate="collapsed" desc="menggerakkan text">
x=(int)(Math.random()*10);
y=(int)(Math.random()*10);
// </editor-fold>
try {
//untuk mengatur kecepatan thread, semakin kecil nilai semakin cepat
Thread.sleep(10);
} catch (Exception e) {
}
//untuk memanggil repaint
SwingUtilities.invokeLater(new Runnable() {
public void run() {
repaint();
}
});
}
}
});
}
public void drawText(Graphics2D g2) {
g2.setFont(new Font("Comic sans ms", 1, 35));
g2.setColor(new Color((int)(Math.random()*250), (int)(Math.random()*250), (int)(Math.random()*250), a));
g2.drawString("Stikom Artha Buana Kupang", 20+x, 50+y);
}
public void drawOrang(Graphics2D g2, int x, int y, int v) {
g2.setStroke(new BasicStroke(6, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
Ellipse2D kepala = new Ellipse2D.Double(x + 38, y + 10, 20, 20);
GeneralPath gp = new GeneralPath();
gp.moveTo(x + 46, y + 32);
gp.lineTo(x + 34, y + 52);
gp.lineTo(x + 27, y + 73);
gp.moveTo(x + 46, y + 32);
gp.lineTo(x + 54, y + 55);
gp.lineTo(x + 71, y + 72);
gp.moveTo(x + 46, y + 32);
gp.lineTo(x + 46, y + 69);
gp.lineTo(x + 46, y + 93);
gp.lineTo(x + 26, y + 112);
gp.moveTo(x + 46, y + 69);
gp.lineTo(x + 59, y + 93);
gp.lineTo(x + 66, y + 118);
//=============================================================
Ellipse2D kepala1 = new Ellipse2D.Double(x + 42, y + 10, 20, 20);
GeneralPath gp1 = new GeneralPath();
gp1.moveTo(x + 46, y + 29);
gp1.lineTo(x + 40, y + 48);
gp1.lineTo(x + 40, y + 77);
gp1.moveTo(x + 46, y + 29);
gp1.lineTo(x + 49, y + 58);
gp1.lineTo(x + 51, y + 74);
gp1.moveTo(x + 46, y + 29);
gp1.lineTo(x + 46, y + 68);
gp1.lineTo(x + 52, y + 90);
gp1.lineTo(x + 46, y + 120);
gp1.moveTo(x + 46, y + 68);
gp1.lineTo(x + 60, y + 92);
gp1.lineTo(x + 38, y + 106);
g2.setPaint(Color.white);
if (v == 1) {
g2.fill(kepala);
g2.draw(gp);
g2.draw(kepala);
} else if (v == 2) {
g2.fill(kepala1);
g2.draw(gp1);
g2.draw(kepala1);
}
}
public void xplay() {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
final JFrame frame = new JFrame("Animasi Orang");
frame.setDefaultCloseOperation(
JFrame.EXIT_ON_CLOSE);
frame.add(new orangbergerak());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// </editor-fold>
public static void main(String salis[]) {
new orangbergerak().xplay();
}
}
0 komentar