# 2
Hi
Thanks for the reply. My code has changed a lot since I last posted yesterday. Im now using drap and drop. However, I am using the cell components (such as a robotic arm and conveyor belt) as JLabel's. The code should explain this better. Ultimately when the production cell is set up I would like to animate the cell components (JLabels). Is this set up the best way of implementing it? I would appreciate any help/suggestions/comments.
The code is as follows:
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import javax.swing.event.*;
import java.awt.dnd.*;
public class CustomCell extends JFrame {
JPanel tpan = new JPanel();
JPanel fpan = new JPanel();
LoadingLabelloadRobot= new LoadingLabel("");
ConveyorLabelconveyorlbl= new ConveyorLabel("");
UnLoadingLabel unloadRobot= new UnLoadingLabel("");
JLabel td= new JLabel("");
JLabel lm;
Color sc;
Cursor dc= new Cursor(Cursor.DEFAULT_CURSOR);
Cursor mc= new Cursor(Cursor.MOVE_CURSOR);
Cursor yd= DragSource.DefaultMoveDrop;
Point mp;
public CustomCell() {
super("Create Custom Cell");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent ev) {
dispose();
System.exit(0);
}
});
setBounds(3,10,635,550); //size of panel
set_dragger();//call function
getContentPane().setLayout(null);
getContentPane().add(fpan);
fpan.setBackground(Color.lightGray); //set background color of left frame
fpan.setLayout(null);
fpan.setBounds(5,5,140,500); //x, y co-rodinate and width and height of left frame
add_comp(loadRobot,Color.white);//call function to add label to left frame
loadRobot.setBounds(10,20,120,50);//set size of label
//loadRobot.setOpaque(true);
add_comp(conveyorlbl,Color.white);//call function to add oval to left frame
conveyorlbl.setBounds(10,90,120,50); //set size of rectangle
add_comp(unloadRobot,Color.white);//call function to add oval to left frame
unloadRobot.setBounds(10,160,120,50); //set size of rectangel
getContentPane().add(tpan);
tpan.setBackground(Color.lightGray); //color of right frame
tpan.setLayout(null);
tpan.setBounds(150,5,470,500); //set size of right frame
setVisible(true);
}
private void set_dragger() {
td.setBackground(Color.gray);
td.setOpaque(true);
td.setBounds(1,1,0,0);
getContentPane().add(td);
}
private void add_comp(JLabel l, Color c) {
l.setHorizontalAlignment(SwingConstants.CENTER);
l.setForeground(Color.black);
l.setBackground(c); //color of label
fpan.add(l);
mak_lis(l); //call function to add mouse listener for each object
}
private void mak_lis(final JLabel l) {
l.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent m)
{
setCursor(yd);
}
});
l.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
lm = l;
sc = lm.getBackground();
l.setBackground(Color.lightGray);
}
public void mouseReleased(MouseEvent m) {
lm.setBackground(sc);
setCursor(dc);
td.setBounds(0,0,0,0);
int x = m.getX()+lm.getX()+fpan.getX();
int y = m.getY()+lm.getY()+fpan.getY();
if (x > tpan.getX())
{
tpan.add(new_lab(lm,x-tpan.getX(),y-tpan.getY()));
tpan.repaint();
}
}
});
}
private Component new_lab(JLabel co, int x, int y) { //function to draw label in new posn
JLabel nl = null;
if (co instanceof JLabel)
{
nl = new ConveyorLabel("");
//((JLabel)nl).setOpaque(true);
}
if (co instanceof ConveyorLabel)
{
nl = new ConveyorLabel("");
}
if (co instanceof UnLoadingLabel)
{
nl = new UnLoadingLabel("");
}
if (co instanceof LoadingLabel){
nl = new LoadingLabel("");
}
nl.setHorizontalAlignment(SwingConstants.CENTER);
nl.setBackground(co.getBackground());
nl.setForeground(co.getForeground());
nl.setSize(co.getSize());
nl.setLocation(x,y);
mak_drg(nl);
return(nl);
}
private void mak_drg(final JLabel l) {
l.addMouseMotionListener(new MouseMotionAdapter()
{
public void mouseDragged(MouseEvent m) {
setCursor(mc);
int x = l.getX()+m.getX()-mp.x;
int y = l.getY()+m.getY()-mp.y;
l.setLocation(x,y);
}
});
l.addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent m) {
lm = l;
mp = m.getPoint();
}
public void mouseReleased(MouseEvent m) {
setCursor(dc);
}
});
}
public class UnLoadingLabel extends JLabel{
private Rectangle2D.Double robInUpper;
private Rectangle2D.Double robInGripperR;
private Rectangle2D.Double robInGripperL;
private Rectangle2D.Double robotPlatform;
private Point2D.Double robInGripperRJoint;
private Point2D.Double robInGripperLJoint;
private Point2D.Double robotLoadPos;
public UnLoadingLabel(String s){
super(s);
setFont(new Font("",2,9));
robotLoadPos = new Point2D.Double(10, 10);
robInUpper = new Rectangle2D.Double();
robInUpper.setRect(robotLoadPos.x+25, robotLoadPos.y, 60, 20);
robInGripperRJoint = new Point2D.Double();
robInGripperLJoint = new Point2D.Double();
robInGripperRJoint.x = robInUpper.getX() + robInUpper.width;
robInGripperRJoint.y = robInUpper.getY();
robInGripperLJoint.x = robInUpper.getX() + robInUpper.width;
robInGripperLJoint.y = robInUpper.getY() + robInUpper.height;
robInGripperR = new Rectangle2D.Double(robInGripperRJoint.x-robInUpper.getWidth()-25, robInGripperRJoint.y, 25, 5);
robInGripperL = new Rectangle2D.Double(robInGripperLJoint.x-robInUpper.getWidth()-25, robInGripperLJoint.y-5, 25, 5);
robotPlatform = new Rectangle2D.Double(robInUpper.getX()+robInUpper.getWidth()-30, robotLoadPos.getY()-10, 40, 40);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(getBackground());
g2.setColor(Color.black);
super.paintComponent(g);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(robInUpper);
g2.draw(robInGripperR);
g2.draw(robInGripperL);
g2.setColor(Color.gray);
g2.draw(robotPlatform);
g2.fill(robotPlatform);
g2.setColor(Color.yellow);
g2.fill(robInUpper);
g2.setColor(Color.red);
g2.fill(robInGripperR);
g2.fill(robInGripperL);
g2.setColor(Color.black);
//g2.drawString("Unloading Robotic Arm", 0, 0);
g.drawString("Unloading Robot", 10, 50);
}
}
public class ConveyorLabel extends JLabel{
private Rectangle2D.Double conveyor;
private Rectangle2D.Double conveyorBelt;
private Rectangle2D.Double conveyorLoadSensor;
private Rectangle2D.Double conveyorUnloadSensor;
private Point2D.Double conveyorPos;
public ConveyorLabel(String s){
super(s);
setFont(new Font("",2,9));
conveyorPos = new Point2D.Double(0, 0);
conveyor = new Rectangle2D.Double();
//conveyor.setRect(conveyorPos.x-195, conveyorPos.y-155, 190, 40);
conveyor.setRect(10, 0, 80, 40);
conveyorBelt = new Rectangle2D.Double();
conveyorBelt.setRect(0, 10, 100, 20);
conveyorLoadSensor = new Rectangle2D.Double();
conveyorLoadSensor.setRect(conveyorBelt.x+conveyorBelt.getWidth()-20, conveyorBelt.y-25, 10, 30);
conveyorUnloadSensor = new Rectangle2D.Double();
conveyorUnloadSensor.setRect(conveyorBelt.x+10, conveyorBelt.y+25, 10, 30);
}
public void paintComponent(Graphics g){
/*Graphics2D g2 = (Graphics2D)g;
g2.setColor(getBackground());
g2.fillOval(0,0,getWidth()-1,getHeight()-1);
g2.setColor(Color.black);
super.paintComponent(g);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.drawOval(0,0,getWidth()-1,getHeight()-1);*/
Graphics2D g2 = (Graphics2D)g;
g2.setColor(getBackground());
g2.setColor(Color.black);
super.paintComponent(g);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(conveyor);
g2.draw(conveyorBelt);
//g2.draw(conveyorLoadSensor);
//g2.draw(conveyorUnloadSensor);
g2.setColor(Color.red);
g2.fill(conveyor);
g2.setColor(Color.black);
g2.fill(conveyorBelt);
/*g2.fill(conveyorLoadSensor);
g2.fill(conveyorUnloadSensor);*/
g.drawString("Conveyor Belt", 40, 50);
}
}
public class LoadingLabel extends JLabel {
private Rectangle2D.Double robInUpper;
private Rectangle2D.Double robInGripperR;
private Rectangle2D.Double robInGripperL;
private Rectangle2D.Double robotPlatform;
private Point2D.Double robInGripperRJoint;
private Point2D.Double robInGripperLJoint;
private Point2D.Double robotLoadPos;
public LoadingLabel(String s){
super(s);
setFont(new Font("",2,9));
//this.setPreferredSize(new Dimension(200, 200));
robotLoadPos = new Point2D.Double(10, 10);
robInUpper = new Rectangle2D.Double();
robInUpper.setRect(robotLoadPos.x, robotLoadPos.y, 60, 20);
robInGripperRJoint = new Point2D.Double();
robInGripperLJoint = new Point2D.Double();
robInGripperRJoint.x = robInUpper.getX() + robInUpper.width;
robInGripperRJoint.y = robInUpper.getY();
robInGripperLJoint.x = robInUpper.getX() + robInUpper.width;
robInGripperLJoint.y = robInUpper.getY() + robInUpper.height;
robInGripperR = new Rectangle2D.Double(robInGripperRJoint.x, robInGripperRJoint.y, 25, 5);
robInGripperL = new Rectangle2D.Double(robInGripperLJoint.x, robInGripperLJoint.y-5, 25, 5);
robotPlatform = new Rectangle2D.Double(robotLoadPos.getX()-10, robotLoadPos.getY()-10, 40, 40);
}
public void paintComponent(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setColor(getBackground());
g2.setColor(Color.black);
super.paintComponent(g);
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_ON);
g2.draw(robInUpper);
g2.draw(robInGripperR);
g2.draw(robInGripperL);
g2.draw(robotPlatform);
g2.setColor(Color.yellow);
g2.fill(robInUpper);
g2.setColor(Color.red);
g2.fill(robInGripperR);
g2.fill(robInGripperL);
g2.setColor(Color.black);
g.drawString("Loading Robot", 40, 50);
}
}
public static void main (String[] args){
new CustomCell();
}
}