-
Notifications
You must be signed in to change notification settings - Fork 0
/
Box2D_Example4.java
180 lines (134 loc) · 3.79 KB
/
Box2D_Example4.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.geom.AffineTransform;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.*;
import net.phys2d.math.Vector2f;
import net.phys2d.raw.*;
import net.phys2d.raw.shapes.Box;
import net.phys2d.raw.strategies.QuadSpaceStrategy;
public class Box2D_Example4 {
public static void main(String s[])
{
JFrame frame = new JFrame("Box2d Example 4 - Bier.png");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JPanel panel = new Ex4Panel();
frame.getContentPane().add(panel);
frame.pack();
frame.setVisible(true);
}
}
class Ex4Panel extends JPanel implements ActionListener, MouseListener
{
/**
*
*/
private static final long serialVersionUID = 1L;
// Add World object
private World myWorld = new World(
new Vector2f(0.0f, 10.0f),
50,
new QuadSpaceStrategy(20,5));
private ArrayList<Body> bodies = new ArrayList<Body>();
private BufferedImage bier;
/* Constructor */
public Ex4Panel()
{
setPreferredSize( new Dimension(640,480));
try {
bier = ImageIO.read(new File("src/bier.png"));
} catch(IOException e) {
System.out.println(e);
}
myWorld.clear();
myWorld.setGravity(0, 10);
// Create Static body
Body b = new StaticBody("Static", new Box(450,5));
b.setPosition(300f,300f);
b.setRotation((float) (Math.PI/60f));
bodies.add(b);
for(int idx = 0; idx < 4; idx++)
{
b = new Body("b"+idx, new Box(100f,197f), 10f);
b.setPosition(210f + idx*130f, 100f);
b.setFriction(0.01f);
bodies.add(b);
}
for(Body body:bodies)
myWorld.add(body);
Timer timer = new Timer(1000/30, this);
timer.start();
addMouseListener(this);
}
// Timer, action performed.
public void actionPerformed(ActionEvent arg0)
{
for(int i=0; i<5; i++) {
myWorld.step();
}
repaint();
}
public void drawBox(Graphics2D g2, Body b)
{
Box box = (Box) b.getShape();
Vector2f[] pts = box.getPoints(b.getPosition(), b.getRotation());
Vector2f p1 = pts[0];
Vector2f p2 = pts[1];
Vector2f p3 = pts[2];
Vector2f p4 = pts[3];
BufferedImage img = bier;
AffineTransform tr = new AffineTransform();
tr.rotate(b.getRotation(), img.getWidth()/2, img.getHeight()/2);
AffineTransformOp op = new AffineTransformOp(tr, AffineTransformOp.TYPE_BILINEAR);
img = op.filter(img, null);
g2.drawImage( img, (int)p1.x, (int)p1.y, 100, 197, null);
// g2.setColor(Color.BLACK);
// g2.drawLine((int) p1.x,(int) p1.y,(int) p2.x,(int) p2.y);
// g2.drawLine((int) p2.x,(int) p2.y,(int) p3.x,(int) p3.y);
// g2.drawLine((int) p3.x,(int) p3.y,(int) p4.x,(int) p4.y);
// g2.drawLine((int) p4.x,(int) p4.y,(int) p1.x,(int) p1.y);
}
// Hier alleen tekenen ! Nooit een repaint() !!
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D)g;
// Dikke lijn, beter zichtbaar
g2.setStroke(new BasicStroke(2));
// Draw body 1 en body 2
for(Body body:bodies)
{
drawBox(g2,body);
}
}
@Override
public void mouseClicked(MouseEvent arg0) {
bodies.get(1).setForce(5000f, 0f);
System.out.println(bodies.get(1));
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}