Link to home
Start Free TrialLog in
Avatar of idunnootje
idunnootje

asked on

Images and jsliders :S

Hi,

i'm a beginner in programming in Java and i seem to be a bit stuck.
I'm trying to create an applet in which 2 pictures are shown. If you click the picture in the upper left corner, it should change into an other picture. This works fine.
But, i also want to use a slider! (I don't even know if this is possible,  for i am a beginner).

The source code without the slider is:

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;

public class panorama extends java.applet.Applet implements MouseListener{
     int status;     //1=turn into red 2=turn into blue
     Image img1, blue, red, panorama;
     MediaTracker mt1, mt2;
     public void init() {
          blue = getImage(getDocumentBase(),"blue.jpg"); //width = 74 height = 72
          red = getImage(getDocumentBase(),"red.jpg");
          panorama = getImage(getDocumentBase(),"Panorama.gif");
          img1 = blue;     //veranderd in rood
          status = 1;
          mt1 = new MediaTracker(this);
          mt1.addImage(img1,0);
          try {
               mt1.waitForAll();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
          mt2 = new MediaTracker(this);
          mt2.addImage(panorama,0);
          try {
               mt2.waitForAll();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
          addMouseListener(this);
     }
     public void destroy() {
        removeMouseListener(this);
     }
     public void paint(Graphics g) {
     switch(status){
     case 1:
     img1 = getImage(getDocumentBase(),"red.jpg");
     g.drawImage(panorama,0,0,this);
     System.out.println("Panorama getekend.");
     g.drawImage(img1,0,0,this);
     status=2;
     break;
     case 2:
     img1 = getImage(getDocumentBase(),"blue.jpg");
     g.drawImage(panorama,0,0,this);
     g.drawImage(img1,0,0,this);
     status=1;
     break;
     default:
     img1 = getImage(getDocumentBase(),"red.jpg");
     g.drawImage(panorama,0,0,this);
     g.drawImage(img1,0,0,this);
     status=0;
     }
     }
     public void mouseReleased(MouseEvent e) {
     System.out.println("In Change.mouseReleased");
     int x = e.getX();
     int y = e.getY();
     if(x>0 && x<74 && y>0 && y<72){
          repaint();
          }
     }
     public void mousePressed(MouseEvent e) {
     System.out.println("In Change.mousePressed");
     }
     public void mouseClicked(MouseEvent e) {
     System.out.println("In Change.mouseClicked");
     }
     public void mouseEntered(MouseEvent e) {
     System.out.println("In Change.mouseEntered");
     }
     public void mouseExited(MouseEvent e) {
     System.out.println("In Change.mouseExited");
     }
}

The pictures red.jpg & blue.jpg are basic images (74x72) made in paint. Panorama.gif can be found at: http://www.olympia360.com/art/Panorama.gif

Now i want to add a slider (is it possible to put it anywhere i want?). I thought i could use parts of the following source code: http://grace.evergreen.edu/artofcomp/examples/Slider2.php

I placed the lines from JSlider slider = new JSlider(... through pane.add(slider); after addMouseListener(this); in my own source. I left out the changelistener and the counter. I also importet javax.swing.*; and javax.swing.event.*;.....

This doesn't work, because i get a compiling error at this.getContentPane():
cannot resolve symbol
symbol  : method getContentPane()
location: class panorama
       Container pane = this.getContentPane();
                             ^ (under the 'g')

I guess these are basic errors but as a beginner i seem to be a bit stuck here. So, can somebody help me? Thanks.

Idunnootje.
Avatar of idunnootje
idunnootje

ASKER

Any help would be appreciated!
are you allowed to use Java 2+ or your applet should be Java 1.1 compatible ?

(JSlider is not available in Java 1.1)
It is allowed. After a lot of trying out i'm a bit further now. I'v succeeded in creating the slider. But it only appears when you resize the appletviewer window to the right and click with your mouse on the right of the window.
Then there is the problem of the picture changing when it should not, on resizing the window for example.

import java.awt.event.*;
import java.awt.image.*;
import java.net.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.event.*;

public class panorama extends java.applet.Applet implements MouseListener{
     int status, count=8;//1=turn in red 2=turn in blue
     Image img1, blue, red, panorama;
     MediaTracker mt1, mt2;
     public void init() {
          blue = getImage(getDocumentBase(),"blue.jpg");
          red = getImage(getDocumentBase(),"red.jpg");
          panorama = getImage(getDocumentBase(),"Panorama.gif");
          img1 = blue;     //veranderd in rood
          status = 1;
          mt1 = new MediaTracker(this);
          mt1.addImage(img1,0);
          try { mt1.waitForAll();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
          mt2 = new MediaTracker(this);
          mt2.addImage(panorama,0);
          try { mt2.waitForAll();
          }
          catch(InterruptedException e) {
               e.printStackTrace();
          }
          addMouseListener(this);
     }
     
     public void destroy() {removeMouseListener(this);}

     public void paint(Graphics g) {
          g.drawImage(panorama,0,0,this);
          switch(status){
          case 1:
               img1 = getImage(getDocumentBase(),"red.jpg");
               g.drawImage(img1,0,0,this);
               status=2;
               drawSlider();
          break;
          case 2:
               img1 = getImage(getDocumentBase(),"blue.jpg");
               g.drawImage(img1,0,0,this);
               status=1;
               drawSlider();
          break;
          default:
               img1 = getImage(getDocumentBase(),"red.jpg");
               g.drawImage(img1,0,0,this);
               status=0;
               drawSlider();
          }
     }

     public void drawSlider(){
          setLayout(new BorderLayout());
          JSlider slider1 = new JSlider (JSlider.VERTICAL, 0, 100, 50);
          slider1.setPaintTicks(true);
          slider1.setMajorTickSpacing(10);
          slider1.setMinorTickSpacing(2);
          add(slider1, BorderLayout.EAST);
     }

     public void mouseReleased(MouseEvent e) {
     System.out.println("In Change.mouseReleased");
     int x = e.getX();
     int y = e.getY();
     if(x>0 && x<74 && y>0 && y<72){
          repaint();
          }
     }
     public void mousePressed(MouseEvent e) {
     }
     public void mouseClicked(MouseEvent e) {
     }
     public void mouseEntered(MouseEvent e) {
     }
     public void mouseExited(MouseEvent e) {
     }
}

thnx
ASKER CERTIFIED SOLUTION
Avatar of heyhey_
heyhey_

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
How would i implement this? So u suggest i should create my applet with panels?
yes

Swing have enough components to handle rendering Images, showing scrollbars etc.

you do not need to override repaint at all.
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

- Points for heyhey_

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer