JBall
package pallina; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JComponent; public class JBall extends JComponent implements Runnable { private boolean running = false; private int vx = 10, vy = 10; public JBall() { this.setPreferredSize(new Dimension(15, 15)); } public boolean isRunning() { return running; } public void setRunning(boolean running) { this.running = running; } public int getVx() { return vx; } public void setVx(int vx) { if (Math.abs(vx) <= 10) this.vx = vx; } public int getVy() { return vy; } public void setVy(int vy) { if (Math.abs(vy) <= 10) this.vy = vy; } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); int w = this.getWidth(); int h = this.getHeight(); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(Color.GREEN); g2.fillOval(0, 0, w, h); g2.setColor(Color.BLACK); g2.drawOval(0, 0, w - 1, h - 1); } public void start() { if (!running) { running = true; Thread t = new Thread(this); t.setDaemon(true); t.start(); } } public void stop() { running = false; } @Override public void run() { while (running) { int x = this.getX(); int y = this.getY(); x += vx; y += vy; Component contenitore = this.getParent(); if (x < 0) { vx = Math.abs(vx); x += vx; } if (x > contenitore.getWidth() - this.getWidth()) { vx = -Math.abs(vx); x += vx; } if (y < 0) { vy = Math.abs(vy); y += vy; } if (y > contenitore.getHeight() - this.getHeight()) { vy = -Math.abs(vy); y += vy; } this.setLocation(x, y); try { Thread.sleep(20); } catch (InterruptedException e) { } } } }
Main
package pallina; import java.awt.EventQueue; import javax.swing.JFrame; import javax.swing.SpringLayout; import javax.swing.JPanel; import javax.swing.border.LineBorder; import java.awt.Color; import javax.swing.JButton; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; public class Main extends JFrame { private JBall ball; /** * Launch the application. */ public static void main(String[] args) { EventQueue.invokeLater(new Runnable() { public void run() { try { Main frame = new Main(); frame.setVisible(true); } catch (Exception e) { e.printStackTrace(); } } }); } /** * Create the frame. */ public Main() { setTitle("Pallina"); setBounds(100, 100, 450, 300); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); SpringLayout springLayout = new SpringLayout(); getContentPane().setLayout(springLayout); JButton btnStart = new JButton("Start"); btnStart.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent arg0) { start(); } }); springLayout.putConstraint(SpringLayout.WEST, btnStart, 10, SpringLayout.WEST, getContentPane()); springLayout.putConstraint(SpringLayout.SOUTH, btnStart, -10, SpringLayout.SOUTH, getContentPane()); getContentPane().add(btnStart); JButton btnStop = new JButton("Stop"); btnStop.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { stop(); } }); springLayout.putConstraint(SpringLayout.NORTH, btnStop, 0, SpringLayout.NORTH, btnStart); springLayout.putConstraint(SpringLayout.WEST, btnStop, 6, SpringLayout.EAST, btnStart); getContentPane().add(btnStop); JPanel panel = new JPanel(); panel.setLayout(null); panel.setBackground(Color.WHITE); panel.setBorder(new LineBorder(new Color(0, 0, 0))); springLayout.putConstraint(SpringLayout.NORTH, panel, 10, SpringLayout.NORTH, getContentPane()); springLayout.putConstraint(SpringLayout.WEST, panel, 10, SpringLayout.WEST, getContentPane()); springLayout.putConstraint(SpringLayout.EAST, panel, -10, SpringLayout.EAST, getContentPane()); springLayout.putConstraint(SpringLayout.SOUTH, panel, -6, SpringLayout.NORTH, btnStart); getContentPane().add(panel); ball = new JBall(); ball.setBounds(10, 10, 15, 15); panel.add(ball); } private void start() { ball.start(); } private void stop() { ball.stop(); } }
Progetto Eclipse: Pallina