Solved

Clickable image form in Java

Posted on 2002-03-30
16
500 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
 
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 

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 50 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

705 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now