-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPainter.java
More file actions
89 lines (76 loc) · 1.88 KB
/
Painter.java
File metadata and controls
89 lines (76 loc) · 1.88 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
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GraphicsEnvironment;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.util.List;
import javax.swing.JFrame;
public class Painter extends JFrame
{
private List<Sphere> sphereList;
private List<Spring> springList;
private double x, y;
private PaintCanvas pc;
public Painter(List<Sphere> sphereList, List<Spring> springList, int x, int y)
{
this.sphereList = sphereList;
this.springList = springList;
this.x = x;
this.y = y;
pc = new PaintCanvas();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
add(pc);
pack();
setVisible(true);
pc.init();
}
public void paintScene()
{
pc.paint();
}
private class PaintCanvas extends Canvas
{
private BufferStrategy buffer;
private BufferedImage bi;
private Graphics2D g2d;
private Graphics g;
public PaintCanvas()
{
this.setPreferredSize(new Dimension((int) x, (int) y));
}
public void init()
{
createBufferStrategy(2);
buffer = getBufferStrategy();
bi = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDefaultConfiguration().createCompatibleImage((int) x, (int) y);
}
public void paint()
{
//clear back buffer
g2d = bi.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, getWidth(), getHeight());
//paint HERE on g2d
for(Sphere s : sphereList)
{
g2d.setColor(Color.BLACK);
s.paint(g2d);
}
for(Spring sp : springList)
{
g2d.setColor(new Color(Math.min((int) Math.rint(Math.abs(((sp.oldlen - sp.stablen) / sp.stablen) * 4000)), 255), 0, 0));
sp.paint(g2d);
}
//blit image and flip
g = buffer.getDrawGraphics();
g.drawImage(bi, 0, 0, null);
if(!buffer.contentsLost())//delete this if?
{
buffer.show();
}
}
}
}