Электронная библиотека книг Александра Фролова и Григория Фролова.
Shop2You.ru Создайте свой интернет-магазин
Библиотека
Братьев
Фроловых
[Назад]

Исходный текст программы PanelDemo.java

Оглавление

Назад

// ==========================================
// PanelDemo.java
//(C) Alexandr Frolov, 1998
// E-mail: frolov@glasnet.ru
// Web:    http://www.glasnet.ru/~frolov 
// ==========================================
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;

public class PanelDemo extends Applet
{
  ImagePanel ip;
  ButtonPanel bp;
  Image[] imgSlides;
  
  // ============================================
  // init
  // ============================================
  public void init()
  {
    imgSlides = new Image[7];
    
    MediaTracker tr = new MediaTracker(this);
    for(int i = 0; i < 7; i++)
    {
      imgSlides[i] = getImage(getCodeBase(), 
	"pic0" + (i + 1) + ".gif");
	
      tr.addImage(imgSlides[i], 0);
      try
      {
        tr.waitForAll();
      }
      catch (InterruptedException e) {}
    }
        
    ip = new ImagePanel(imgSlides);
    bp = new ButtonPanel(ip);
    
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
    c.anchor = GridBagConstraints.CENTER; 
    c.fill   = GridBagConstraints.NONE;  
    c.gridheight = 1;
    c.gridwidth  = 1;
    c.gridx = GridBagConstraints.RELATIVE; 
    c.gridy = GridBagConstraints.RELATIVE; 
    c.insets = new Insets(5, 10, 5, 10);
    
    gbl.setConstraints(ip, c);
    add(ip);
    
    c.insets = new Insets(0, 0, 0, 0);
    c.fill   = GridBagConstraints.NONE;  
    gbl.setConstraints(bp, c);
    add(bp);
  }
  
  // ============================================
  // getAppletInfo
  // ============================================
  public String getAppletInfo()
  {
    return "Name: PanelDemo";
  }
}

// ============================================
// Class ImagePanel
// ============================================
class ImagePanel extends Panel
{
  Dimension dm;
  Image[] imgSlides;
  int currentImage = 0;
  Dimension dimMinSize;
  
  // ===========================================
  // ImagePanel
  // ===========================================
  public ImagePanel(Image[] img)
  {
    int imHeight;
    int imWidth;
    
    imgSlides = img;
    imHeight = img[0].getHeight(this);
    imWidth = img[0].getWidth(this);
    
    dimMinSize = 
      new Dimension(imWidth + 4, imHeight + 4);
  }
  
  // ===========================================
  // paint
  // ===========================================
  public void paint(Graphics g)
  {
    super.paint(g);
    dm = getSize();
    
    g.drawImage(imgSlides[currentImage],
       2, 2, this);
    
    g.drawRect(0, 0,
       dm.width - 1, dm.height - 1);
  }
  
  // ===========================================
  // getPreferredSize
  // ===========================================
  public Dimension getPreferredSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // getMinimumSize
  // ===========================================
  public Dimension getMinimumSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // preferredSize
  // ===========================================
  public Dimension preferredSize()
  {
    return dimMinSize;
  }
  
  // ===========================================
  // minimumSize
  // ===========================================
  public Dimension minimumSize()
  {
    return dimMinSize;
  }
}

// ============================================
// Class ButtonPanel
// ============================================
class ButtonPanel extends Panel
  implements ActionListener
{
  Dimension dm;
  Button[] button;
  ImagePanel ip;
  
  // ===========================================
  // ButtonPanel
  // ===========================================
  public ButtonPanel(ImagePanel ipanel)
  {
    ip = ipanel;
    button = new Button[7];
    
    GridBagLayout gbl = new GridBagLayout();
    GridBagConstraints c = 
      new GridBagConstraints();
    
    setLayout(gbl);
    
    c.anchor = GridBagConstraints.NORTHWEST; 
    c.fill   = GridBagConstraints.NONE;  
    c.gridheight = 1;
    c.gridwidth  = GridBagConstraints.REMAINDER;
    c.gridx = GridBagConstraints.RELATIVE; 
    c.gridy = GridBagConstraints.RELATIVE; 
    c.weightx = 1;
    c.weighty = 0;
    c.insets = new Insets(5, 10, 0, 10);
        
    for(int i = 0; i < 7; i++)
    {
      button[i] = 
        new Button("Image " + (i + 1));
      
      if(i == 6)
        c.insets = new Insets(5, 10, 5, 10);
      
      gbl.setConstraints(button[i], c);
      add(button[i]);
      
      button[i].addActionListener(this);
    }
  }
  
  // ===========================================
  // paint
  // ===========================================
  public void paint(Graphics g)
  {
    super.paint(g);
    dm = getSize();
    
    g.setColor(Color.red);
    g.drawRect(0, 0,
      dm.width - 1, dm.height - 1);
  }
  
  // ============================================
  // actionPerformed
  // ============================================
  public void actionPerformed(ActionEvent e)
  {
    for(int i = 0; i < 7; i++)
    {
      if(e.getSource().equals(button[i]))
      {
	ip.currentImage = i;
	ip.repaint();
      }
    }
  }
}

[Назад]