[[oktatas:programozás:java|< Java]] ====== Java Animáció ====== * **Szerző:** Sallai András * Copyright (c) 2014, Sallai András * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC BY-SA 4.0]] * Web: https://szit.hu ===== Animáció gombnyomásra ===== ==== Mehet a kocka ==== Az alábbi példában egy kis négyzet addig megy jobbra, amíg bal sarka el nem éri a 100 pixeles határt. A négyzetet JPanel objektumra rajzoltam, de ez opcionális. import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.Timer; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Graphics; import java.awt.Color; class Program extends JFrame { JButton MehetGomb = new JButton("Mehet"); SajatPanel panel1 = new SajatPanel(); int x, y; Timer mehetIdozito; Program() { mehetIdozito = new Timer (100, new Mehet_Timer()); x = 5; y = 5; MehetGomb.addActionListener(new MehetGomb_Click()); MehetGomb.setBounds(200, 300, 100, 30); panel1.setBounds(0,0, 200, 200); add(MehetGomb); add(panel1); setSize(800, 600); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } class Mehet_Timer implements ActionListener { public void actionPerformed(ActionEvent e) { if(x<100) x++; else mehetIdozito.stop(); repaint(); } } class SajatPanel extends JPanel { public void paint(Graphics g) { g.setColor (Color.red); g.fillRect(x, y, 50, 50); } } class MehetGomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0; i<30; i++) { mehetIdozito.start(); } } } public static void main(String args[]) { new Program(); } } A "Mehet" nyomógomb elindítja az időzítőt. Amikor bekövetkezik az időzítés, újrarajzolás történik. Ha a négyzet eléri a 100 pixeles határt, akkor leáll az időzítő. ==== Kocka oda-vissza ==== Ebben a példában a kocka oda-vissza mozog. import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.Timer; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.Graphics; import java.awt.Color; class Program extends JFrame { JButton MehetGomb = new JButton("Mehet"); JButton VisszaGomb = new JButton("Vissza"); SajatPanel panel1 = new SajatPanel(); int x, y; Timer mehetIdozito; Timer visszaIdozito; Program() { mehetIdozito = new Timer (100, new MehetIdozito_Time()); visszaIdozito = new Timer (100, new VisszaIdozito_Time()); x = 5; y = 5; MehetGomb.addActionListener(new MehetGomb_Click()); MehetGomb.setBounds(200, 300, 100, 30); VisszaGomb.addActionListener(new VisszaGomb_Click()); VisszaGomb.setBounds(200, 340, 100, 30); panel1.setBounds(0,0, 200, 200); add(MehetGomb); add(VisszaGomb); add(panel1); setSize(800, 600); setLayout(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } class MehetIdozito_Time implements ActionListener { public void actionPerformed(ActionEvent e) { if(x<100) x++; else mehetIdozito.stop(); repaint(); } } class VisszaIdozito_Time implements ActionListener { public void actionPerformed(ActionEvent e) { if(x>5) x--; else visszaIdozito.stop(); repaint(); } } class SajatPanel extends JPanel { public void paint(Graphics g) { g.setColor (Color.red); g.fillRect(x, y, 50, 50); } } class MehetGomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0; i<30; i++) { mehetIdozito.start(); } } } class VisszaGomb_Click implements ActionListener { public void actionPerformed(ActionEvent e) { for(int i=0; i<30; i++) { visszaIdozito.start(); } } } public static void main(String args[]) { new Program(); } }