Link to home
Start Free TrialLog in
Avatar of Guster
Guster

asked on

Clickable image form in Java

Hi,

I was wondering would anyone be able to tell me how I would go about creating a clickable image form in Java?? I'm working on a program that displays different types of wave modulation. For example, if you click on the frequency button, a sine wave will appear in a display canvas. What I want to do is to have the display canvas split into an equal number of segments that have a "0" value or a "1" value(say the default would be "0") so that when a segment is clicked that part of the sine wave will change(if for a frequency wave if a segment is clicked from "0" to "1", the frequency of the wave at that segment will increase). I'm running this program in a Java applet and it can display three types of waves frequency, amplitude and phase shift.
I'm fairly new at working with Java and would appreciate any help.

Regards,
Guster
Avatar of yongsing
yongsing

What is this segment and "0" and "1" values? Anyway, just add a MouseListener to your canvas, and do whatever you want inside the mouseClicked() method.
Guster,
Are you saying that there is only one sine wave from beginning to end of the image or is there more than one wave appearing?  If there is more than one sine wave then do each of these waves have an option to be either labeled with a zero or a one?  

Delphi3
yourCanvas.addMouseListener(new MouseAdapter() {
  public void mousePressed(MouseEvent me) {
   System.out.println("Pressed at : " + me.getPoint());
  }
  public void mouseReleased(MouseEvent me) {
   System.out.println("Released at : " + me.getPoint());
  }
  public void mouseClicked(MouseEvent me) {
   System.out.println("Clicked at : " + me.getPoint());
  }
});
... and use import java.awt.event.*;
Avatar of Guster

ASKER

Avatar of Guster

ASKER

Avatar of Guster

ASKER

Avatar of Guster

ASKER

Avatar of Guster

ASKER

Greetings all, we are going through a transition, thus the intermittent downtime and some problems posting comments; please try again.  There is more about this on the What's New link to the left.
Thanks for your patience and understanding.
Moondancer - EE Moderator
Avatar of Guster

ASKER

Hi again,

One type of wave is displayed at a time. Say you open the applet, it will be blank and then you click the amplitude wave button, then the amplitude wave will appear, and as a default I want all the segments to be defaulted to the "0" binary value (Im trying to display the wave with the binary values "0" and "1", so if one segment of the wave is clicked to the value "1" then the amplitude of that segment will change to a larger amplitude, if it is clicked again it will go back to "0" and the amplitude will change back to the smaller amplitude, for the frequency wave a segment with the binary value "0" might have only 2 revolutions whereas if a segment has the "1" binary value the number of revolutions will be larger etc.)
Then if you want to display another wave, you hit the refresh button and then click on say the frequency wave button to display the frequency wave.

Below is the code I have so far:

import java.awt. * ;
import java.awt.event. * ;

public class DoodleSineWave extends java.applet.Applet implements ActionListener {
       int x0, xN, y0, yN;
       private int APPLET_WIDTH = 600;
       private int APPLET_HEIGHT = 450;
                         
       private Label titleLabel;
       private DoodleSineCanvas myCanvas;
       private Button snglButton;
       private Button freqButton;
       private Button ampButton;
       private Button phaseButton;
       private Button refreshButton;
             
       public void init() {
       // How big is the applet?
       Dimension d = getSize();
                             
       x0 = 0;
       xN = d.width - 1;
       y0 = 0;
       yN = d.height - 1;
                             
       Panel p, p1;
       //setLayout(new BorderLayout());
       p = new Panel();
       p1 = new Panel();
             
       myCanvas = new DoodleSineCanvas();
       add(myCanvas);
       snglButton = new Button("One Norml Wave");
       snglButton.addActionListener(this);
       p.add(snglButton); // a single cycle of wave
       freqButton = new Button("Inc Freq Wave");
       freqButton.addActionListener(this);
       p.add(freqButton); //increase the number of times the wave cycles happens
       ampButton = new Button("Inc Amp Wave");
       ampButton.addActionListener(this);
       p.add(ampButton); // increase the extent of the peaks and vallies
       phaseButton = new Button("Shft Phse Wave");
       phaseButton.addActionListener(this);
       p.add(phaseButton); //  adjust where it starts by moving things over.
       refreshButton = new Button("Refresh Wave");
       refreshButton.addActionListener(this);
       p.add(refreshButton);
             
       // removed the Choices Choice c = new Choice()
                             
       setBackground(Color.blue);
       setSize(APPLET_WIDTH, APPLET_HEIGHT);
                             
       add("South", p);
       
       }
       double amp;
       double freq;
       double phaseshift;
                         
