title: custom java Swing component Model, UIDelegate, component format
body: <p>I was given the task to create a custom swing component. I have my component functioning properly in a test application which includes JSlider that is used to zoom in and out on an Image. However I am required to present my custom component in a Model, UIDelegate, and Component class format and I am totally lost on how to convert my code so that it follows this format. Here is the code for my test application. </p>
  
  <pre><code>package test;
  
  import java.awt.*;
  import java.awt.geom.AffineTransform;
  import java.awt.image.BufferedImage;
  
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  import javax.swing.event.*;
  
  import java.io.File;
  import java.net.URL;
  
  import javax.imageio.ImageIO;
  
  public class ZoomDemo extends JComponent implements ChangeListener {
  
  JPanel gui;
  /**
   * Displays the image.
   */
  JLabel imageCanvas;
  Dimension size;
  double scale = 1.0;
  private BufferedImage image;
  
  public ZoomDemo() {
      size = new Dimension(10, 10);
      setBackground(Color.black);
      try {
           image = ImageIO.read(new File("car.jpg"));
      } catch (Exception ex) {
          ex.printStackTrace();
      }
  }
  
  public void setImage(Image image) {
      imageCanvas.setIcon(new ImageIcon(image));
  }
  
  public void initComponents() {
      if (gui == null) {
          gui = new JPanel(new BorderLayout());
          gui.setBorder(new EmptyBorder(5, 5, 5, 5));
          imageCanvas = new JLabel();
          JPanel imageCenter = new JPanel(new GridBagLayout());
          imageCenter.add(imageCanvas);
          JScrollPane imageScroll = new JScrollPane(imageCenter);
          imageScroll.setPreferredSize(new Dimension(300, 100));
          gui.add(imageScroll, BorderLayout.CENTER);
      }
  }
  
  public Container getGui() {
      initComponents();
      return gui;
  }
  
  public void stateChanged(ChangeEvent e) {
      int value = ((JSlider) e.getSource()).getValue();
      scale = value / 100.0;
      paintImage();
  }
  
  protected void paintImage() {
  
      int imageWidth = image.getWidth();
      int imageHeight = image.getHeight();
      BufferedImage bi = new BufferedImage(
              (int)(imageWidth*scale), 
              (int)(imageHeight*scale), 
              image.getType());
      Graphics2D g2 = bi.createGraphics();
      g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
              RenderingHints.VALUE_ANTIALIAS_ON);
      AffineTransform at = AffineTransform.getTranslateInstance(0, 0);
      at.scale(scale, scale);
      g2.drawRenderedImage(image, at);
      setImage(bi);
  }
  
  public Dimension getPreferredSize() {
      int w = (int) (scale * size.width);
      int h = (int) (scale * size.height);
      return new Dimension(w, h);
  }
  
  private JSlider getControl() {
      JSlider slider = new JSlider(JSlider.HORIZONTAL, 1, 500, 50);
      slider.setMajorTickSpacing(50);
      slider.setMinorTickSpacing(25);
      slider.setPaintTicks(true);
      slider.setPaintLabels(true);
      slider.addChangeListener(this);
      return slider;
  }
  
  public static void main(String[] args) {
      ZoomDemo app = new ZoomDemo();
      JFrame frame = new JFrame();
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setContentPane(app.getGui());
      app.setImage(app.image);
  
      // frame.getContentPane().add(new JScrollPane(app));  
      frame.getContentPane().add(app.getControl(), "Last");
      frame.setSize(700, 500);
      frame.setLocation(200, 200);
      frame.setVisible(true);
  }
  }
  </code></pre>
  
  <p>The following code is the class format i need to follow</p>
  
  <p>Component Class</p>
  
  <pre><code>package component;
  
  import javax.swing.JComponent;
  
  import javax.swing.JSlider;
  import javax.swing.plaf.ComponentUI;
  
  public class ProgressBar extends JComponent {
  
  public static ComponentUI createUI(JComponent c) {
      return new ZoomUI();
  }
  
  public void installUI(JComponent c){
  
  
  
  }
  
  public void uninstallUI (JComponent c){
  
  }
  }
  </code></pre>
  
  <p>Model CLass</p>
  
  <pre><code>public class ZoomModel extends JSLider  {
  
  
  
  }
  </code></pre>
  
  <p>UIDelegate Class</p>
  
  <pre><code>public class ZoomUI extends ComponentUI implements ChangeListener{
  
  }
  </code></pre>
  
  <p>Any help on how I can implement my custom component in this format would be greatly appreciated. I am very new to Swing and documentation I have found on custom components has been very confusing and of little help.</p>
  
  <p>test application</p>
  
  <pre><code>package test;
  
  import java.awt.*;
  import java.awt.event.ActionEvent;
  import java.awt.event.ActionListener;
  import java.awt.geom.AffineTransform;
  import java.awt.image.BufferedImage;
  import java.beans.PropertyChangeEvent;
  import java.beans.PropertyChangeListener;
  
  import javax.swing.*;
  import javax.swing.border.EmptyBorder;
  import javax.swing.border.LineBorder;
  import javax.swing.event.*;
  
  import java.io.File;
  import java.net.URL;
  
  import javax.imageio.ImageIO;
  
  import component.ZoomComponent;
  
  public class ZoomDemo  extends JPanel implements PropertyChangeListener, ActionListener {
  
  ZoomComponent zoomer;
  JPanel board;
  private BufferedImage image;
  
  public ZoomDemo( ) {
      super(true);  
      setLayout(new BorderLayout( )); 
      board = new JPanel(true); 
      board.setPreferredSize(new Dimension(300, 300)); 
      board.setBorder(new LineBorder(Color.black, 5));
  
      zoomer = new ZoomComponent();
      add(board, BorderLayout.NORTH);
      add(zoomer, BorderLayout.SOUTH);
  
  
  
  }
  
  
  @Override
  public void actionPerformed(ActionEvent arg0) {
      // TODO Auto-generated method stub
  
  }
  
  @Override
  public void propertyChange(PropertyChangeEvent arg0) {
      // TODO Auto-generated method stub
  
  }
  
  public static void main(String[] args) {
  
      UIManager.getDefaults().put("ZoomComponentUI", "component.BasicZoomUI");
      ZoomDemo s= new ZoomDemo();
      JFrame frame = new JFrame("Sample Sketch Application"); 
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
      frame.setContentPane(s); 
      frame.pack( ); 
      frame.setVisible(true);
  }
  </code></pre>
  
  <p>}</p>