将自定义java swing组件实现到测试应用程序中

时间:2014-11-07 08:32:35

标签: java swing user-interface testing custom-component

我开发了一个自定义摇摆组件,用于放大和缩小图像。考虑到我是Java和Swing的新手,这对我来说是一项非常困难的任务。据我所知,我已经正确地实现了我的类,但是我似乎无法弄清楚如何在测试应用程序中运行我的组件。

// AbstractZoomModel

package component;

import java.awt.Dimension;
import java.awt.Image;
import javax.swing.DefaultBoundedRangeModel;

public abstract class AbstractZoomModel extends DefaultBoundedRangeModel implements ZoomModel {

public AbstractZoomModel() {
    super(100, 0, 0, 200);
}

@Override
public Dimension getScaledSize() {
    Dimension size = new Dimension(0, 0);
    Image image = getImage();
    if (image != null) {
        double scale = getValue() / 100d;
        size.width = (int) Math.round(image.getWidth(null) * scale);
        size.height = (int) Math.round(image.getHeight(null) * scale);

    }
    return size;
}

public Image getImage() {
    // TODO Auto-generated method stub
    return null;
}

}

// DefaultZoomModel

 package component;

import java.awt.Image;

public class DefaultZoomModel extends AbstractZoomModel {

Image image;

public DefaultZoomModel(Image image) {
    this.image = image;
}

@Override
public Image getImage() {
    return image;
}

}

// ZoomModel

package component;


import java.awt.Dimension;
import java.awt.Image;
import javax.swing.BoundedRangeModel;

public interface ZoomModel extends BoundedRangeModel {

public Image getImage();

public Dimension getScaledSize();

}

// ZoomComponent

package component;

import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JComponent;
import javax.swing.UIManager;

public class ZoomComponent extends JComponent {

private static final String uiClassID = "ZoomComponentUI";
private ZoomModel model;

public ZoomComponent() {
    setBackground(Color.black);
    setFocusable(true);
    updateUI();
}

public void setModel(ZoomModel newModel) {
    if (model != newModel) {
        ZoomModel old = model;
        this.model = newModel;
        firePropertyChange("model", old, newModel);
    }
}

public ZoomModel getModel() {
    return model;
}

@Override
public Dimension getPreferredSize() {
    ZoomModel model = getModel();
    Dimension size = new Dimension(100, 100);
    if (model != null) {
        size = model.getScaledSize();
    }
    return size;
}


public void setUI(BasicZoomUI ui) {
    super.setUI(ui);
}

@Override
public void updateUI() {
    if (UIManager.get(getUIClassID()) != null) {
        ZoomUI ui = (ZoomUI) UIManager.getUI(this);
        setUI(ui);
    } else {
        setUI(new BasicZoomUI());
    }
}

public BasicZoomUI getUI() {
    return (BasicZoomUI) ui;
}

@Override
public String getUIClassID() {
    return uiClassID;
}
}

// BasicZoomUI

包组件;

public class BasicZoomUI extends ZoomUI {

private ZoomComponent zoomComponent;
private MouseAdapter mouseHandler;
private ChangeListener changeHandler;

private Action zoomIn;
private Action zoomOut;
private PropertyChangeListener propertyChangeHandler;

protected ChangeListener getChangeHandler() {
    if (changeHandler == null) {
        changeHandler = new ChangeHandler();
    }
    return changeHandler;
}

protected void installMouseListener() {
    mouseHandler = new MouseAdapter() {

        @Override
        public void mouseClicked(MouseEvent e) {
            zoomComponent.requestFocusInWindow();
        }

        @Override
        public void mouseWheelMoved(MouseWheelEvent e) {
            int amount = e.getWheelRotation();
            ZoomModel model = zoomComponent.getModel();
            if (model != null) {

                int value = model.getValue();
                model.setValue(value + amount);

            }
        }

    };
    zoomComponent.addMouseListener(mouseHandler);
    zoomComponent.addMouseWheelListener(mouseHandler);

}

protected void installModelPropertyChangeListener() {

    propertyChangeHandler = new PropertyChangeListener() {
        @Override
        public void propertyChange(PropertyChangeEvent evt) {
            ZoomModel old = (ZoomModel) evt.getOldValue();
            if (old != null) {
                old.removeChangeListener(getChangeHandler());
            }
            ZoomModel newValue = (ZoomModel) evt.getNewValue();
            if (newValue != null) {
                newValue.addChangeListener(getChangeHandler());
            }
        }
    };

    zoomComponent.addPropertyChangeListener("model", propertyChangeHandler);

}

protected void installKeyBindings() {

    zoomIn = new ZoomInAction();
    zoomOut = new ZoomOutAction();

    InputMap inputMap = zoomComponent.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "zoomIn");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0), "zoomOut");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0), "zoomIn");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0), "zoomOut");

    ActionMap actionMap = zoomComponent.getActionMap();
    actionMap.put("zoomIn", zoomIn);
    actionMap.put("zoomOut", zoomOut);
}

