summaryrefslogtreecommitdiff
path: root/trunk/users/adrian/host/src/org/reprap/gui/botConsole/bedPanel.java
blob: 2264730100b77c9bf9b1a63d4d7965b0d4dd5ef7 (plain)
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
/*
 * 
 * !!!!!
 * NOTE: PLEASE ONLY EDIT THIS USING THE NETBEANS IDE 6.0.1 OR HIGHER!!!!
 * !!!!!
 * 
 * ... an .xml file is associated with this class. Cheers.
 *
 * bedPanel.java
 *
 * Created on 30 March 2008, 18:55
 */

package org.reprap.gui.botConsole;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.font.FontRenderContext;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;

/**
 *
 * @author  reprap
 */
public class bedPanel extends javax.swing.JPanel {
	private static final long serialVersionUID = 1L;
	private final int CROSS_HAIR_SIZE = 10;
    private Line2D.Double a1, a2, b1, b2;
    private Point2D.Double aDatum, bDatum;
    private int x;
    private int y;
    private Font font;
    private int fontSize;
    
    /** Creates new form bedPanel */
    public bedPanel() {
        
        // First cross hair
        aDatum = new Point2D.Double();
        a1 = new Line2D.Double();
        a2 = new Line2D.Double();
        
        // History cross hair
        bDatum = new Point2D.Double();
        b1 = new Line2D.Double();
        b2 = new Line2D.Double();
        
        initComponents();
    
    }
    
    public void setDimensions() {
        x = this.getWidth();
        y = this.getHeight();

        // Text imitialisation
        fontSize = (int)(y/20);
        font = new Font("dialog", Font.PLAIN, fontSize);
        repaint();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        setBackground(java.awt.Color.white);
        setMaximumSize(new java.awt.Dimension(200, 200));
        setMinimumSize(new java.awt.Dimension(200, 200));
        setPreferredSize(new java.awt.Dimension(200, 200));

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 200, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 200, Short.MAX_VALUE)
        );
    }// </editor-fold>//GEN-END:initComponents
    
    public void mousePressed() {
        unClicked = false;
    }
    
    public void updateCrossHair(int posX, int posY) {
        
        c = Color.black;
        
        aDatum.setLocation(posX, posY);
        a1.setLine(aDatum.getX(), aDatum.getY()-CROSS_HAIR_SIZE, aDatum.getX(), aDatum.getY()+CROSS_HAIR_SIZE);
        a2.setLine(aDatum.getX()-CROSS_HAIR_SIZE, aDatum.getY(), aDatum.getX()+CROSS_HAIR_SIZE, aDatum.getY());
        
        updateOldPosition(posX, posY);
        
        repaint();
    }
    
    public void updateOldPosition(int posX, int posY) {
        
        bDatum.setLocation(posX, posY);
        b1.setLine(bDatum.getX(), bDatum.getY()-CROSS_HAIR_SIZE/2, bDatum.getX(), bDatum.getY()+CROSS_HAIR_SIZE/2);
        b2.setLine(bDatum.getX()-CROSS_HAIR_SIZE/2, bDatum.getY(), bDatum.getX()+CROSS_HAIR_SIZE/2, bDatum.getY());
    }
    
    public void dragCrossHair(int posX, int posY) {
        
        c = Color.red;
        
        aDatum.setLocation(posX, posY);
        a1.setLine(aDatum.getX(), aDatum.getY()-x, aDatum.getX(), aDatum.getY()+x);
        a2.setLine(aDatum.getX()-x, aDatum.getY(), aDatum.getX()+x, aDatum.getY());
        
        repaint();
    }
    
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
                        RenderingHints.KEY_ANTIALIASING,
        RenderingHints.VALUE_ANTIALIAS_ON);
        g2.setRenderingHint(
                        RenderingHints.KEY_COLOR_RENDERING,
        RenderingHints.VALUE_COLOR_RENDER_SPEED);
        g2.setRenderingHint(
                        RenderingHints.KEY_RENDERING,
        RenderingHints.VALUE_RENDER_SPEED);
        g2.setColor(c);
        g2.draw(a1);		
        g2.draw(a2);

        g2.setColor(Color.gray);
        g2.draw(b1);		
        g2.draw(b2);

        if (unClicked) {
            g2.setFont(font);
            FontRenderContext frc = g2.getFontRenderContext();

            float width;
            float sx;
            float sy;
            float lineHeight = font.getSize();
            float space = font.getSize()/4;
            int lines = s.length;

            for (int i = 0; i < lines; i++) {
                  width = (float)font.getStringBounds(s[i], frc).getWidth();
                  sx = (x - width)/2;
                  sy = y/2+(lineHeight)/2 - (lines-1)*((lineHeight+space)/2) + (i*(lineHeight+space));
                  g2.drawString(s[i], sx, sy);
            }
        }
    }
   
    private Color c;
    private boolean unClicked = true;
    private String s1 = "Click to load new coordinates";
    private String s2 = "Drag for cross-hairs";
    private String s3 = ""; //"Home X & Y axes first";
    private String[] s = new String[] { s3, s1, s2 };
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}