Jumpa lagi di Imposible Sin Vosotras, kali ini yang mungki ane postingin yaitu tentang membuat animasi sederhana menggunakan bahasa pemrograman java. Dalam animasi ini, saya pake aplikasi JCreator.
Berikut listing programnya :
Simpan dengan animasiOrang.java
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;
public class animasiOrang extends JPanel {
//Membuat variabel thread
Thread animasi, repaint;
int x=10,y=10,a=10, batas = 240;
int xOrang = -100, yOrang = 100, v = 1;
// <editor-fold defaultstate="collapsed" desc="Siman Mendrofa">
public animasiOrang() {
setPreferredSize(new Dimension(600, 300));
setFocusable(true);
setBackground(Color.green);
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("Siman Mendrofa || 0910000338", 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.red);
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 animasiOrang());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// </editor-fold>
public static void main(String salis[]) {
new animasiOrang().xplay();
}
}
Hasilnya Seperti gambar berikut :
Berikut listing programnya :
Simpan dengan animasiOrang.java
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;
public class animasiOrang extends JPanel {
//Membuat variabel thread
Thread animasi, repaint;
int x=10,y=10,a=10, batas = 240;
int xOrang = -100, yOrang = 100, v = 1;
// <editor-fold defaultstate="collapsed" desc="Siman Mendrofa">
public animasiOrang() {
setPreferredSize(new Dimension(600, 300));
setFocusable(true);
setBackground(Color.green);
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("Siman Mendrofa || 0910000338", 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.red);
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 animasiOrang());
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
// </editor-fold>
public static void main(String salis[]) {
new animasiOrang().xplay();
}
}
Hasilnya Seperti gambar berikut :
Artikel Lain :
- Daftar Kata Dan Istilah Dalam Jaringan
- Perkenalan Diri Saat Wawancara Kerja
- Cara Memperkenalkan Diri
0 komentar:
Posting Komentar
Please Give Your Feedback Or Message.
Thank You!!?