Ik heb een applet dat coorinaten plot op een jpanel.(scatterplot)
Probleem:
Als ik de applet start dan zie alleen mijn jpanel zonder spots. Als ik de applet restart
dan worden de punten/assen wel geplot in de jpanel, trek ik de window groter dan gaan de punten/assen naar de achtergrond(contenpane?) en blijft de jpanel op de voorgrond.
Wat doe ik niet goed, ik plot de spots wel op de jpanel maar als ik de applet start zie ik ze niet, als ik de applet restart zie ik ze wel.. maar maar ik de window groter dan worden ze op de contentpane gezet en is de jpanel weer leeg.
Ik ben er al een paar dagen mee bezig maar kom er niet uit, help will be much appreciated
Hier is de code:
Probleem:
Als ik de applet start dan zie alleen mijn jpanel zonder spots. Als ik de applet restart
dan worden de punten/assen wel geplot in de jpanel, trek ik de window groter dan gaan de punten/assen naar de achtergrond(contenpane?) en blijft de jpanel op de voorgrond.
Wat doe ik niet goed, ik plot de spots wel op de jpanel maar als ik de applet start zie ik ze niet, als ik de applet restart zie ik ze wel.. maar maar ik de window groter dan worden ze op de contentpane gezet en is de jpanel weer leeg.
Ik ben er al een paar dagen mee bezig maar kom er niet uit, help will be much appreciated
Hier is de code:
code:
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
| //=-=-=-=-=-=-=-=-=-=-=-=-=-
import javax.swing.*;
import java.awt.event.*;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.geom.*;
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.BorderLayout;
import java.util.Iterator;
import java.util.ListIterator;
import java.util.Vector;
import org.gjt.mm.mysql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.sql.*;
import java.util.List;
public class jpanelPlot extends JApplet implements MouseListener, MouseMotionListener{
double scaleX, scaleY, originX, originY;
private List coordsTest, description;
private double X, Y ;
JTextField txt = new JTextField(10);
JPanel panel = new JPanel();
JLabel statusBar = new JLabel("Coords..");
public void init() {
Container cp = getContentPane();
cp.setLayout(new BorderLayout());
cp.setBackground(Color.yellow);
panel.setBackground(Color.white);
panel.addMouseListener(this);
panel.addMouseMotionListener(this);
cp.add(panel,BorderLayout.CENTER);
cp.add(statusBar,BorderLayout.SOUTH);
show();
}
public void start() {
coordsTest = new ArrayList();
// Test coordinaten
coordsTest.add(new Point2D.Double(10,0.3));
coordsTest.add(new Point2D.Double(12,0.3));
coordsTest.add(new Point2D.Double(9,-0.2));
coordsTest.add(new Point2D.Double(8,1));
List XValues = new ArrayList();
List YValues = new ArrayList();
for (Iterator i = coordsTest.iterator(); i.hasNext();) {
Point2D.Double pt = (Point2D.Double) i.next();
XValues.add(new Double(pt.x));
YValues.add(new Double(pt.y));
}
final double MAXX = ((Double)Collections.max(XValues)).doubleValue();
final double MINX = ((Double)Collections.min(XValues)).doubleValue();
final double MAXY = ((Double)Collections.max(YValues)).doubleValue();
final double MINY = ((Double)Collections.min(YValues)).doubleValue();
final double rangeX = 20; // range of x values
final double rangeY = 3; // range of y values
// draw panel
panel = new JPanel() {
// Plot points on the panel
public void paintComponent(Graphics g) {
//super.paintComponent(g);
int width = getWidth(); // get width and height of the canvas
int height = getHeight();
scaleX = width/rangeX; // number of pixels per unit x
scaleY = height/rangeY; // number of pixels per unit y
originX=0;
originY=height/2;
System.out.println("coordinaten: "+ coordsTest.get(2));
// plot the points on the Panel
for (Iterator i = coordsTest.iterator(); i.hasNext();) {
Point2D.Double pt = (Point2D.Double)i.next();
System.out.println("Pt: " + pt);
g.setColor(Color.red);
g.fillOval((int)(pt.x*scaleX + originX) , (int)(-pt.y*scaleY + originY) ,5,5);
}
// Draw the axes below.
g.setColor(Color.black);
g.drawLine(0, (int)originY, width, (int)originY); // x-axis
g.drawLine((int)originX, 0, (int)originX, height); // y-axis
// Draw the axes above.
// Label the axes below.
int numLabelsX = 1; // Number of X labels.
int numLabelsY = 1; // Number of Y labels.
// Declare new Font
Font font = new Font("SanSerif", Font.BOLD, 11);
// Set current font
g.setFont(font);
// Get information about the font
FontMetrics fm = getFontMetrics(font);
double ySpacing = height/(numLabelsY);
for( int y = 0; y <= height; y += ySpacing) {
g.drawString(Integer.toString((int)((y-originY)/-scaleY)),
(int)originX, y==0 ? y+fm.getAscent() : y); // so that the top label won't be missed off
}
double xSpacing = width/(numLabelsX);
for(int x = 0; x <= width; x += xSpacing) {
String xLabel = Integer.toString((int)((x-originX)/scaleX));
int lblWidth = fm.stringWidth(xLabel);
// make sure right-most label is not missed off
g.drawString(xLabel, x+lblWidth>width ? width-lblWidth : x, (int)originY);
}
// Label the axes above.
}
}; // draw panel
} // end run()
public void mouseClicked(MouseEvent event) {
double expix= 3/scaleX; // set click boundry
double expiy= 3/scaleY;
double xPos = event.getX(); // coords of the event retative to the source
double yPos = event.getY(); // coords of the event relative to the source
double xPlot = (double)((xPos-originX)/scaleX); // coords translated to plot dimentions
double yPlot = (double)((yPos-originY)/-scaleY); // coords translated to plot dimentions
for (ListIterator i = coordsTest.listIterator(); i.hasNext(); ) {
Point2D.Double pt = (Point2D.Double)i.next();
if((Math.abs((double)pt.x - xPlot)) <= expix && (Math.abs((double)pt.x - xPlot)) >= 0 && (Math.abs((double)pt.y - yPlot)) <= expiy && (Math.abs((double)pt.y - yPlot)) >=0) {
xPlot = (double)pt.x;
yPlot = (double)pt.y;
//statusBar.setText("A,M: [" + xPlot + ", " + yPlot + " ]" + " rp-value: " + description.get((i.nextIndex())-1));
// get rpvalue
// String rpvalue = "" + description.get((i.nextIndex())-1);
// displayURL("http://www.ensembl.org/Mus_musculus/contigview?mapfrag=" + rpvalue);
break;
}
else {
statusBar.setText("no point [" + xPlot + ", " + yPlot + "]");
}
}
}
public void mouseDragged(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseMoved(MouseEvent e) {
}
public void mousePressed(MouseEvent e) {
}
public void mouseReleased(MouseEvent e) {
}
} // end jpanelPlot |