protected void installModelChangeListener() {

    ZoomModel model = getModel();
    if (model != null) {
        model.addChangeListener(getChangeHandler());
    }

}

@Override
public void installUI(JComponent c) {

    zoomComponent = (ZoomComponent) c;

    installMouseListener();
    installModelPropertyChangeListener();
    installKeyBindings();
    installModelChangeListener();

}

protected void uninstallModelChangeListener() {

    getModel().removeChangeListener(getChangeHandler());

}

protected void uninstallKeyBindings() {

    InputMap inputMap = zoomComponent.getInputMap(JComponent.WHEN_FOCUSED);
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_ADD, 0), "donothing");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_SUBTRACT, 0), "donothing");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_PLUS, 0), "donothing");
    inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_MINUS, 0), "donothing");

    AbstractAction blank = new AbstractAction() {
        @Override
        public void actionPerformed(ActionEvent e) {
        }
    };

    ActionMap actionMap = zoomComponent.getActionMap();
    actionMap.put("zoomIn", blank);
    actionMap.put("zoomOut", blank);

}

protected void uninstallModelPropertyChangeListener() {

    zoomComponent.removePropertyChangeListener(propertyChangeHandler);
    propertyChangeHandler = null;

}

protected void uninstallMouseListener() {

    zoomComponent.removeMouseWheelListener(mouseHandler);
    mouseHandler = null;

}

@Override
public void uninstallUI(JComponent c) {

    uninstallModelChangeListener();
    uninstallModelPropertyChangeListener();
    uninstallKeyBindings();
    uninstallMouseListener();

    mouseHandler = null;
    zoomComponent = null;

}

@Override
public void paint(Graphics g, JComponent c) {
    super.paint(g, c);
    paintImage(g);
}

protected void paintImage(Graphics g) {
    if (zoomComponent != null) {
        ZoomModel model = zoomComponent.getModel();
        Image image = model.getImage();
        Dimension size = model.getScaledSize();
        int x = (zoomComponent.getWidth() - size.width) / 2;
        int y = (zoomComponent.getHeight() - size.height) / 2;
        g.drawImage(image, x, y, size.width, size.height, zoomComponent);
    }
}

public static ComponentUI createUI(JComponent c) {
    return new BasicZoomUI();
}

protected ZoomModel getModel() {

    return zoomComponent == null ? null : zoomComponent.getModel();

}

protected class ChangeHandler implements ChangeListener {

    @Override
    public void stateChanged(ChangeEvent e) {
        zoomComponent.revalidate();
        zoomComponent.repaint();
    }

}

protected class ZoomAction extends AbstractAction {

    private int delta;

    public ZoomAction(int delta) {
        this.delta = delta;
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        ZoomModel model = getModel();
        if (model != null) {
            model.setValue(model.getValue() + delta);
        }
    }

}

protected class ZoomOutAction extends ZoomAction {

    public ZoomOutAction() {
        super(-5);
    }

}

protected class ZoomInAction extends ZoomAction {

    public ZoomInAction() {
        super(5);
    }

}

}

// ZoomUI

package component;

import javax.swing.plaf.ComponentUI;

public abstract class ZoomUI extends ComponentUI {    


}

到目前为止,这是我的测试应用程序,我知道这是不正确的,并且无法正常运行。有关如何正确实现此组件并使其在测试应用程序中运行的任何提示都会受到极大的影响

package test;


public class ZoomDemo  extends JPanel implements PropertyChangeListener, ActionListener {

ZoomComponent zoomer;
JPanel board;
private BufferedImage image;

public ZoomDemo( ) {

    super(true);  
    setLayout(new BorderLayout( )); 




    ZoomModel model = new DefaultZoomModel(image);
    board = new JPanel(true); 
    board.setPreferredSize(new Dimension(500, 600)); 


    zoomer = new ZoomComponent();
    zoomer.setModel(model);
    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);
}


}

0 个答案:

没有答案