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.
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);
}
}
The following code is the class format i need to follow
Component Class
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){
}
}
Model CLass
public class ZoomModel extends JSLider {
}
UIDelegate Class
public class ZoomUI extends ComponentUI implements ChangeListener{
}
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.
test application
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);
}
}
Okay, so that was a fun adventure into parts of the API I don't use :), start by having a read through How to Write a Custom Swing Component and it's associated links, this will give you the ground work to understand what is about to happen...
Model
The Interface
Personally, I always start with an interface, life is better with interfaces and it gives you more flexibility. Now, which model should you extend from (based on your requirements)...?
Well, the best choice I could find was the BoundedRangeModel, which is also used by the JSlider...this actually means that I can not only pass this model to the view, but to a JSlider and without any extra work, have the slider change the image!! Win-Win
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.BoundedRangeModel;
public interface ZoomModel extends BoundedRangeModel {
public Image getImage();
public Dimension getScaledSize();
}
The Abstract
Next, I like to make an abstract version, this is where I put "common" functionality, which is likely to be the same for most implementations, in this case, it might not be required, but I'm finckle like this...
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;
}
}
So, you can see here, I've defined some basic properties, a starting zoom level of 100, a max level of 200 and a minimum level of 0, plus I've implemented the getScaledSize, which is used a bit and makes life easier...
The Default...
Now, because we like been nice, we provide a "default" implementation of the model. This is pretty basic in that all it does it takes a reference to an image...
import java.awt.Image;
public class DefaultZoomModel extends AbstractZoomModel {
Image image;
public DefaultZoomModel(Image image) {
this.image = image;
}
#Override
public Image getImage() {
return image;
}
}
You could create implementations that download images from an URL for example...
The View
Okay, this is the actually component itself, which gets added to your UI. It contains the basic functionality need to construct and prepare the UI delegate and manage the model. The key thing of interest here is the use of the property change support to provide notification of the change to the model, this is important as you will see...
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;
}
}
The UI Delegate
Now the other fun stuff...If we follow standard convention, you would normally provide an abstract concept of the UI delegate, for example...
import javax.swing.plaf.ComponentUI;
public abstract class ZoomUI extends ComponentUI {
}
From this, other delegates will grow...
Basic UI Delegate
Convention would normally suggest you provide a "basic" implementation, doing a lot of the heavy lifting, but allowing other implementations the opportunity to jump in change things to there likely
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
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);
}
}
}
From here you could go and devise platform specific implementations, but I've decided to stick with the basic delegate...
Putting it all together
If that wasn't enough, before you can use any of it, you must install the delegate...
UIManager.getDefaults().put("ZoomComponentUI", "your.awesome.package.name.BasicZoomUI");
nb: Change your.awesome.package.name to reflect your actual package name...
Runnable Example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestZoom100 {
public static void main(String[] args) {
new TestZoom100();
}
public TestZoom100() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.getDefaults().put("ZoomComponentUI", "your.awesome.package.name.BasicZoomUI");
try {
DefaultZoomModel model = new DefaultZoomModel(ImageIO.read(new File("/your/awesome/image.jpg")));
model.setValue(50);
ZoomComponent zoomComp = new ZoomComponent();
zoomComp.setModel(model);
JSlider slider = new JSlider(model);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(zoomComp));
frame.add(slider, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
}
Don't forget to change the package name for the BasicZoomUI to the package name you have it stored in and actually specify a image file ;)
Related
I'm making a 2d game engine and trying to improve the input system. When I try to use the WASD keys, the KeyListener sometimes won't detect the key being pressed but it still detects it being released. I tried using KeyBindings instead but I get the same problem. Anyone know what's going on?
(maybe it has something to do with my IDE(netbeans), mac, or java?)
edit: here's someone else's code that has the same problem
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setContentPane(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
private Surface surface;
public TestPane() {
setLayout(new BorderLayout());
surface = new Surface();
InputMap inputMap = getInputMap(WHEN_IN_FOCUSED_WINDOW);
ActionMap actionMap = getActionMap();
System.out.println("yo");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, false), "Pressed.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true), "Release.left");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false), "Pressed.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, true), "Release.right");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, false), "Pressed.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_W, 0, true), "Release.up");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, false), "Pressed.down");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_S, 0, true), "Release.down");
actionMap.put("Pressed.left", surface.getLeftPressAction());
actionMap.put("Release.left", surface.getLeftReleaseAction());
actionMap.put("Pressed.right", surface.getRightPressAction());
actionMap.put("Release.right", surface.getRightReleaseAction());
actionMap.put("Pressed.up", surface.getUpPressAction());
actionMap.put("Release.up", surface.getUpReleaseAction());
actionMap.put("Pressed.down", surface.getDownPressAction());
actionMap.put("Release.down", surface.getDownReleaseAction());
add(surface);
}
}
public class Surface extends Canvas {
private String displayText = "...";
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2d = (Graphics2D) g.create();
FontMetrics fm = g2d.getFontMetrics();
int x = (getWidth() - fm.stringWidth(displayText)) / 2;
int y = ((getHeight() - fm.getHeight()) / 2) + fm.getAscent();
g2d.drawString(displayText, x, y);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
public Action getLeftPressAction() {
return new TextAction("Left");
}
public Action getLeftReleaseAction() {
return new ClearAction();
}
public Action getRightPressAction() {
return new TextAction("Right");
}
public Action getRightReleaseAction() {
return new ClearAction();
}
public Action getUpPressAction() {
return new TextAction("Up");
}
public Action getUpReleaseAction() {
return new ClearAction();
}
public Action getDownPressAction() {
return new TextAction("Down");
}
public Action getDownReleaseAction() {
return new ClearAction();
}
public class TextAction extends AbstractAction {
private String text;
public TextAction(String text) {
this.text = text;
}
#Override
public void actionPerformed(ActionEvent e) {
displayText = text;
repaint();
}
}
public class ClearAction extends AbstractAction {
#Override
public void actionPerformed(ActionEvent e) {
displayText = "...";
repaint();
}
}
}
}
I just tried using the jar file in a windows 10 and it worked perfectly... It may have something to do with my operating system or my version of java.
I have two components each which its own MouseMotionListener. When I move the mouse from the first to the second Component while dragging the first Component, the MouseMotionListener seems to be disabled for the second Component, i.e. mouseMoved is not called at all although I move the mouse over the second component. How do I avoid that "disabling"?
Example:
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class DragTest implements MouseMotionListener {
private static JPanel p1 = new JPanel();
private static JPanel p2 = new JPanel();
public DragTest() {
}
public static void main(String[] args) {
p1.setBackground(Color.RED);
p1.addMouseMotionListener(new DragTest());
p2.setBackground(Color.BLUE);
p2.addMouseMotionListener(new DragTest());
JFrame frame = new JFrame("Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100, 100, 600, 300);
frame.setLayout(new GridLayout(1, 2));
frame.add(p1);
frame.add(p2);
frame.setVisible(true);
}
#Override
public void mouseMoved(MouseEvent e) {
if (e.getSource() == p1) {
System.out.println("mouse movement in p1");
} else if (e.getSource() == p2) {
System.out.println("mouse movement in p2");
}
}
#Override
public void mouseDragged(MouseEvent e) {
if (e.getSource() == p1) {
System.out.println("mouse drag in p1");
} else if (e.getSource() == p2) {
System.out.println("mouse drag in p2");
}
}
}
Disclaimer: This is a VERY, VERY basic example of the core drag'n'drop API and is based on this example and this example and this example and designed to simply demonstrate the possibility to ascertain the location of a prescribed drop
So when it comes to dragging things around, especially across components, it's generally better to use the drag'n'drop API over simply using MouseMotionListener and MouseListener. This is what the API has been designed for and provides notifications to both targets about the nature of the operation
The following example simply stores the location's at which you drop something and paints a nice little dot there
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Point;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.LineBorder;
import javax.swing.border.MatteBorder;
public class DragAndDropTest {
public static void main(String[] args) {
new DragAndDropTest();
}
public DragAndDropTest() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setLayout(new GridLayout(1, 2));
add(new DropPane());
add(new DragPane());
}
#Override
public Dimension getPreferredSize() {
return new Dimension(200, 200);
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g.create();
g2d.dispose();
}
}
public class DragPane extends JPanel {
private DragSource ds;
private Transferable transferable;
public DragPane() {
ds = new DragSource();
transferable = new Transferable() {
#Override
public DataFlavor[] getTransferDataFlavors() {
return new DataFlavor[]{DataFlavor.stringFlavor};
}
#Override
public boolean isDataFlavorSupported(DataFlavor flavor) {
return DataFlavor.stringFlavor.equals(flavor);
}
#Override
public Object getTransferData(DataFlavor flavor) throws UnsupportedFlavorException, IOException {
return "This is a test";
}
};
ds.createDefaultDragGestureRecognizer(this, DnDConstants.ACTION_COPY_OR_MOVE, new DragGestureListener() {
#Override
public void dragGestureRecognized(DragGestureEvent dge) {
// This is where you would export the data you want
// to transfer
ds.startDrag(dge, DragSource.DefaultCopyDrop, transferable, new DragSourceListener() {
#Override
public void dragEnter(DragSourceDragEvent dsde) {
}
#Override
public void dragOver(DragSourceDragEvent dsde) {
}
#Override
public void dropActionChanged(DragSourceDragEvent dsde) {
}
#Override
public void dragExit(DragSourceEvent dse) {
}
#Override
public void dragDropEnd(DragSourceDropEvent dsde) {
}
});
}
});
setLayout(new GridBagLayout());
add(new JLabel("Drag from here"));
setBorder(new LineBorder(Color.RED));
}
}
public class DropPane extends JPanel {
private List<Point> dropPoints;
public DropPane() {
dropPoints = new ArrayList<>(25);
setDropTarget(new DropTarget(this, new DropTargetListener() {
#Override
public void dragEnter(DropTargetDragEvent dtde) {
}
#Override
public void dragOver(DropTargetDragEvent dtde) {
}
#Override
public void dropActionChanged(DropTargetDragEvent dtde) {
}
#Override
public void dragExit(DropTargetEvent dte) {
}
#Override
public void drop(DropTargetDropEvent dtde) {
// Normally here, I'd inspect the Transferable and make sure
// what is been dropped and can be imported, I'd then go through
// the process of unwrapping the data from the Transferable and
// processing it appropriatly, but in this example, I really don't
// care, I just care about WHERE the event occured
dropPoints.add(dtde.getLocation());
dtde.dropComplete(true);
repaint();
}
}));
setLayout(new GridBagLayout());
add(new JLabel("Drop to here"));
setBorder(new MatteBorder(1, 1, 1, 0, Color.RED));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.RED);
for (Point p : dropPoints) {
g.fillOval(p.x - 2, p.y - 2, 5, 5);
}
}
}
}
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.
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);
}
}
The following code is the class format i need to follow
Component Class
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){
}
}
Model CLass
public class ZoomModel extends JSLider {
}
UIDelegate Class
public class ZoomUI extends ComponentUI implements ChangeListener{
}
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.
test application
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);
}
}
Okay, so that was a fun adventure into parts of the API I don't use :), start by having a read through How to Write a Custom Swing Component and it's associated links, this will give you the ground work to understand what is about to happen...
Model
The Interface
Personally, I always start with an interface, life is better with interfaces and it gives you more flexibility. Now, which model should you extend from (based on your requirements)...?
Well, the best choice I could find was the BoundedRangeModel, which is also used by the JSlider...this actually means that I can not only pass this model to the view, but to a JSlider and without any extra work, have the slider change the image!! Win-Win
import java.awt.Dimension;
import java.awt.Image;
import javax.swing.BoundedRangeModel;
public interface ZoomModel extends BoundedRangeModel {
public Image getImage();
public Dimension getScaledSize();
}
The Abstract
Next, I like to make an abstract version, this is where I put "common" functionality, which is likely to be the same for most implementations, in this case, it might not be required, but I'm finckle like this...
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;
}
}
So, you can see here, I've defined some basic properties, a starting zoom level of 100, a max level of 200 and a minimum level of 0, plus I've implemented the getScaledSize, which is used a bit and makes life easier...
The Default...
Now, because we like been nice, we provide a "default" implementation of the model. This is pretty basic in that all it does it takes a reference to an image...
import java.awt.Image;
public class DefaultZoomModel extends AbstractZoomModel {
Image image;
public DefaultZoomModel(Image image) {
this.image = image;
}
#Override
public Image getImage() {
return image;
}
}
You could create implementations that download images from an URL for example...
The View
Okay, this is the actually component itself, which gets added to your UI. It contains the basic functionality need to construct and prepare the UI delegate and manage the model. The key thing of interest here is the use of the property change support to provide notification of the change to the model, this is important as you will see...
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;
}
}
The UI Delegate
Now the other fun stuff...If we follow standard convention, you would normally provide an abstract concept of the UI delegate, for example...
import javax.swing.plaf.ComponentUI;
public abstract class ZoomUI extends ComponentUI {
}
From this, other delegates will grow...
Basic UI Delegate
Convention would normally suggest you provide a "basic" implementation, doing a lot of the heavy lifting, but allowing other implementations the opportunity to jump in change things to there likely
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.ActionMap;
import javax.swing.InputMap;
import javax.swing.JComponent;
import javax.swing.KeyStroke;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import javax.swing.plaf.ComponentUI;
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);
}
}
}
From here you could go and devise platform specific implementations, but I've decided to stick with the basic delegate...
Putting it all together
If that wasn't enough, before you can use any of it, you must install the delegate...
UIManager.getDefaults().put("ZoomComponentUI", "your.awesome.package.name.BasicZoomUI");
nb: Change your.awesome.package.name to reflect your actual package name...
Runnable Example
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSlider;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
public class TestZoom100 {
public static void main(String[] args) {
new TestZoom100();
}
public TestZoom100() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
ex.printStackTrace();
}
UIManager.getDefaults().put("ZoomComponentUI", "your.awesome.package.name.BasicZoomUI");
try {
DefaultZoomModel model = new DefaultZoomModel(ImageIO.read(new File("/your/awesome/image.jpg")));
model.setValue(50);
ZoomComponent zoomComp = new ZoomComponent();
zoomComp.setModel(model);
JSlider slider = new JSlider(model);
JFrame frame = new JFrame("Testing");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(new JScrollPane(zoomComp));
frame.add(slider, BorderLayout.SOUTH);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
} catch (IOException exp) {
exp.printStackTrace();
}
}
});
}
}
Don't forget to change the package name for the BasicZoomUI to the package name you have it stored in and actually specify a image file ;)
I want to create a draggable component contains a shape (circle) and a text (JLabel) under it. But i dont get the shape and text in the jpanel. I attached the code below.
SubMapViewer Class
package test;
import com.businesslense.topology.client.config.Condition;
import com.businesslense.topology.client.config.ConfigReader;
import com.businesslense.topology.client.config.NodeConfig;
import com.businesslense.topology.client.config.Parameter;
import java.awt.Color;
import java.awt.Graphics;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import javax.swing.JFrame;
import javax.swing.JLayeredPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
public class SubMapViewer extends JLayeredPane {
#Override
public void paint(Graphics grphcs) {
super.paint(grphcs); //To change body of generated methods, choose Tools | Templates.
System.out.println("Paint called");
}
public void showMap() {
Runnable gui = new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(
UIManager.getSystemLookAndFeelClassName());
} catch (Exception ex) {
}
JFrame f = new JFrame("Draggable Components");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(300, 300);
f.setLocationRelativeTo(null);
JLayeredPane panel = new SubMapViewer();
panel.setLayout(null);
NodeComponent nodeComponent = new NodeComponent(50);
JPanel jPanel = new JPanel();
jPanel.setSize(100, 100);
jPanel.setBorder(javax.swing.BorderFactory.createLineBorder(Color.BLACK, 2));
jPanel.add(nodeComponent);
jPanel.setVisible(true);
Draggable d2 = new Draggable(jPanel, 200, 150);
panel.add(d2);
f.add(panel);
f.setVisible(true);
}
};
//GUI must start on EventDispatchThread:
SwingUtilities.invokeLater(gui);
}
Properties getDisplayProperties(Properties properties) {
//List<String> menuList = new ArrayList<>();
List<com.businesslense.topology.client.config.NodeConfig> nodeConfigs = ConfigReader.getData().getNodeConfig();
outer:
for (Iterator<NodeConfig> it = nodeConfigs.iterator(); it.hasNext();) {
NodeConfig nodeConfig = it.next();
List<Condition> conditions = nodeConfig.getCondtion();
boolean match = false;
for (Iterator<Condition> it1 = conditions.iterator(); it1.hasNext();) {
Condition condition = it1.next();
if (condition.getValue().equalsIgnoreCase("" + properties.get(condition.getName()))) {
match = true;
} else {
continue outer;
}
}
if (match) {
Properties displayProperties = new Properties();
for (Iterator<Parameter> it1 = nodeConfig.getParameter().iterator(); it1.hasNext();) {
Parameter parameter = it1.next();
displayProperties.put(parameter.getName(), parameter.getValue());
}
return displayProperties;
}
}
return null;
}
public static void main(String[] arv) {
SubMapViewer subMapViewer = new SubMapViewer();
subMapViewer.showMap();
}
}
NodeComponent Class
package test;
import com.businesslense.topology.client.marker.DefaultNodeComponent;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JLabel;
import javax.swing.JPanel;
import org.apache.log4j.Logger;
public class NodeComponent extends JPanel {
private String name;
private Integer size;
static Logger logger = Logger.getLogger(DefaultNodeComponent.class.getName());
public NodeComponent(Integer size) {
setLayout(null);
setSize(size , size * 2);
this.size = size;
this.name = "ICON NAME";
JLabel textLabel = new JLabel(name);
textLabel.setLocation(size, 10);
add(textLabel);
}
#Override
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g.create();
int shapePaddingVal = (size * 5) / 100;
int shapeRadius = (size * 90) / 100;
Color shapeColor = Color.BLACK;
g2d.setColor(shapeColor);
g2d.setStroke(new BasicStroke(2));
g2d.drawOval(shapePaddingVal, shapePaddingVal, shapeRadius/2, shapeRadius/2);
g2d.setColor(Color.BLACK);
g2d.setStroke(new BasicStroke(0, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND));
}
}
Draggable Class
package test;
/*
* Draggable.java
*/
import java.awt.*;
import javax.swing.*;
import java.awt.event.MouseEvent;
import javax.swing.border.Border;
import javax.swing.event.MouseInputAdapter;
public class Draggable extends JComponent {
private Point pointPressed;
private JComponent draggable;
public Draggable(final JComponent component, final int x, final int y) {
draggable = component;
// draggable.setCursor(draggable.getCursor());
setCursor(new Cursor(Cursor.HAND_CURSOR));
setLocation(x, y);
setSize(component.getPreferredSize());
setLayout(new BorderLayout());
add(component);
MouseInputAdapter mouseAdapter = new MouseHandler();
addMouseMotionListener(mouseAdapter);
addMouseListener(mouseAdapter);
}
#Override
public void setBorder(final Border border) {
super.setBorder(border);
if (border != null) {
Dimension size = draggable.getPreferredSize();
Insets insets = border.getBorderInsets(this);
size.width += (insets.left + insets.right + 5);
size.height += (insets.top + insets.bottom);
setSize(size);
}
}
private class MouseHandler extends MouseInputAdapter {
#Override
public void mouseDragged(final MouseEvent e) {
Point pointDragged = e.getPoint();
Point loc = getLocation();
loc.translate(pointDragged.x - pointPressed.x,
pointDragged.y - pointPressed.y);
setLocation(loc);
}
#Override
public void mousePressed(final MouseEvent e) {
pointPressed = e.getPoint();
}
}
}
I want to create a draggable component contains a shape (circle) and a text (JLabel) under it
Why don't you use use a JLabel with an Icon and text?
NodeComponent nodeComponent = new NodeComponent(50);
JPanel jPanel = new JPanel();
jPanel.setSize(100, 100);
jPanel.add(nodeComponent);
By default a JPanel uses a FlowLayout, which respects the components "preferred size". Your NodeComponent class should override the getPreferredSize() method to return a realistic value. Another reason for just using a JLabel since it will determine the preferred size for you based on the Icon and text.
JLabel textLabel = new JLabel(name);
textLabel.setLocation(size, 10);
add(textLabel);
You are adding the label to the component which uses a null layout. Since the size is zero, the text will not display.
i want to draw a canvas that will be movable on a Jpanel. That is when the user clicks on the canvas and drags it it must move to a new positon. i have implemented the MouseMotionListener but i have no idea of what to include inside to make the canvas move as per requierements. here is the DisplayCanvas class:
class DisplayCanvas extends Canvas
{
public DisplayCanvas()
{
setBounds(20, 40, 300, 300);
setBackground(Color.white);
}
}
class shape extends JFrame implements MouseMotionListener{
static JPanel panel;
static Container contentpane;
static DisplayCanvas canvas;
shape()
{
canvas=new DisplayCanvas();
canvas.addMouseMotionListener(this);
panel= new JPanel();
panel.setBounds(20,20,250,140);
panel.setLayout(null);
contentpane = getContentPane();
contentpane.add(canvas);
contentpane.add(panel);
}
#Override
public void mouseDragged(MouseEvent e) {}
#Override
public void mouseMoved(MouseEvent arg0) {}
}
this is how i test it.
public class display
{
static JFrame frame;
public static void main(String[] args)
{
frame=new shape();
frame.setBounds(380, 200, 500, 400);
frame.setTitle("SHAPE AND COLOR");
frame.setVisible(true);
}
}
NB: please do not suggest that i use the JPanel am required to use the canvas.
The fact you don't want to extend JPanel seems quite weird but it is not unfeasible. Yet you will likely encounter issues at some point because you are mixing lightweight and heavyweight components. You will probably have visual glitches and other display issues.
However I would draw your attention to several important mistakes you made in your current code:
Don't extends classes if not needed (no need to extend JFrame nor Canvas)
Don't make variables static unless absolutely necessary
Follow Java naming conventions: class names always start with an Upper-case letter
Don't use a null LayoutManager.
Here is a snippet illustrating very basic way you can make this work (code needs to be refactored to separate aspects properly)
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager2;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.LinkedHashMap;
import java.util.Map;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
public class TestHeavyweightLightweight {
public class MyLayoutManager implements LayoutManager2 {
private Map<Component, Rectangle> constraints = new LinkedHashMap<Component, Rectangle>();
#Override
public void addLayoutComponent(String name, Component comp) {
constraints.put(comp, comp.getBounds());
}
#Override
public void removeLayoutComponent(Component comp) {
constraints.remove(comp);
}
#Override
public Dimension preferredLayoutSize(Container parent) {
Rectangle rect = new Rectangle();
for (Rectangle r : constraints.values()) {
rect = rect.union(r);
}
return rect.getSize();
}
#Override
public Dimension minimumLayoutSize(Container parent) {
return preferredLayoutSize(parent);
}
#Override
public void layoutContainer(Container parent) {
for (Map.Entry<Component, Rectangle> e : constraints.entrySet()) {
e.getKey().setBounds(e.getValue());
}
}
#Override
public void addLayoutComponent(Component comp, Object constraints) {
if (constraints instanceof Rectangle) {
this.constraints.put(comp, (Rectangle) constraints);
} else {
addLayoutComponent((String) null, comp);
}
}
#Override
public Dimension maximumLayoutSize(Container target) {
return new Dimension(Integer.MAX_VALUE, Integer.MAX_VALUE);
}
#Override
public float getLayoutAlignmentX(Container target) {
return 0;
}
#Override
public float getLayoutAlignmentY(Container target) {
return 0;
}
#Override
public void invalidateLayout(Container target) {
}
public void setConstraints(Component component, Rectangle rect) {
constraints.put(component, rect);
}
public class MouseDragger extends MouseAdapter {
private Point lastLocation;
private Component draggedComponent;
#Override
public void mousePressed(MouseEvent e) {
draggedComponent = e.getComponent();
lastLocation = SwingUtilities.convertPoint(draggedComponent, e.getPoint(), draggedComponent.getParent());
}
#Override
public void mouseDragged(MouseEvent e) {
Point location = SwingUtilities.convertPoint(draggedComponent, e.getPoint(), draggedComponent.getParent());
if (draggedComponent.getParent().getBounds().contains(location)) {
Point newLocation = draggedComponent.getLocation();
newLocation.translate(location.x - lastLocation.x, location.y - lastLocation.y);
newLocation.x = Math.max(newLocation.x, 0);
newLocation.x = Math.min(newLocation.x, draggedComponent.getParent().getWidth() - draggedComponent.getWidth());
newLocation.y = Math.max(newLocation.y, 0);
newLocation.y = Math.min(newLocation.y, draggedComponent.getParent().getHeight() - draggedComponent.getHeight());
setConstraints(draggedComponent, new Rectangle(newLocation, draggedComponent.getSize()));
if (draggedComponent.getParent() instanceof JComponent) {
((JComponent) draggedComponent.getParent()).revalidate();
} else {
draggedComponent.getParent().invalidate();
draggedComponent.getParent().validate();
}
lastLocation = location;
}
}
#Override
public void mouseReleased(MouseEvent e) {
lastLocation = null;
draggedComponent = null;
}
public void makeDraggable(Component component) {
component.addMouseListener(this);
component.addMouseMotionListener(this);
}
}
}
private Canvas canvas;
private JPanel panel;
protected void createAndShowGUI() {
JFrame frame = new JFrame(TestHeavyweightLightweight.class.getSimpleName());
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
canvas = new Canvas();
canvas.setBackground(Color.WHITE);
panel = new JPanel();
MyLayoutManager mgr = new MyLayoutManager();
panel.setLayout(mgr);
panel.add(canvas, new Rectangle(20, 40, 300, 300));
MyLayoutManager.MouseDragger mouseDragger = mgr.new MouseDragger();
mouseDragger.makeDraggable(canvas);
frame.add(panel);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
new TestHeavyweightLightweight().createAndShowGUI();
}
});
}
}