Pagi bloger ane mau share tugas java lagi nih kali ini Membuat Objek Bergerak di Java Neatben
langsung aja di coba nih source codenya
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Animation111100042 extends JPanel implements ActionListener {
int a, b, x, y;
Timer timer;
JLabel label1;
Animation111100042() {
a = 30;
b = 30;
timer = new Timer(3, this);
label1 = new JLabel("Tugas 5 Animation by aldkidrebel");
label1.setBounds(a, b, 100, 20);
}
public void actionPerformed(ActionEvent e) {
System.out.println("a = " + a);
System.out.println("b = " + b);
repaint();
a += 1;
b += 0;
if (a > 500) {
a -= 1;
b += 1;
}
if (b > 480) {
a -= 2;
b -= 0;
}
if (a < 30) {
a -= 1;
b += -1;
}
if (b == 30) {
a += 1;
b += 0;
}
{
x -= 1;
y -= 0;
if (x < -200) {
x = 600;
}
}
}
public void paintComponent(Graphics g2d) {
super.paintComponent(g2d);
g2d.setColor(Color.yellow);
g2d.fillRect(a, b, 80, 30);
g2d.setColor(Color.red);
g2d.fillOval(a, a, 20, 20);
g2d.fill3DRect(b, b, 20, 20, true);
g2d.setColor(Color.green);
g2d.fillOval(b, a, 20, 20);
label1.setBounds(x, y, 200, 20);
add(label1);
}
public static void main(String[] args) {
JFrame Frame = new JFrame("Tugas 5 Pemrograman Animation Ali 111100042");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Animation111100042 KotakMuter = new Animation111100042();
Frame.add(KotakMuter);
Frame.setSize(600, 550);
Frame.setVisible(true);
Frame.setLocationRelativeTo(null);
Frame.setResizable(false);
KotakMuter.timer.start();
}
}
keterangan Jika ingin membuat Kotak atau Bulat dengan bolak balik pertikal atau horijontal gunakan, fungsi if and else dibawah codingan ini
public void actionPerformed(ActionEvent e) {
System.out.println("a = " + a);
System.out.println("b = " + b);
repaint();
-------------------------------
if (kanan == true) {
dan seterusnya lanjut gawe dulu gan
Output Program
langsung aja di coba nih source codenya
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Animation111100042 extends JPanel implements ActionListener {
int a, b, x, y;
Timer timer;
JLabel label1;
Animation111100042() {
a = 30;
b = 30;
timer = new Timer(3, this);
label1 = new JLabel("Tugas 5 Animation by aldkidrebel");
label1.setBounds(a, b, 100, 20);
}
public void actionPerformed(ActionEvent e) {
System.out.println("a = " + a);
System.out.println("b = " + b);
repaint();
a += 1;
b += 0;
if (a > 500) {
a -= 1;
b += 1;
}
if (b > 480) {
a -= 2;
b -= 0;
}
if (a < 30) {
a -= 1;
b += -1;
}
if (b == 30) {
a += 1;
b += 0;
}
{
x -= 1;
y -= 0;
if (x < -200) {
x = 600;
}
}
}
public void paintComponent(Graphics g2d) {
super.paintComponent(g2d);
g2d.setColor(Color.yellow);
g2d.fillRect(a, b, 80, 30);
g2d.setColor(Color.red);
g2d.fillOval(a, a, 20, 20);
g2d.fill3DRect(b, b, 20, 20, true);
g2d.setColor(Color.green);
g2d.fillOval(b, a, 20, 20);
label1.setBounds(x, y, 200, 20);
add(label1);
}
public static void main(String[] args) {
JFrame Frame = new JFrame("Tugas 5 Pemrograman Animation Ali 111100042");
Frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Animation111100042 KotakMuter = new Animation111100042();
Frame.add(KotakMuter);
Frame.setSize(600, 550);
Frame.setVisible(true);
Frame.setLocationRelativeTo(null);
Frame.setResizable(false);
KotakMuter.timer.start();
}
}
keterangan Jika ingin membuat Kotak atau Bulat dengan bolak balik pertikal atau horijontal gunakan, fungsi if and else dibawah codingan ini
public void actionPerformed(ActionEvent e) {
System.out.println("a = " + a);
System.out.println("b = " + b);
repaint();
-------------------------------
if (kanan == true) {
dan seterusnya lanjut gawe dulu gan
Output Program
2 Komentar
-
Unknown May 13, 2014 at 10:18 AM TERIMA KASIH KAKAK..SANGAT MEMBANTU :D -
karawangTeam December 11, 2014 at 12:56 AM yupzzz