GRAFIKA KOMPUTER ANIMASI MENGGUNAKAN NETBEANS

 ANIMASI  MENGGUNAKAN SOURCE CODE  NETBEANS
BERIKUT SOURCE CODENYA 


import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public final class bola extends JFrame {

private JPanel jp;
private Timer timer;

public bola() {
initComponents();

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.pack();
this.setLocationByPlatform(true);
timer.start();
}

public void initComponents() {
ActionListener al = (ActionEvent evt) -> {
jp.repaint();
};
timer = new Timer(10,al); 
jp = new JPanel() {
int x;
int y;
int xspeed = 1;
int yspeed = 1;
Dimension preferredSize = new Dimension(500, 300);
@Override
public Dimension getPreferredSize() {
return preferredSize;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.move();
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(x, y, 20, 20);
                              
this.move();
Graphics2D g3 = (Graphics2D)g;
g3.setRenderingHint(
RenderingHints.KEY_ANTIALIASING, 
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(x, y, 60, 60);
                
this.move();
Graphics2D g4 = (Graphics2D)g;
g4.setRenderingHint(
RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g.drawOval(x, y, 30, 30);
}
void move() {
x = x + xspeed;
y = y + yspeed;
if (x < 0) {
x = 0;
xspeed = -xspeed;
} else if (x > getWidth() - 10) {
x = getWidth() - 10;
xspeed = -xspeed;
}
if (y < 0) {
y = 0;
yspeed = -yspeed;
} else if (y == getHeight() - 10) {
y = getHeight() - 10;
yspeed = -yspeed;
}
}
};
jp.setBackground(Color.ORANGE);

this.add(jp);
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {

public void run() {
new bola().setVisible(true);
}
});
}
}


Komentar

Postingan Populer