|
|
|
![]() |
|
Strumenti |
![]() |
#1 |
Member
Iscritto dal: Sep 2008
Città: Padova
Messaggi: 172
|
[Java] Applet Movimento Palla
Salve a tutti ragazzi.. Vorrei riuscire a realizzare un applet che permette il movimenti di una palla su schermo, implementando il rimbalzo.
Non so da dove iniziare qualcuno può aiutarmi?
__________________
PACKARD BELL Easy Note TJ75@CPU INTEL Core i5 430M //GPU ATI RADEON HD 5470//RAM CORSAIR 4GB DDR3// HD WESTERN DIGITAL 640GB SATAII 3.0 GB/s |
![]() |
![]() |
![]() |
#2 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
I passaggi non sono molti.
Crei una classe che estende JComponent. In quella classe dichiari un metodo con questa firma: Codice:
@Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); Graphics2D g = (Graphics2D)graphics; } Quello che disegni con "g" appare nella regione della finestra assegnata al tuo componente. Il codice che segue, che può servirti come base, disegna una palla rossa in una finestra: Codice:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import javax.swing.JComponent; import javax.swing.JFrame; class Test { public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { Screen screen = new Screen(); screen.setPreferredSize(new Dimension(400, 400)); JFrame window = new JFrame("Ball test"); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); window.add(screen); window.pack(); window.setVisible(true); }}); } } class Screen extends JComponent { private Shape ball = new Ellipse2D.Float(0, 0, 100, 100); private Point2D ballLocation = new Point2D.Double(); private Paint ballFill = Color.RED; private Paint ballDraw = Color.BLACK; @Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); Graphics2D g = (Graphics2D)graphics; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.translate(ballLocation.getX(), ballLocation.getY()); g.setPaint(ballFill); g.fill(ball); g.setPaint(ballDraw); g.draw(ball); g.translate(-ballLocation.getX(), -ballLocation.getY()); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT); } }
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#3 |
Member
Iscritto dal: Sep 2008
Città: Padova
Messaggi: 172
|
Ciao e grazie per le tue risposte sempre precise e puntuali..
Ma per fare muovere la palla cosa devo implementare?
__________________
PACKARD BELL Easy Note TJ75@CPU INTEL Core i5 430M //GPU ATI RADEON HD 5470//RAM CORSAIR 4GB DDR3// HD WESTERN DIGITAL 640GB SATAII 3.0 GB/s |
![]() |
![]() |
![]() |
#4 |
Member
Iscritto dal: Sep 2008
Città: Padova
Messaggi: 172
|
Ho provato ad implementare un ciclo for:
Codice:
for (int i = 0; i <100; i++)
__________________
PACKARD BELL Easy Note TJ75@CPU INTEL Core i5 430M //GPU ATI RADEON HD 5470//RAM CORSAIR 4GB DDR3// HD WESTERN DIGITAL 640GB SATAII 3.0 GB/s |
![]() |
![]() |
![]() |
#5 |
Senior Member
Iscritto dal: Nov 2004
Città: Tra Verona e Mantova
Messaggi: 4553
|
L'idea del ciclo non è errata. In effetti un ciclo serve ma è "di animazione". Si tratta semplicemente di ripetere all'infinito una sequenza di codice che fa due cose:
1. computa il trascorrere del tempo a partire da un'istanza iniziale arbitrario 2. aggiorna lo stato dell'ambiente in base al trascorrere del tempo E' più facile a farsi che a dirsi. Il codice che segue è una mutazione del precedente in cui trovi questa animazione temporale. Codice:
import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Paint; import java.awt.RenderingHints; import java.awt.Shape; import java.awt.event.ActionListener; import java.awt.event.ActionEvent; import java.awt.geom.Ellipse2D; import java.awt.geom.Point2D; import javax.swing.JComponent; import javax.swing.JFrame; class Test { public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { Screen screen = new Screen(); screen.setPreferredSize(new Dimension(400, 400)); JFrame window = new JFrame("Ball test"); window.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); window.add(screen); window.pack(); window.setVisible(true); }}); } } class Animator { private javax.swing.Timer engine = new javax.swing.Timer(10, new ActionListener() { public void actionPerformed(ActionEvent e) { animate(); } }); private Screen targetScreen; private long lastTime; private double ballVelocity = 0.1f;//pixel per millisecond public Animator(Screen screen) { targetScreen = screen; } public void start() { lastTime = System.nanoTime(); engine.start(); } public void stop() { engine.stop(); } private Screen getScreen() { return targetScreen; } /** * Esegue l'animazione. */ private void animate() { //temporizzazione long now = System.nanoTime(); long dTimeMillis = (now - lastTime) / 1000000; lastTime = now; //calcola lo sposamento: v * dt double dy = ballVelocity * dTimeMillis; //applica lo spostamento alla palla Point2D buffer = new Point2D.Double(); getScreen().getBallLocation(buffer); buffer.setLocation(buffer.getX(), buffer.getY() + dy); getScreen().setBallLocation(buffer); } } class Screen extends JComponent { private Shape ball = new Ellipse2D.Float(0, 0, 100, 100); private Point2D ballLocation = new Point2D.Double(); private Paint ballFill = Color.RED; private Paint ballDraw = Color.BLACK; private Animator animator; /** * Invocato da Swing quando questo componente viene aggiunto ad un contenitore * visibile */ @Override public void addNotify() { super.addNotify(); animator = new Animator(this); animator.start(); } /** * Invocato da Swing quando questo componente viene rimosso da un contenitore * visibile o quel contenitore perde lo stato di proiettabilità. */ @Override public void removeNotify() { super.removeNotify(); animator.stop(); } public void getBallLocation(Point2D buffer) { buffer.setLocation(ballLocation.getX(), ballLocation.getY()); } public void setBallLocation(Point2D newLocation) { ballLocation.setLocation(newLocation.getX(), newLocation.getY()); repaint();//aggiorna lo schermo } @Override protected void paintComponent(Graphics graphics) { super.paintComponent(graphics); Graphics2D g = (Graphics2D)graphics; g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g.translate(ballLocation.getX(), ballLocation.getY()); g.setPaint(ballFill); g.fill(ball); g.setPaint(ballDraw); g.draw(ball); g.translate(-ballLocation.getX(), -ballLocation.getY()); g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_DEFAULT); } }
__________________
Uilliam Scecspir ti fa un baffo? Gioffri Cioser era uno straccione? E allora blogga anche tu, in inglese come me! |
![]() |
![]() |
![]() |
#6 |
Member
Iscritto dal: Apr 2010
Messaggi: 47
|
Mi sono interessato a questo applet ma ho provato ad inserirlo in una pagina html in questo modo:
Codice:
<html> <head> </head> <body> <div align = "center"> <h1>Anima pallina</h1> <APPLET code="pallina.class" width="550" height="550"></APPLET><br> </div> <body> </html> |
![]() |
![]() |
![]() |
#7 |
Senior Member
Iscritto dal: Oct 2006
Città: Roma
Messaggi: 1383
|
perché non é un applet, é un programma con metodo main; non devi metterlo in una pagina web, devi lanciarlo. ed inoltre é impossibile che il compilatore ti abbia generato un file pallina.class, non c'é nessuna classe di nome "pallina".
|
![]() |
![]() |
![]() |
Strumenti | |
|
|
Tutti gli orari sono GMT +1. Ora sono le: 21:44.