Results 1 to 5 of 5
Thread: Interactive Panel
- 10-04-2009, 05:19 PM #1
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Interactive Panel
Dear Members,
I am sorry to used that Thread , and i attached my code as a zip file with the reply , anyway I am so sorry for all that , well here is my code I am facing problem in mouse events.
Java Code:class GraphicComponent extends JPanel { // make points of view data JLabel xViewLabel; JLabel yViewLabel; JLabel xModelLabel; JLabel yModelLabel; double [] data = {1.0 , 5.8, 7.4 , 3.2}; AffineTransform xform; AffineTransform inverted; final int PAD = 20; public GraphicComponent () { } public void setPoint(Point p, int x, int y) { p.setLocation(x,y); repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if(xform == null) setTransform(); double w = getWidth(); double h = getHeight(); // draw axes g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD)); // top - to bottom g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD)); // left to write // draw data g2.setPaint(Color.red); for(int i = 0; i < data.length ; i++) { Point2D.Double p = modelToView(i, data[i]); g2.fill(new Ellipse2D.Double(p.x-1.5, p.y-1.5, 4, 4)); } } private Point2D.Double modelToView(double x, double y) { Point2D.Double dst = new Point2D.Double(); xform.transform(new Point2D.Double(x, y), dst); return dst; } private Point2D.Double viewToModel(Point p) { Point2D.Double dst = new Point2D.Double(); inverted.transform(p, dst); return dst; } private void setTransform() { xform = new AffineTransform(); double w = getWidth(); double h = getHeight(); double dataMax = getDataMax(); double max = Math.ceil(dataMax/10)*10; System.out.printf("dataMax = %f max = %f%n", dataMax, max); double yScale = (h - 2*PAD)/max; double xScale = (w - 2*PAD)/(data.length-1); xform.translate(PAD, h-PAD); // PAD = x-axis , h-PAD = y-axis xform.scale(xScale, -yScale); // xScale = x-axis , -yscale = y-axis setInverted(); } private void setInverted() { inverted = (AffineTransform)xform.clone(); if(inverted.getDeterminant() != 0) { try { inverted.invert(); } catch(NoninvertibleTransformException e) { System.out.println("invert error: " + e.getMessage()); } } else { inverted.setToIdentity(); } } private double getDataMax() { double [] copy = Arrays.copyOf(data, data.length); Arrays.sort(copy); return copy[data.length-1]; } private JPanel getOutputPanels() { Dimension d = new Dimension(65, 25); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; panel.add(getViewPanel(d), gbc); panel.add(getModelPanel(d), gbc); return panel; } private JPanel getViewPanel(Dimension d) { xViewLabel = getLabel(d); yViewLabel = getLabel(d); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); JLabel label = getLabel(d); label.setText("View"); panel.add(label, gbc); gbc.weightx = 1.0; addComponents("x", xViewLabel, panel, gbc, true); addComponents("y", yViewLabel, panel, gbc, false); return panel; } private JPanel getModelPanel(Dimension d) { xModelLabel = getLabel(d); yModelLabel = getLabel(d); JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); JLabel label = getLabel(d); label.setText("Model"); panel.add(label, gbc); gbc.weightx = 1.0; addComponents("x", xModelLabel, panel, gbc, true); addComponents("y", yModelLabel, panel, gbc, false); return panel; } private JLabel getLabel(Dimension d) { JLabel label = new JLabel(); label.setPreferredSize(d); label.setBorder(BorderFactory.createEtchedBorder()); return label; } private void addComponents(String s, JLabel label, Container c, GridBagConstraints gbc, boolean b) { gbc.weightx = b ? 1.0 : 0; gbc.anchor = GridBagConstraints.EAST; c.add(new JLabel(s), gbc); gbc.weightx = b ? 0 : 1.0; gbc.anchor = GridBagConstraints.WEST; c.add(label, gbc); } public static void main(String[] args) { GraphicComponent gc = new GraphicComponent(); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(gc); f.add(gc.getOutputPanels(), "Last"); f.setSize(400,400); f.setLocation(100,100); f.setVisible(true); //make and show gui DragClass dc = new DragClass(gc); gc.addMouseListener(dc); gc.addMouseMotionListener(dc); } }Thanks youJava Code:class DragClass extends MouseAdapter { GraphicComponent component; Point selPoint; double [] data ; boolean okayToDrag = false; Point offset = new Point(); int closeEnough; private int dragIndex = NOT_DRAGGING; private final static int NOT_DRAGGING = -1; private int x; private int y; private int NEIGHBORHOOD = 4; public DragClass(GraphicComponent gc) { component = gc; this.data = component.data; } @Override public void mousePressed(MouseEvent e) { dragIndex = NOT_DRAGGING; int minDistance = Integer.MAX_VALUE; Point p = e.getPoint(); closeEnough = -1; for ( int i = 0; i < data.length ; i++) { selPoint.x = i; selPoint.y = (int) data[i]; offset.x = p.x - selPoint.x; offset.y = p.y - selPoint.y; int distance = (int) (Math.sqrt( offset.x* offset.x + offset.y + offset.y)); if (distance < minDistance) { minDistance = distance; closeEnough = i; } break; } // end of for loop if (minDistance > NEIGHBORHOOD) return; dragIndex = closeEnough; } // end of pressed event @Override public void mouseReleased(MouseEvent e) { if (dragIndex == NOT_DRAGGING) return; dragIndex = NOT_DRAGGING; } @Override public void mouseDragged(MouseEvent e) { if (dragIndex == NOT_DRAGGING) return; x = e.getX() - offset.x; y = e.getY() - offset.y; component.setPoint(selPoint, x, y); } }
- 10-04-2009, 08:48 PM #2
> I am facing problem in mouse events.
Patient: Doctor, I am facing problem in my, er, um, mouse events
Doctor: here you go. 2 tablets 3 times a day and you'll be right as rain.
Not a likely scenario, huh? The doctor would need more information, or the patient could get the wrong medicine and end up with no mouse events at all, or too many.
How To Ask Questions The Smart Way
db
-
I think that you refer to this: http://www.java-forums.org/showthread.php?t=19927
No biggie.
???and i attached my code as a zip file with the reply ,
You may wish to give us more information here as "problem in mouse events" tells very little.anyway I am so sorry for all that , well here is my code I am facing problem in mouse events.
Much luck.
- 10-06-2009, 05:10 AM #4
Member
- Join Date
- Oct 2009
- Posts
- 3
- Rep Power
- 0
Interactive Panel
Hi Again,
<CODE>
public void mousePressed(MouseEvent e) {
dragIndex = NOT_DRAGGING;
int minDistance = Integer.MAX_VALUE;
Point p = e.getPoint();
closeEnough = -1;
for ( int i = 0; i < data.length ; i++)
{
selPoint.x = i;
selPoint.y = (int) data[i];
offset.x = p.x - selPoint.x;
offset.y = p.y - selPoint.y;
int distance = (int) (Math.sqrt( offset.x* offset.x + offset.y + offset.y));
if (distance < minDistance) {
minDistance = distance;
closeEnough = i;
}
</CODE>
I am getting NullPointer exception in upper mentioned code in Dark Red color
Data is double array holding actual data model values.
Appreciated if you answer me..
Thanks
- 10-06-2009, 09:16 AM #5
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
Similar Threads
-
interactive scattering xy plots using java
By homi in forum Java 2DReplies: 10Last Post: 10-03-2009, 02:07 PM -
Interactive TouchScreen - Java Programmers wanted
By indigovision in forum Jobs OfferedReplies: 0Last Post: 02-26-2009, 04:23 PM -
Interactive vector map in Java
By nenadm in forum AWT / SwingReplies: 1Last Post: 02-17-2009, 08:52 PM -
Java Interactive Profiler 1.1
By JavaBean in forum Java SoftwareReplies: 0Last Post: 06-24-2007, 08:47 AM -
Java Interactive Profiler 1.1 rc 1
By levent in forum Java SoftwareReplies: 0Last Post: 06-04-2007, 08:04 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks