-
Notifications
You must be signed in to change notification settings - Fork 0
/
Desenho2.java
92 lines (79 loc) · 2.72 KB
/
Desenho2.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package bolaBaskete;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
*
* @author Sara Tuma
*/
public class Desenho2 extends JPanel implements Runnable{
Thread t = new Thread (this);
public Bola bola = new Bola();
public int y=1;
public int bateu=0;
private int angulo=0;
private double aceleracao=1.1;
private boolean terminar=true;
public Desenho2(){
t.start();
JFrame f = new JFrame();
f.setSize(600,600);
f.setTitle("Bola de baskete batendo no chão");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setBackground(Color.white);
// f.setBackground(Color.black);
f.setResizable(false);
f.setLocationRelativeTo(null);
this.setSize(600,600);
f.add(this);
f.setVisible(true);
}
@Override
public void paintComponent (Graphics g){
super.paintComponent(g);
Graphics2D g2 =(Graphics2D) g;
// g2.rotate(Math.toRadians(angulo*-1),bola.getPosx()+(bola.getLargura()/2), bola.getPosy()+(bola.getAltura()/2));
AffineTransform obj = g2.getTransform();
g2.translate(0, y);
g2.drawImage(bola.getImg(),bola.getPosx(),bola.getPosy(), bola.getLargura(), bola.getAltura(), this);
g2.setTransform(obj);
//obj= g2.getTransform();
}
private void dormir(){
try {
t.sleep(15);
} catch (InterruptedException ex) {
Logger.getLogger(Desenho2.class.getName()).log(Level.SEVERE, null, ex);
}
}
private void actualizar(){
// this.angulo = (int) (this.angulo >= 360 ? 0 : 1 + this.angulo);
if(bola.getPosy()+bola.getAltura()>570)
{
System.out.println( "BATEU NO CHÃO " );
y=y*-1; bateu++;
}
bola.setDy(++y);
bola.setPosy(bola.getPosy()+bola.getDy());
}
@Override
public void run(){
while(terminar){
//Limitei a quantidade de batidas (ao chão!!)
if(bateu==40) break;
actualizar(); //Actualizando a nova posicao da bola e suas alterações
dormir(); //Pausar a execução da thread
repaint(); //Chamada do metodo paintComponet()
}
}
}