MEMBUAT ANIMASI SMILE
NAMA : NOVITA SABUNA
NIM : 13110239
/*
* 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 smilenovita;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;
@SuppressWarnings("serial")
class animasiMobil extends JPanel{
private static final int D_W = 400;
private static final int D_H = 400;
List<smile> smile;
@SuppressWarnings({ "unchecked", "rawtypes" })
public animasiMobil() {
setBackground(new Color(50, 200, 200));
setLayout(null);
smile = new ArrayList();
smile.add(new smile(100, 300));
smile.add(new smile(200, 100));
Timer timer = new Timer(50, new ActionListener(){
public void actionPerformed(ActionEvent e) {
for (smile smile : smile) {
smile.move();
repaint();
}
}
});
timer.start();
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
for (smile smile : smile) {
smile.drawsmile(g);
}
}
@Override
public Dimension getPreferredSize() {
return new Dimension(D_W, D_H);
}
public class smile {
private static final int INCREMENT = 5;
int x, y;
public smile(int x, int y) {
this.x = x;
this.y = y;
}
public void drawsmile(Graphics g) {
g.setColor(Color.yellow);
g.fillOval(x, y, 100, 100);
//g.fillRect(x, y, 100, 30);
g.setColor(Color.black); // body
g.fillOval(x + 15, y + 20, 20, 20); // mata
g.fillOval(x + 60, y + 20, 20, 20); // mata
//g.fillRect(x + 15, y - 20, 60, 20); // top
g.fillOval(x + 15, y + 70, 70, 5);
g.setColor(Color.white);
}
public void move() {
if (x == D_W) {
x = 0;
} else {
x += INCREMENT;
}
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new JFrame();
frame.getContentPane().add(new animasiMobil());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Novita Sabuna | 13110239 | emoticon");
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
}
OUTPUT :