I am making a free cross platform mock up designer in Java. I have designed the UI & was able to add components(selected by user) on a Panel.Now I wish to do following things-
1.make the added components re-sizable while the app is running.I mean I want to show a double head arrow when the mouse pointer is at border of that component so that user starts drag the mouse pointer & its size increases or decreases.
2.enable smooth move of components inside the panel while dragged.(by this time it seems frustrating because it blinks(flickers) while moving & place it self in another position rather than mouse pointer hotspot!)
3.Some components(during adding to panel by selecting that component from component pane then clicking on the panel) are placed a bit far from expected point(hotspot).
Here’s the code for drag:
public void dragControl(MouseEvent evt)
{
JComponent jc=(JComponent)evt.getSource();
if((evt.getX()<drawingPane.getWidth()-64)&&(evt.getY()<drawingPane.getHeight()-32))
{
jc.setLocation(evt.getX(),evt.getY());
drawingPane.validate();
}
}
and here’s one that for adding the component in panel.(drawingpane)
private void finalizeControlAddition(JComponent c,JComponent cont,MouseEvent evt,int width,int height)
{
if((evt.getX()<drawingPane.getWidth()-width)&&(evt.getY()<drawingPane.getHeight()-height))
addComponent(cont,c,evt.getX(),evt.getY(),width,height);
//
c.setName(Integer.toString(counter));//save a tag of its identification
//
components.add(counter, c);
counter++;
//
selectedControl=-1;
//
cont.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
//
statusLabel.setText("Control added.Drag to give proper position or select to change properties from Properties pane.");
}
private void addComponent(Container container,Component c,int x,int y,int width,int height)
{
c.setBounds(x,y,width,height);
container.add(c);
c.repaint();
}
After trying hard I have solved these problems. As these are very common problems that anyone may face, I have created a open library for doing all these tasks which you can use in your app. It can be found here:
http://sourceforge.net/p/actioncomponent/home/Home