Animasi sederhana sederhana Java Netbenas
kali ini untuk saya akan memposting animasi ssederhana di netbeans java ..
teks di gambar akan bergerak dari atas ke bawah..
langsung saja,
pertama buat project baru Java lalu Java aplikasication dan namanya di gnnti dengan TeksGerak,
lalu pada project klik kanan new lalu pilih newFrame Form
lalu diganti nama dengan TeksGerakJFrame
lalu ini kodingnya,
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class TeksGerakJFrame extends JFrame implements Runnable { // ini sudah di ganti
Thread th = new Thread (this);
int x=15;
public TeksGerakJFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,400); // ukuran frame
setLocation(500,0); //letak frame di layar
th.start();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.clearRect(0, 0, 200, 200); //penghapusan jejak langkah perpindahan string
g.drawString("Jufri Sinantong", 95, x); //arah pergeseran string, x atau y
}
public void run(){
int y=0;
while(true){
try {
th.sleep(90); //kecepatan jalannya string
x+=15; //langkah(spasi)pergerakan string ke bawah
if(x==300){
x=0;} //tempat mulainya perulangan
if (x>=290){ //tempat string menghilang/berhenti ketika di break
y++;
if(y==0){ //apabila teks ingin berhenti dalam pengulangan ke 3, maka angka 0 harus diganti dengan 3
break;
}
}
}
catch (Exception e) {
}repaint();
}
}
/** Creates new form TeksGerakJFrame */
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
new TeksGerakJFrame();
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
}
// Variables declaration - do not modify
// End of variables declaration
}
sekian
sumber :http://www.katabah.com/2014/03/cara-membuat-animasi-teks-bergerak-dari.html
teks di gambar akan bergerak dari atas ke bawah..
langsung saja,
pertama buat project baru Java lalu Java aplikasication dan namanya di gnnti dengan TeksGerak,
lalu pada project klik kanan new lalu pilih newFrame Form
lalu diganti nama dengan TeksGerakJFrame
lalu ini kodingnya,
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
public class TeksGerakJFrame extends JFrame implements Runnable { // ini sudah di ganti
Thread th = new Thread (this);
int x=15;
public TeksGerakJFrame(){
setDefaultCloseOperation(EXIT_ON_CLOSE);
setVisible(true);
setSize(400,400); // ukuran frame
setLocation(500,0); //letak frame di layar
th.start();
}
public void paint(Graphics g){
g.setColor(Color.black);
g.clearRect(0, 0, 200, 200); //penghapusan jejak langkah perpindahan string
g.drawString("Jufri Sinantong", 95, x); //arah pergeseran string, x atau y
}
public void run(){
int y=0;
while(true){
try {
th.sleep(90); //kecepatan jalannya string
x+=15; //langkah(spasi)pergerakan string ke bawah
if(x==300){
x=0;} //tempat mulainya perulangan
if (x>=290){ //tempat string menghilang/berhenti ketika di break
y++;
if(y==0){ //apabila teks ingin berhenti dalam pengulangan ke 3, maka angka 0 harus diganti dengan 3
break;
}
}
}
catch (Exception e) {
}repaint();
}
}
/** Creates new form TeksGerakJFrame */
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
//
private void initComponents() {
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 400, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGap(0, 300, Short.MAX_VALUE)
);
pack();
}//
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
new TeksGerakJFrame();
//
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(TeksGerakJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//
/* Create and display the form */
}
// Variables declaration - do not modify
// End of variables declaration
}
sekian
sumber :http://www.katabah.com/2014/03/cara-membuat-animasi-teks-bergerak-dari.html
Komentar