1 | package org.expeditee.items;
|
---|
2 |
|
---|
3 | import java.awt.Graphics;
|
---|
4 | import java.awt.Graphics2D;
|
---|
5 |
|
---|
6 | import javax.script.Invocable;
|
---|
7 | import javax.swing.JPanel;
|
---|
8 |
|
---|
9 | public class JSPanel extends JPanel {
|
---|
10 |
|
---|
11 | private static final long serialVersionUID = 1L;
|
---|
12 |
|
---|
13 | private final Invocable invocable;
|
---|
14 | private final String functionName;
|
---|
15 | private boolean autoPaint;
|
---|
16 |
|
---|
17 | public JSPanel(Invocable invocable, String functionName, boolean autoPaint) {
|
---|
18 | super();
|
---|
19 | this.invocable = invocable;
|
---|
20 | this.functionName = functionName;
|
---|
21 | this.autoPaint = autoPaint;
|
---|
22 | this.setIgnoreRepaint(true);
|
---|
23 | }
|
---|
24 |
|
---|
25 | public JSPanel(Invocable invocable, String functionName) {
|
---|
26 | this(invocable, functionName, true);
|
---|
27 | }
|
---|
28 |
|
---|
29 | private boolean paintNext = false;
|
---|
30 | @Override
|
---|
31 | public void paintComponent(Graphics g) {
|
---|
32 | if(!autoPaint && !paintNext)
|
---|
33 | return;
|
---|
34 | paintNext = false;
|
---|
35 | try {
|
---|
36 | invocable.invokeFunction(functionName, (Graphics2D) g);
|
---|
37 | } catch (Exception e) {
|
---|
38 | e.printStackTrace();
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Calls paintComponent()
|
---|
44 | * The only way to cause a repaint if autoPaint is false
|
---|
45 | */
|
---|
46 | public void doPaint() {
|
---|
47 | paintNext = true;
|
---|
48 | this.repaint();
|
---|
49 | }
|
---|
50 |
|
---|
51 | public void setAutoPaint(boolean autoPaint) {
|
---|
52 | this.autoPaint = autoPaint;
|
---|
53 | }
|
---|
54 |
|
---|
55 | public boolean getAutoPaint() {
|
---|
56 | return autoPaint;
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Make some padding around the drawable area so we don't fight with the item outline
|
---|
61 | */
|
---|
62 | @Override
|
---|
63 | public int getX() {
|
---|
64 | return super.getX() + 5;
|
---|
65 | }
|
---|
66 |
|
---|
67 | @Override
|
---|
68 | public int getY() {
|
---|
69 | return super.getY() + 5;
|
---|
70 | }
|
---|
71 |
|
---|
72 | @Override
|
---|
73 | public int getWidth() {
|
---|
74 | return super.getWidth() - 10;
|
---|
75 | }
|
---|
76 |
|
---|
77 | @Override
|
---|
78 | public int getHeight() {
|
---|
79 | return super.getHeight() - 10;
|
---|
80 | }
|
---|
81 | }
|
---|