Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Clickable image form in Java

Posted on 2002-03-30
16
Medium Priority
?
519 Views
Last Modified: 2008-03-03
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
0
Comment
Question by:Guster
  • 8
  • 3
  • 2
  • +2
16 Comments
 
LVL 9

Expert Comment

by:yongsing
ID: 6907450
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.
0
 
LVL 4

Expert Comment

by:delphi3
ID: 6908506
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
0
 
LVL 9

Expert Comment

by:Ovi
ID: 6909735
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());
  }
});
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 9

Expert Comment

by:Ovi
ID: 6909736
... and use import java.awt.event.*;
0
 

Author Comment

by:Guster
ID: 6915341
0
 

Author Comment

by:Guster
ID: 6915347
0
 

Author Comment

by:Guster
ID: 6915366
0
 

Author Comment

by:Guster
ID: 6915386
0
 

Author Comment

by:Guster
ID: 6915395
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6915537
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
0
 

Author Comment

by:Guster
ID: 6918356
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
0
 

Author Comment

by:Guster
ID: 6918394
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
0
 
LVL 9

Expert Comment

by:yongsing
ID: 6918449
Change "MouseClicked" to "mouseClicked". All method names begin with a lower case letter.
0
 

Author Comment

by:Guster
ID: 6918467
The same error message comes up when I changed it?
0
 
LVL 9

Accepted Solution

by:
yongsing earned 150 total points
ID: 6919798
When you implement an interface, you need to implement all the methods in the interface. Since you implement MouseListener, you need to have the methods mouseClicked(), mouseEnter(), mouseExited(), mousePressed() and mouserReleased() methods in your DoodleSineWaveTest class:

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

   public DoodleSineWaveTest {
      ...
      myCanvas.addMouseListener(this);
   }

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

  public void mouseEntered(MouseEvent e) {
  }

  public void mouseExited(MouseEvent e) {
  }

  public void mousePressed(MouseEvent e) {
  }

  public void mouseReleased(MouseEvent e) {
  }
}

The alternative is to use an anonymous class:

myCanvas.addMouseListener(
   new MouserAdapter() {
      public void mouseClicked(MouseEvent e) {
         if (m.getSource() == myCanvas) {
         myCanvas.getGraphics().drawLine(80, 80, 300, 80);
      }
   }
);

If you don't understand interfaces and anonymous classes, you should pick up a Java book to read.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6922212
~~~~~:)-> Thanks for returning and finalizing this. <=(:~~~~~~~

Did you try yongsing's last information?

Moondancer - EE Moderator
0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

972 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question