Working with JFrame  
Author Message
Rookie





PostPosted: 2003-9-12 7:50:00 Top

java-programmer, Working with JFrame I Have The Following code to measure how far the mouse has move I can get it
to work for JApplet But not for JFrame how can I get it to work for JFrame?

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class Odometer extends JApplet implements MouseMotionListener,
ActionListener

{

private int td=0;

private int last_x=0;

private int last_y=0;

private Graphics g;

private JPanel extra=new JPanel();

private JButton btn=new JButton("Reset");

private JLabel lbl=new JLabel("");

private JTextField txt=new JTextField(10);

private Container c = getContentPane();

public void init()

{

btn.addActionListener(this);

extra.add(btn);

extra.add(txt);

extra.add(lbl);

c.add(extra);

this.addMouseMotionListener(this);

}

public void mouseMoved(MouseEvent e)

{

double ld=0;

String str;

int x;

int y;

x=e.getX();

y=e.getY();

ld=Math.sqrt((Math.pow(x-last_x,2))+(Math.pow(y-last_y,2)));

str=Double.toString(ld);

td=td+Double.valueOf(str).intValue();

txt.setText(Integer.toString(td));

last_x=x;

last_y=y;

}

public void actionPerformed(ActionEvent e)

{

if(e.getSource()==btn)

{

td=0;

txt.setText(Integer.toString(td));

}

}

public void mouseDragged(MouseEvent e){}

}


 
058147m





PostPosted: 2003-9-13 4:54:00 Top

java-programmer >> Working with JFrame "Rookie" <email***@***.com> wrote in message news:<email***@***.com>...
> I Have The Following code to measure how far the mouse has move I can get it
> to work for JApplet But not for JFrame how can I get it to work for JFrame?
>
> import java.applet.*;
>
> import java.awt.*;
>
> import java.awt.event.*;
>
> import javax.swing.*;
>
> public class Odometer extends JApplet implements MouseMotionListener,
> ActionListener
>
> {
>
> private int td=0;
>
> private int last_x=0;
>
> private int last_y=0;
>
> private Graphics g;
>
> private JPanel extra=new JPanel();
>
> private JButton btn=new JButton("Reset");
>
> private JLabel lbl=new JLabel("");
>
> private JTextField txt=new JTextField(10);
>
> private Container c = getContentPane();
>
> public void init()
>
> {
>
> btn.addActionListener(this);
>
> extra.add(btn);
>
> extra.add(txt);
>
> extra.add(lbl);
>
> c.add(extra);
>
> this.addMouseMotionListener(this);
>
> }
>
> public void mouseMoved(MouseEvent e)
>
> {
>
> double ld=0;
>
> String str;
>
> int x;
>
> int y;
>
> x=e.getX();
>
> y=e.getY();
>
> ld=Math.sqrt((Math.pow(x-last_x,2))+(Math.pow(y-last_y,2)));
>
> str=Double.toString(ld);
>
> td=td+Double.valueOf(str).intValue();
>
> txt.setText(Integer.toString(td));
>
> last_x=x;
>
> last_y=y;
>
> }
>
> public void actionPerformed(ActionEvent e)
>
> {
>
> if(e.getSource()==btn)
>
> {
>
> td=0;
>
> txt.setText(Integer.toString(td));
>
> }
>
> }
>
> public void mouseDragged(MouseEvent e){}
>
> }

since i dont really want to design a whole gui, The basic way to do
it, is to design your gui using the awt framework and then create a
mouse listener like you have in the applet, and have it set up to do
what ever you need to do in the inner classs what you create to work
with these listeners. basically its alot of trial and error, but that
is the whole process of Object Orientated Analysis and Design. Start
small and build up. The gui's and the JApplets or Applets use the same
listener classes, so it is just the set up that you need to be
concerned with.