       public void actionPerformed(ActionEvent e) {
           if (e.getSource() == snglButton) {
              freq = Math.PI / (180/8) ;
              amp = 1;
              phaseshift = 0;
           } else if (e.getSource() == freqButton) {
                     freq = Math.PI / (180/8) ;
                     amp = 1 ;
                     phaseshift = 0;
                  } else if (e.getSource() == ampButton) {
                            freq = Math.PI / (180/8);
                            amp = 0.5 ;
                            phaseshift = 0;
                         } else if (e.getSource() == phaseButton) {
                                   freq = Math.PI / (180/8);
                                   amp = 1 ;
                                   phaseshift = 2 * Math.PI / 4 ; // 2 * Math.PI/4 = 90 degrees
                                                                                                } else if (e.getSource() == refreshButton) {
                                                                      myCanvas.repaint();
                                                                                                                        }
           double r = 30 * amp ; // amplitude
           double cx = 60; // x start drawing on the Canvas at 60
           double cy = 110; // y start position on Canvas at 110
           double piRadian = Math.PI / 180; // initialize piRadian
           int n = 1 ; // a counter to show in the System.out. the approximate degree
           // and radian equivalent.
                             
           while (piRadian < 2 * 180 * freq ) {
           //   Show the results in the syeten out windor
                 System.out.println( n + " degress = " + piRadian + " radians"); // showing a degree  of
                 n = n + 1; // and the radian values
                 cx = cx + 1.0; // progress of cx to the right
                 double rx1 = cx;
                 int x1 = (int) rx1;
                 double ry1 = r * Math.sin(piRadian + phaseshift) + cy; // has to move cy down in order to
                     //be seen
                 int y1 = (int)ry1;
                 double rx2 = rx1 ;
                 int x2 = (int)rx2 ;
                 double ry2 = ry1 ;
                 int y2 = (int)ry2;
                 y1 = 220 - y1; // translates the y1 values to the twice the Canvas height
                 y2 = 220 - y2; // translates the y2 values to the twice the Canvas height
                 //myCanvas.getGraphics().drawLine(x1,y1,x2,y2); // this graphing tool will only use integers
                 //piRadian = piRadian + freq; // for the while  another one more freq variable
                 myCanvas.getGraphics().drawOval(x1,y1,1,2);  
                 piRadian = piRadian + freq;
               
                 }
              }
                         
          }      // removed all the if statements and the  rest of the looping
                         
                       
The other class is below:

class DoodleSineCanvas extends Canvas {
                             
private final int CANVAS_WIDTH = 400;
private final int CANVAS_HEIGHT = 200;
                             
public DoodleSineCanvas() {
                                 
    setBackground(Color.cyan);
    setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
}
                             
public void paint(Graphics g) {
    g.setColor(Color.black);
    g.drawString("Modulation Diagram", 150, 10);
    g.drawLine(60, 40, 60, 180); // Vertical axis canvas position for start position is 60,110
    g.drawLine(60, 110, 400, 110); // Horizontal axis canvas position for start position is
                     //60,110
    g.drawString("0", 50, 115);
    g.drawString("1", 50, 85);
    g.drawLine(60, 80, 70, 80); // Horizontal marker for one above  (0,0)
    g.drawString("2", 50, 55);
    g.drawLine(60, 50, 70, 50); // Horizontal marker for two above(0,0)
    g.drawString("-1", 45, 145);
    g.drawLine(60, 140, 70, 140); // Horizontal marker for one below (0,0)
    g.drawString("-2", 45, 175);
    g.drawLine(60, 170, 70, 170); // Horizontal marker for two below (0,0)
    //g.drawLine(150, 100, 150, 120); // Vertical Y marker at 180 degrees
    //g.drawString("180", 140, 130);
    //g.drawLine(240, 100, 240,120); //  Vertical Y marker at 360 degrees
    //g.drawString("360", 230, 130);
 }
}

Regards,
Guster
Avatar of Guster

ASKER

Hi again,

I'm trying to implement the MouseListener for something simple to start off with, but whenever I compile it an error message says :
"DoodleSineWaveTest.java:6: class DoodleSineWaveTest must be declared abstract. It does not define void mousePressed(java.awt.event.MouseEvent) from interface java.awt.event.MouseListener.

public class DoodleSineWaveTest extends java.applet.Applet implements ActionListener, MouseListener { "

             ^

below is the extra code I've put in the program:

public class DoodleSineWaveTest extends java.applet.Applet implements ActionListener, MouseListener {

...
myCanvas.addMouseListener(this);
...

public void MouseClicked(MouseEvent m) {
            if (m.getSource() == myCanvas) {
            myCanvas.getGraphics().drawLine(80, 80, 300, 80);
        }
}


I'm just trying to be able to display a line in the display area whenever it is clicked on. Am I doing something wrong here??

Regards,
Guster
Change "MouseClicked" to "mouseClicked". All method names begin with a lower case letter.
Avatar of Guster

ASKER

The same error message comes up when I changed it?
ASKER CERTIFIED SOLUTION
Avatar of yongsing
yongsing

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
~~~~~:)-> Thanks for returning and finalizing this. <=(:~~~~~~~

Did you try yongsing's last information?

Moondancer - EE Moderator