-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimulation.java
More file actions
131 lines (88 loc) · 2.41 KB
/
Simulation.java
File metadata and controls
131 lines (88 loc) · 2.41 KB
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
import javax.swing.JPanel;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Simulation extends JPanel implements KeyListener, MouseListener, MouseMotionListener {
private static int WIDTH;
private static int HEIGHT;
private static BufferedImage simulationImage;
private static int[] simulationRaster;
private Fabric fabric;
private Render ren;
public Simulation(int width, int height) {
addKeyListener(this);
addMouseListener(this);
addMouseMotionListener(this);
WIDTH = width;
HEIGHT = height;
simulationImage = new BufferedImage(WIDTH, HEIGHT,BufferedImage.TYPE_INT_ARGB);
simulationRaster = ((DataBufferInt) simulationImage.getRaster().getDataBuffer()).getData();
ren = new Render(WIDTH, HEIGHT, simulationRaster);
init();
}
public void init(){
int vertexCountX = 172/2;
int vertexCountY = 80/2;
int linkLength = 10*2;
int linkStrength = 6/2;
int xPos = 100;
int yPos = 100;
fabric = new Fabric( xPos, yPos, vertexCountX, vertexCountY, linkLength, linkStrength, ren);
}
public void tick() {
fabric.tick();
}
public void paint(Graphics g) {
ren.clear();
fabric.draw( ren );
g.drawImage(simulationImage, 0, 0, WIDTH, HEIGHT, null);
}
public int oldX, oldY ;
public void mouseDragged(MouseEvent me) {
fabric.drag( me.getX(), me.getY(), oldX, oldY );
oldX = me.getX();
oldY = me.getY();
}
public void mouseMoved(MouseEvent me) {
oldX = me.getX();
oldY = me.getY();
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
}
public void mouseEntered(MouseEvent e) {
requestFocus();
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyTyped(KeyEvent e) {
if( e.getKeyChar() == 'r' ){
init();
}
}
}