Java App-Hotspot ve RealMedia Kullanımı

Bu Applet’te 2 kitaplık kullanacağız:
1. HotSpot
2. RealMedia

Aşağıdaki .class dosyalarına ihtiyacımız var:
1. HotSpotListener.class (arabirim)
2. HotSpot.class (bileşen)
3. Loadable.class (arabirim)
4. RealMedia.class (görünmez bileşen)

Ayrıca uygulamada kullanmak üzere 8 gif’e ihtiyacımız olacak. Gif’ler burada yer almamaktadır.

Bu uygulamada 2 buton olacaktır. İlk butonuna basıldığında bir gif görüntülenecek, ikinci butona basıldığında gif ortadan kaldırılacaktır.

import java.awt.*;
import java.applet.*;

// RealMedia için Loadable gereklidir, böylece bu kodun görüntü ve sesi yüklenebilir.
// HotSpotListener, HotSpot'un hotSpotEvent (HotSpot) çağrısında bulunmasına izin vermek için gereklidir.
public class Library extends Applet implements Loadable, HotSpotListener
{
  RealMedia rm;
  HotSpot hs1,hs2;
  int x =1;
  
  public void init()
  {
    setLayout(null);
    setBackground(Color.black);
    rm = new RealMedia(this);
    rm.add("ornek1.gif","ornek1");
    rm.add("ornek2.jpg","ornek2");
    
  // 3 resim ile Başlat butonu
  // Varsayılan durum, mouseOver ve mouseClick durumu.
    rm.add("ornek3.gif");
    rm.add("ornek4.gif");
    rm.add("ornek5.gif");
    
  //Diğer buton
    rm.add("ornek6.gif");
    rm.add("ornek7.gif");
    rm.add("ornek8.gif");
  //resimler yükleniyor
    rm.load();
  }
    
  public void startUp()
  {
  //buton1'i oluştur
    hs1 = new HotSpot(this);
  //Varsayılan resim
    hs1.setImage(rm.get("ornek3.gif"),1);
  //mouseOver resmi
    hs1.setImage(rm.get("ornek4.gif"),2);
  //mouseClick resmi
    hs1.setImage(rm.get("ornek5.gif"),3);
  //Konum ve boyut tanımlama
    hs1.setBounds(20,300,200,200);
    
  // Aynı şekilde buton2 oluşturulur.
    hs2 = new HotSpot(this);
    hs2.setImage(rm.get("ornek6.gif"),1);
    hs2.setImage(rm.get("ornek7.gif"),2);
    hs2.setImage(rm.get("ornek8.gif"),3);
    hs2.setBounds(260,300,200,200);
    
    add(hs1);
    add(hs2);
    hs1.repaint();
    hs2.repaint();
  }
  
  public void paint(Graphics g)
  {
    g.setColor(Color.white);
    
    if (!rm.isLoaded()) 
    {
      g.drawString("loading file "+rm.getCurrent(),20,20);
      g.drawString("of "+rm.getTotalFiles(),20,40);			
      g.drawString("Percent: "+rm.getPercent(),20,60);			
      g.fillRect(20,80,rm.getPercent(),20);			
    }

    else
    {
      if (x == 1)
        g.drawImage(rm.get("ornek1"),80,20,this);
      else
        g.drawImage(rm.get("ornek2"),80,20,this);
    }	
  }
  
  public void hotSpotEvent(HotSpot hs)
  {
    if (hs == hs1)
      x = 1;
    else
      x = 2;
    repaint();	
  }

  public Image loadImage(String file) 
  {
    return getImage(getCodeBase(),file);
  }
  
  public AudioClip loadAudio(String file) 
  {
    return getAudioClip(getDocumentBase(),file);
  }	
}

HotSpotListener.java

import java.awt.*;

interface HotSpotListener 
{
  void hotSpotEvent(HotSpot hs);
}

HotSpot.java

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

public class HotSpot extends Canvas implements MouseListener
{
  private static HotSpotListener hsl;
  private Color fgcolor1,fgcolor2,fgcolor3;
  private Color bgcolor1,bgcolor2,bgcolor3;
  private String mesg1,mesg2,mesg3;
  private Image bgimage1,bgimage2,bgimage3;
  private int width;
  private int height;
  private boolean mouseOver, mousePressed;;
  private boolean enabled;
  private boolean useImages;
  private boolean action;
  private FontMetrics fm;
  private int fontwidth, fontsize;
  private Image buff;
  private Graphics g;
  
  public HotSpot(HotSpotListener hsl)
  {
    bgcolor1 = new Color(0,0,0);
    bgcolor2 = new Color(0,0,0);
    bgcolor3 = new Color(0,0,0);
    fgcolor1 = new Color(0,0,000);
    fgcolor2 = new Color(060,060,060);
    fgcolor3 = new Color(255,255,255);
    mesg1 = "";
    mesg2 = "";
    mesg3 = "";
    setBackground(Color.yellow);
    this.hsl = hsl;
    addMouseListener(this);
  }

  public HotSpot(String mesg1,HotSpotListener hsl)
  {
    this(hsl);
    this.mesg1 = mesg1;
  }

  public void setImage(Image img,int state)
  {
    switch (state)
    {
    case 1 : this.bgimage1 = img; break;
    case 2 : this.bgimage2 = img; break;
    case 3 : this.bgimage3 = img; break;
    default : this.bgimage1 = img;
    }
  }

