-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrawpad.java
More file actions
113 lines (106 loc) · 3.82 KB
/
Copy pathdrawpad.java
File metadata and controls
113 lines (106 loc) · 3.82 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
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class drawpad extends JPanel implements ActionListener{
final int sim_height = 500;
final int sim_width = 500;
int k;
Timer timer;
Graphics2D g2d;
drawpad()
{
this.setPreferredSize(new Dimension(sim_width,sim_height));
timer = new Timer((int)(world.time_increment*1000),this);
System.out.println("started simulation");
timer.start();
k = 0;
}
public void paint(Graphics g)
{
super.paint(g);
g2d = (Graphics2D)g;
// g2d.drawOval(250,250,10,10);
// draw all the required shapes based on the input.
//draw lines
g2d.drawString("time : " + k/(float)100, 420, 20);
int x1 = 0,y1 = 0,x2 = 0,y2 = 0;
for(int i = 0; i < world.lines.size(); i++)
{
y1 = (int)(world.lines.get(i).l1.start.y);;
y2 = (int)(world.lines.get(i).l1.end.y);
x1 = (int)(world.lines.get(i).l1.start.x);
x2 = (int)(world.lines.get(i).l1.end.x);
/*
if(world.lines.get(i).l1.a != 0){
x1 = (int)(world.lines.get(i).l1.start.x);
x2 = (int)((world.lines.get(i).l1.b * y2 + world.lines.get(i).l1.c)/world.lines.get(i).l1.a);
}
else{
x1 = (int)(-1 * world.lines.get(i).l1.c / world.lines.get(i).l1.b);
x2 = x1;
}*/
try{
g2d.drawLine(x1, sim_height - y1, x2, sim_height - y2);
x1 = 0;
x2 = sim_width;
y1 = 0;
y2 = 0;
//g2d.drawLine(x1, sim_height - y1, x2, sim_height - y2);
}
catch(NullPointerException e)
{
System.out.println("error found in segment 1");
}
}
//draw circles
for(int i = 0; i < world.circles.size(); i++)
{
g2d.setPaint(Color.GREEN);
try{
g2d.fillOval(
(int)(world.circles.get(i).c.center.x - (int)world.circles.get(i).c.radius),
sim_height - (int)(world.circles.get(i).c.center.y + (int)world.circles.get(i).c.radius),
(int)world.circles.get(i).c.radius * 2,
(int)world.circles.get(i).c.radius * 2
);
}
catch(NullPointerException e)
{
System.out.println("error found in segment 2");
}
g2d.setPaint(Color.BLACK);
}
//draw spheres
for(int j = 0; j < world.balls.size(); j++){
try{
g2d.drawOval(
(int)(world.postions.get(k)[j].x - world.balls.get(j).c.radius),
sim_height - (int)(world.postions.get(k)[j].y + (int)world.balls.get(j).c.radius),
(int)world.balls.get(j).c.radius * 2,
(int)world.balls.get(j).c.radius * 2
);
g2d.drawString("sphere " + j + " : " + (world.postions.get(k)[j].x) + " , " + (world.postions.get(k)[j].y),0,20*(j + 1));
}
catch(NullPointerException e)
{
System.out.println(" NP error found in segment 3\n i = " + k + "\nj = " + j);
}
catch(IndexOutOfBoundsException e)
{
System.out.println("too many frames rendered");
}
}
}
@Override
public void actionPerformed(ActionEvent e)
{
if(k < world.postions.size() - 2)
{
k ++;
}
else{
k = 0;
}
repaint();
}
}