Merhaba arkadaşlar, bu yazımızla birlikte artık java ile görsel programlama örnekleri paylaşacağız. İlk yazımız olan buton ile herhangi bir nesneyi (top, kare, üçgen, resim) X ve Y kordinatlarını değiştirerek hareket ettirmeyi öğreneceğiz. Buradaki asıl mantık Sağ, Sol, Aşağı, Yukarı olarak adlandırdığımız butonlara ActionListener sınıfını kullanarak görev vermektir.
Örneğin; Aşağı butonuna bastığımızda topumuzun Y kordinatında + yönde yer değiştirmsini sağlayarak topumuzu aşağı doğru hareket ettirmiş olacağız.
Not: Bu kodlar ile buton, label ve resim de hareket ettirebilirsiniz. Dilerseniz KeyEvent sınıfını kullanarak klavye ile de hareket ettirebilirsiniz.
Yapacağımız proje şu şekilde olacaktır;
Java ile nesne, buton, resim hareket ettirme kodlarımız;
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
package teknografya; import java.awt.BorderLayout; import java.awt.EventQueue; import java.awt.Image; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JPanel; import javax.swing.border.EmptyBorder; import javax.swing.ImageIcon; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class nesne_hareket_ettirme extends JFrame { private JPanel contentPane; int x,y; public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { nesne_hareket_ettirme frame = new nesne_hareket_ettirme(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } public nesne_hareket_ettirme() { setTitle("Teknografya.com Nesne Hareketi"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setBounds(100, 100, 472, 507); contentPane = new JPanel(); contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); setContentPane(contentPane); contentPane.setLayout(null); Image top1 = new ImageIcon(this.getClass().getResource("/topp.png")).getImage(); JLabel top = new JLabel(""); top.setBounds(215, 146, 25, 35); top.setIcon(new ImageIcon(top1)); contentPane.add(top); JButton btn_asagi = new JButton("Aşağı"); btn_asagi.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { x = (int) top.getBounds().getX(); y = (int) top.getBounds().getY(); if(y<410) y += 20; top.setBounds(x, y, 25, 35); } }); btn_asagi.setBounds(178, 428, 97, 25); contentPane.add(btn_asagi); JButton btn_yukari = new JButton("Yukarı"); btn_yukari.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { x = (int) top.getBounds().getX(); y = (int) top.getBounds().getY(); if(y>0) y -= 20; top.setBounds(x, y, 25, 35); } }); btn_yukari.setBounds(178, 368, 97, 25); contentPane.add(btn_yukari); JButton btn_saga = new JButton("Sağa"); btn_saga.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { x = (int) top.getBounds().getX(); y = (int) top.getBounds().getY(); if(x<415) x += 20; top.setBounds(x, y, 25, 35); } }); btn_saga.setBounds(230, 399, 97, 25); contentPane.add(btn_saga); JButton btn_sola = new JButton("Sola"); btn_sola.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { x = (int) top.getBounds().getX(); y = (int) top.getBounds().getY(); if(x>0) x -= 20; top.setBounds(x, y, 25, 35); } }); btn_sola.setBounds(128, 399, 97, 25); contentPane.add(btn_sola); } } |
Sorunuz olursa konu altından belirtiniz.