  public void paint(Graphics g2)
  {
    Dimension dim = getSize();
    if (buff == null)
    {
      buff = createImage(dim.width,dim.height);
      g = buff.getGraphics();
    }

    int bgwidth, bgheight = 0;

    if (mousePressed)
    {
      g.setColor(bgcolor3);
      g.fillRect(0,0,dim.width,dim.height);

      if (bgimage3 != null)
      {
        bgwidth = bgimage3.getWidth(this)/2;
        bgheight = bgimage3.getHeight(this)/2;
        g.drawImage(bgimage3,dim.width/2-bgwidth,dim.height/2-bgheight,this);
      }
    }
    else if (mouseOver)
    {
      g.setColor(bgcolor2);
      g.fillRect(0,0,dim.width,dim.height);
      if (bgimage2 != null)
      {
        bgwidth = bgimage2.getWidth(this)/2;
        bgheight = bgimage2.getHeight(this)/2;
        g.drawImage(bgimage2,dim.width/2-bgwidth,dim.height/2-bgheight,this);
      }
    }
    else
    {
      g.setColor(bgcolor1);
      g.fillRect(0,0,dim.width,dim.height);
      if (bgimage1 != null)
      {
        bgwidth = bgimage1.getWidth(this)/2;
        bgheight = bgimage1.getHeight(this)/2;
        g.drawImage(bgimage1,dim.width/2-bgwidth,dim.height/2-bgheight,this);
      }
    }
    g2.drawImage(buff,0,0,this);
  }

  public void update(Graphics g3)
  {
    paint(g3);
  }	

  public void mouseClicked(MouseEvent me)
  {
    repaint();
  }
  public void mouseReleased(MouseEvent me)
  {
    hsl.hotSpotEvent(this);
    mousePressed = false;
    repaint();
  }
  public void mousePressed(MouseEvent me)
  {
    mousePressed = true;
    repaint();
  }
  public void mouseEntered(MouseEvent me)
  {
    mouseOver =true;
    setCursor(new Cursor(Cursor.HAND_CURSOR));
    repaint();
  }
  public void mouseExited(MouseEvent me)
  {
    mouseOver = false;
    mousePressed = false;
    setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
    repaint();
  }
}

Loadable.java

import java.applet.AudioClip;
import java.awt.*;

public interface Loadable
{
  public Image loadImage(String fileName);
  public AudioClip loadAudio(String fileName);
  public void startUp();
  public void repaint();
  
}

RealMedia.java

import java.util.*;
import java.awt.*;
import java.applet.*;

public class RealMedia extends Component implements Runnable
{
  private Loadable lb;
  private Hashtable htImage, htsound;
  private Stack st1,st2;
  private int delay, size;
  private boolean loadFlag = true;
  private MediaTracker mt;
  private boolean loading;
  private Thread pauseThread;

  public RealMedia(Loadable lb)
  {
    this.lb = lb;
    htImage = new Hashtable();
    htsound = new Hashtable();
    st1 = new Stack();
    st2 = new Stack();
    mt = new MediaTracker(this);
    pauseThread = new Thread(this);
    setDelay(40);	
  }

  public void add(String file)
  {
    st1.push(getMedia(file));
    st2.push(file);
  }

  public void add(String file,String name)
  {
    st1.push(getMedia(file));
    st2.push(name);
  }

  public Image get(String name)
  {	
    return (Image)htImage.get(name);
  }
  
  public void play(String name)
  {		
    AudioClip sound = (AudioClip)htsound.get(name);
    sound.play();
  }
  
  public int getCurrent()
  {
    return size-st1.size();
  }
  
  public int getPercent() 
  {
    return (int)((double)getCurrent()/(double)size*100);
  }
  
  public int getTotalFiles()
  {
    return size;
  }
  
  public void load() 
  {	
    size = st1.size();
    pauseThread.start();
  }
  
  public void setDelay(int delay)
  {
    this.delay = delay;
  }

  public boolean isLoaded()
  {
    return st1.empty();
  }

  private Object getMedia(String file)
  {
    if (isAudio(file)) return new String(file);
    else return lb.loadImage(file);
  }

  private void loadNext()
  {
    if (!st1.empty() && !loading)
    {	
      loading = true;
      mt = new MediaTracker(this);
      Object currentFile = null;
      String currentName = "";
      currentFile = (Object)st1.pop();
      currentName = (String)st2.pop();
            
      if (currentFile instanceof Image)
      {
        mt.addImage((Image)currentFile,0);
        htImage.put(currentName,(Image)currentFile);
      }
      else if (currentFile instanceof String) 
      {
        htsound.put(currentName,lb.loadAudio((String)currentFile));
      }
      try
      {
        mt.waitForAll();
      }
      catch (Exception p)
      {
      }
    }
    loading = false;
  }
  
  public void run() 
  {
    while (!st1.empty()) 
    {
    	loadNext();
    lb.repaint();
    try 
    {
      pauseThread.sleep(delay);
    }
    catch (Exception e)
    {
    }
    }
    lb.startUp();
    pauseThread = null;
    System.out.println("Media Fully Loaded");
  }
    
  private boolean isAudio(String file)
  {
    if (file.endsWith(".au")) return true;
    else return false;
  }
}