Results 1 to 10 of 10
Thread: Java 2d Graphics Drag and Drop
- 07-13-2009, 08:54 PM #1
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
Java 2d Graphics Drag and Drop
Hi, I am trying to do some drag and drop with graphics. I have several rectangles that I want people to be able to click on, and drag over to a grid. If the cursor is in the grid, then I want a copy of the graphic to "snap" to one of the grid squares. If it is in the menu section, I don't want it to do anything. I've kind of gotten it to drag, but it is very slow, and lands about half an inch to the left of the original image. I'm pretty new to Java, especially graphics, so any help is greatly appreciated. Below you will find the code.
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.*; import java.awt.image.BufferedImage; import javax.swing.*; public class DragAndDrop extends JFrame { WATCanvas canvas; public DragAndDrop() { super(); Container container = getContentPane(); setBackground(Color.WHITE); canvas = new WATCanvas(800,600,45,40); container.add(canvas); JPanel panel = new JPanel(); panel.setLayout(new GridLayout(1, 2)); container.add(panel, BorderLayout.SOUTH); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setSize(1200,1000); setVisible(true); } class WATCanvas extends JPanel { Cursor curSP, curCSF, curCDF, curUB, curPP, curMod, curKD, cur; int buttonLeftBorder = 900; int buttonTopBorder = 20; double spX=buttonLeftBorder, spY=buttonTopBorder+10, spW=20, spH=20;//singlePallet coordinates double csFX=buttonLeftBorder, csFY=buttonTopBorder+90, csFW=80, csFH=40;//caseFlow coordinates double cdFX=buttonLeftBorder, cdFY=buttonTopBorder+150, cdFW=40, cdFH=40;//caddyFlow coordinates double ubX=buttonLeftBorder, ubY=buttonTopBorder+210, ubW=80, ubH=20;//unloadBulk coordinates double ppX=buttonLeftBorder, ppY=buttonTopBorder+250, ppW=80, ppH=20;//prePick coordinates double modX=buttonLeftBorder, modY=buttonTopBorder+290, modW=40, modH=40;//module coordinates double kdX=buttonLeftBorder, kdY=buttonTopBorder+350, kdW=20, kdH=20;//kd coordinates int x1, y1, x2, y2; int width, height, rows, cols; Rectangle2D singlePallet; Rectangle2D caseFlow; Rectangle2D caddyFlow; Rectangle2D unloadBulk; Rectangle2D prePick; Rectangle2D module; Rectangle2D kd; Rectangle2D selectedShape; Rectangle2D boundingRec; public WATCanvas(int w, int h, int r, int c) { setBackground(Color.white); setSize(width = w, height = h); rows = r; cols = c; addMouseListener(new MyMouseListener()); addMouseMotionListener(new MyMouseMotionListener()); setTitle("Warehouse Allocation Model"); } public void paint(Graphics g) { Font insideDrawing = new Font("TimesRoman",Font.PLAIN, 9); Font boldDescription = new Font("TimesRoman", Font.BOLD, 10); Graphics2D g2 = (Graphics2D) g; //single pallet location singlePallet = new Rectangle2D.Double(spX,spY,spW,spW); g2.draw(singlePallet); g2.setFont(insideDrawing); g2.drawString("xxx", buttonLeftBorder+05, buttonTopBorder+15); g2.setFont(boldDescription); g2.drawString("Single Pallet Lane", buttonLeftBorder+25, buttonTopBorder+20); //Multiple Pallets g2.setFont(insideDrawing); g2.drawString("xxx",buttonLeftBorder+05,buttonTopBorder+55); g2.drawRect(buttonLeftBorder, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+20, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+40, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+60, buttonTopBorder+50, 20, 20); g2.setFont(boldDescription); g2.drawString("Flow Lane (4-deep example)", buttonLeftBorder+85, buttonTopBorder+60); //Case Flow caseFlow = new Rectangle2D.Double(csFX,csFY,csFW,csFH); g2.setFont(insideDrawing); g2.drawString("Case Flow", buttonLeftBorder+20, buttonTopBorder+115); g2.draw(caseFlow); g2.setFont(boldDescription); g2.drawString("Standard Case Flow Rack", buttonLeftBorder+85, buttonTopBorder+115); //Caddy Flow caddyFlow = new Rectangle2D.Double(cdFX,cdFY,cdFW,cdFH); g2.setFont(insideDrawing); g2.drawString("Caddy",buttonLeftBorder+10, buttonTopBorder+170); g2.drawString("Flow",buttonLeftBorder+12,buttonTopBorder+180); g2.draw(caddyFlow); g2.setFont(boldDescription); g2.drawString("Standard Caddy Flow Rack", buttonLeftBorder+45, buttonTopBorder+175); //Unload Bulk Door unloadBulk = new Rectangle2D.Double(ubX,ubY,ubW,ubH); g2.setFont(insideDrawing); g2.setColor(Color.BLACK); g2.fill(unloadBulk); g2.setColor(Color.WHITE); g2.drawString("Unload/Bulk Door", buttonLeftBorder+3, buttonTopBorder+223); g2.setColor(Color.BLACK); g2.setFont(boldDescription); g2.drawString("Unload/Bulk Door", buttonLeftBorder+85, buttonTopBorder+223); //Pre-Pick Trad prePick = new Rectangle2D.Double(ppX,ppY,ppW,ppH); g2.setFont(insideDrawing); g2.fill(prePick); g2.setColor(Color.WHITE); g2.drawString("PP/Trad",buttonLeftBorder+20, buttonTopBorder+263); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("Pre-Pick Module", buttonLeftBorder+85, buttonTopBorder+263); //Module with spotted pattern module = new Rectangle2D.Double(modX,modY,modW,modH); g2.setFont(insideDrawing); BufferedImage bi = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.setColor(Color.WHITE); big.fillRect(0, 0, 5, 5); big.setColor(Color.LIGHT_GRAY); big.fillOval(2, 2, 2, 2); TexturePaint tp = new TexturePaint(bi, new Rectangle(5,5)); g2.setPaint(tp); g2.fill(module); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("Module", buttonLeftBorder+04, buttonTopBorder+310); g2.draw(module); g2.setFont(boldDescription); g2.drawString("Standard Caddy Flow Rack", buttonLeftBorder+45, buttonTopBorder+310); //KD with spotted pattern kd = new Rectangle2D.Double(kdX,kdY,kdW,kdH); BufferedImage bi2 = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB); Graphics2D big2 = bi2.createGraphics(); big2.setColor(Color.DARK_GRAY); big2.fillRect(0, 0, 5, 5); big2.setColor(Color.BLACK); big2.fillOval(2, 2, 2, 2); TexturePaint tp2 = new TexturePaint(bi2, new Rectangle(5,5)); g2.setPaint(tp2); g2.fill(kd); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("KD", buttonLeftBorder+4, buttonTopBorder+365); g2.draw(kd); g.setFont(boldDescription); g.drawString("KD Stands", buttonLeftBorder+25, buttonTopBorder+365); width = 900; height = 800; int start = 25; int rowHt = 20; for(int j=0;j<=rows;++j) g.drawLine(start, start + j*rowHt, height+start,start + j*rowHt); int rowWid = 20; for(int k=0;k<=cols;++k) g.drawLine(start+k*rowWid, start, start + k*rowWid, width+start); if (cur != null) setCursor(cur); } class MyMouseListener extends MouseAdapter { public void mousePressed(MouseEvent e) { if (singlePallet.contains(e.getX(), e.getY())) { selectedShape = singlePallet; if (boundingRec != null) boundingRec = singlePallet.getBounds2D(); } else if (caseFlow.contains(e.getX(), e.getY())) { selectedShape = caseFlow; if (boundingRec != null) boundingRec = caseFlow.getBounds2D(); } else if (caddyFlow.contains(e.getX(), e.getY())) { selectedShape = caddyFlow; if (boundingRec != null) boundingRec = caddyFlow.getBounds2D(); } else if (unloadBulk.contains(e.getX(), e.getY())) { selectedShape = unloadBulk; if (boundingRec != null) boundingRec = unloadBulk.getBounds2D(); } else if (prePick.contains(e.getX(), e.getY())) { selectedShape = prePick; if (boundingRec != null) boundingRec = prePick.getBounds2D(); } else if (module.contains(e.getX(), e.getY())) { selectedShape = module; if (boundingRec != null) boundingRec = module.getBounds2D(); } else if (kd.contains(e.getX(), e.getY())) { selectedShape = kd; if (boundingRec != null) boundingRec = kd.getBounds2D(); } else { boundingRec = null; } canvas.repaint(); x1 = e.getX(); y1 = e.getY(); } public void mouseReleased(MouseEvent e) { if (singlePallet.contains(e.getX(), e.getY())) { boundingRec = singlePallet.getBounds2D(); selectedShape = singlePallet; } else if (caseFlow.contains(e.getX(), e.getY())) { boundingRec = caseFlow.getBounds2D(); selectedShape = caseFlow; } else if (caddyFlow.contains(e.getX(), e.getY())) { boundingRec = caddyFlow.getBounds2D(); selectedShape = caddyFlow; } else if (unloadBulk.contains(e.getX(), e.getY())) { boundingRec = unloadBulk.getBounds2D(); selectedShape = unloadBulk; } else if (prePick.contains(e.getX(), e.getY())) { boundingRec = prePick.getBounds2D(); selectedShape = prePick; } else if (module.contains(e.getX(), e.getY())) { boundingRec = module.getBounds2D(); selectedShape = module; } else if (kd.contains(e.getX(), e.getY())) { boundingRec = kd.getBounds2D(); selectedShape = kd; } //canvas.repaint(); } public void mouseClicked(MouseEvent e) { if (singlePallet.contains(e.getX(), e.getY())) { selectedShape = singlePallet; boundingRec = singlePallet.getBounds2D(); } else if (caseFlow.contains(e.getX(), e.getY())) { selectedShape = caseFlow; boundingRec = caseFlow.getBounds2D(); } else if (caddyFlow.contains(e.getX(), e.getY())) { selectedShape = caddyFlow; boundingRec = caddyFlow.getBounds2D(); } else if (unloadBulk.contains(e.getX(), e.getY())) { selectedShape = unloadBulk; boundingRec = unloadBulk.getBounds2D(); } else if (prePick.contains(e.getX(), e.getY())) { selectedShape = prePick; boundingRec = prePick.getBounds2D(); } else if (module.contains(e.getX(), e.getY())) { selectedShape = module; boundingRec = module.getBounds2D(); } else if (kd.contains(e.getX(), e.getY())) { selectedShape = kd; boundingRec = kd.getBounds2D(); } else { if (boundingRec != null) boundingRec = null; } canvas.repaint(); } } class MyMouseMotionListener extends MouseMotionAdapter { public void mouseDragged(MouseEvent e) { if (singlePallet.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = singlePallet; x2 = e.getX(); y2 = e.getY(); spX = spX + x2 - x1; spY = spY + y2 - y1; x1 = x2; y1 = y2; } else if (caseFlow.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); csFX = csFX + x2 - x1; csFY = csFY + y2 - y1; x1 = x2; y1 = y2; } else if (caddyFlow.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); cdFX = cdFX + x2 - x1; cdFY = cdFY + y2 - y1; x1 = x2; y1 = y2; } else if (unloadBulk.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); ubX = ubX + x2 - x1; ubY = ubY + y2 - y1; x1 = x2; y1 = y2; } else if (prePick.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); ppX = ppX + x2 - x1; ppY = ppY + y2 - y1; x1 = x2; y1 = y2; } else if (module.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); modX = modX + x2 - x1; modY = modY + y2 - y1; x1 = x2; y1 = y2; } else if (kd.contains(e.getX(), e.getY())) { boundingRec = null; selectedShape = caseFlow; x2 = e.getX(); y2 = e.getY(); kdX = kdX + x2 - x1; kdY = kdY + y2 - y1; x1 = x2; y1 = y2; } } public void mouseMoved(MouseEvent e) { if(singlePallet != null && caseFlow != null && caddyFlow != null && unloadBulk != null && prePick != null && module != null && kd != null) { if (singlePallet.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (caseFlow.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (caddyFlow.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (unloadBulk.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (prePick.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (module.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else if (kd.contains(e.getX(), e.getY())) { cur = Cursor.getPredefinedCursor(Cursor.HAND_CURSOR); } else { cur = Cursor.getDefaultCursor(); } } canvas.repaint(); } } } public static void main(String arg[]) { new DragAndDrop(); }
- 07-13-2009, 10:00 PM #2
These kind of applications are complex and it is better to build them with care.
When you run into something like this it is often helpful to turn aside and make up a small test app to isolate what you want to work on. This will give you the space to experiment and work things out with more ease, speed and efficiency.
Try starting with something like this
and begin building it up to do what you want, such as selection, cursor changes, multiple rectangles or shapes, etc.Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.MouseInputAdapter; public class GraphicDragAndDrop extends JPanel { Rectangle rect = new Rectangle(100,100,150,75); protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.blue); g2.draw(rect); } public void setRect(int x, int y) { rect.setLocation(x, y); repaint(); } public static void main(String[] args) { GraphicDragAndDrop test = new GraphicDragAndDrop(); new GraphicDragController(test); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(400,400); f.setLocation(100,100); f.setVisible(true); } } class GraphicDragController extends MouseInputAdapter { GraphicDragAndDrop component; Point offset = new Point(); boolean dragging = false; public GraphicDragController(GraphicDragAndDrop gdad) { component = gdad; component.addMouseListener(this); component.addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); Rectangle r = component.rect; if(r.contains(p)) { offset.x = p.x - r.x; offset.y = p.y - r.y; dragging = true; } } public void mouseReleased(MouseEvent e) { dragging = false; } public void mouseDragged(MouseEvent e) { if(dragging) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } } }
Then you can integrate what you develop in your test app into your main app.
Sometimes the work you do in your test app helps you see how to redesign/refactor you main app.
- 07-13-2009, 10:06 PM #3
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
Oh wow, thank you. This is extremely helpful. I really appreciate your help!
-Shawna
- 07-15-2009, 05:37 PM #4
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
I'm having a problem running this program after changing it a bit. I get a null pointer exception, but I'm not sure why. Do I need to re-initialize singlePallet, etc... in the mouse events?
Java Code:import java.awt.*; import java.awt.event.*; import java.awt.geom.Rectangle2D; import java.awt.image.BufferedImage; import javax.swing.*; import javax.swing.event.MouseInputAdapter; public class DNDTest extends JPanel { Cursor curSP, curCSF, curCDF, curUB, curPP, curMod, curKD, cur; int buttonLeftBorder = 900; int buttonTopBorder = 20; int spX=buttonLeftBorder, spY=buttonTopBorder+10, spW=20, spH=20;//singlePallet coordinates int csFX=buttonLeftBorder, csFY=buttonTopBorder+90, csFW=80, csFH=40;//caseFlow coordinates int cdFX=buttonLeftBorder, cdFY=buttonTopBorder+150, cdFW=40, cdFH=40;//caddyFlow coordinates int ubX=buttonLeftBorder, ubY=buttonTopBorder+210, ubW=80, ubH=20;//unloadBulk coordinates int ppX=buttonLeftBorder, ppY=buttonTopBorder+250, ppW=80, ppH=20;//prePick coordinates int modX=buttonLeftBorder, modY=buttonTopBorder+290, modW=40, modH=40;//module coordinates int kdX=buttonLeftBorder, kdY=buttonTopBorder+350, kdW=20, kdH=20;//kd coordinates int x1, y1, x2, y2; int rows=45, cols = 40; int width, height; boolean draggingSP, draggingCSF, draggingCDF, draggingUB, draggingPP, draggingMod, draggingKD = false; Rectangle singlePallet; Rectangle caseFlow; Rectangle caddyFlow; Rectangle unloadBulk; Rectangle prePick; Rectangle module; Rectangle kd; Rectangle selectedShape; Rectangle boundingRec; protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); Font insideDrawing = new Font("TimesRoman",Font.PLAIN, 9); Font boldDescription = new Font("TimesRoman", Font.BOLD, 10); //single pallet location Rectangle singlePallet = new Rectangle(spX,spY,spW,spW); g2.draw(singlePallet); g2.setFont(insideDrawing); g2.drawString("xxx", buttonLeftBorder+05, buttonTopBorder+15); g2.setFont(boldDescription); g2.drawString("Single Pallet Lane", buttonLeftBorder+25, buttonTopBorder+20); //Multiple Pallets g2.setFont(insideDrawing); g2.drawString("xxx",buttonLeftBorder+05,buttonTopBorder+55); g2.drawRect(buttonLeftBorder, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+20, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+40, buttonTopBorder+50, 20, 20); g2.drawRect(buttonLeftBorder+60, buttonTopBorder+50, 20, 20); g2.setFont(boldDescription); g2.drawString("Flow Lane (4-deep example)", buttonLeftBorder+85, buttonTopBorder+60); //Case Flow caseFlow = new Rectangle(csFX,csFY,csFW,csFH); g2.setFont(insideDrawing); g2.drawString("Case Flow", buttonLeftBorder+20, buttonTopBorder+115); g2.draw(caseFlow); g2.setFont(boldDescription); g2.drawString("Standard Case Flow Rack", buttonLeftBorder+85, buttonTopBorder+115); //Caddy Flow caddyFlow = new Rectangle(cdFX,cdFY,cdFW,cdFH); g2.setFont(insideDrawing); g2.drawString("Caddy",buttonLeftBorder+10, buttonTopBorder+170); g2.drawString("Flow",buttonLeftBorder+12,buttonTopBorder+180); g2.draw(caddyFlow); g2.setFont(boldDescription); g2.drawString("Standard Caddy Flow Rack", buttonLeftBorder+45, buttonTopBorder+175); //Unload Bulk Door unloadBulk = new Rectangle(ubX,ubY,ubW,ubH); g2.setFont(insideDrawing); g2.setColor(Color.BLACK); g2.fill(unloadBulk); g2.setColor(Color.WHITE); g2.drawString("Unload/Bulk Door", buttonLeftBorder+3, buttonTopBorder+223); g2.setColor(Color.BLACK); g2.setFont(boldDescription); g2.drawString("Unload/Bulk Door", buttonLeftBorder+85, buttonTopBorder+223); //Pre-Pick Trad prePick = new Rectangle(ppX,ppY,ppW,ppH); g2.setFont(insideDrawing); g2.fill(prePick); g2.setColor(Color.WHITE); g2.drawString("PP/Trad",buttonLeftBorder+20, buttonTopBorder+263); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("Pre-Pick Module", buttonLeftBorder+85, buttonTopBorder+263); //Module with spotted pattern module = new Rectangle(modX,modY,modW,modH); g2.setFont(insideDrawing); BufferedImage bi = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB); Graphics2D big = bi.createGraphics(); big.setColor(Color.WHITE); big.fillRect(0, 0, 5, 5); big.setColor(Color.LIGHT_GRAY); big.fillOval(2, 2, 2, 2); TexturePaint tp = new TexturePaint(bi, new Rectangle(5,5)); g2.setPaint(tp); g2.fill(module); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("Module", buttonLeftBorder+04, buttonTopBorder+310); g2.draw(module); g2.setFont(boldDescription); g2.drawString("Standard Caddy Flow Rack", buttonLeftBorder+45, buttonTopBorder+310); //KD with spotted pattern kd = new Rectangle(kdX,kdY,kdW,kdH); BufferedImage bi2 = new BufferedImage(5,5,BufferedImage.TYPE_INT_RGB); Graphics2D big2 = bi2.createGraphics(); big2.setColor(Color.DARK_GRAY); big2.fillRect(0, 0, 5, 5); big2.setColor(Color.BLACK); big2.fillOval(2, 2, 2, 2); TexturePaint tp2 = new TexturePaint(bi2, new Rectangle(5,5)); g2.setPaint(tp2); g2.fill(kd); g2.setFont(boldDescription); g2.setColor(Color.BLACK); g2.drawString("KD", buttonLeftBorder+4, buttonTopBorder+365); g2.draw(kd); g2.setFont(boldDescription); g2.drawString("KD Stands", buttonLeftBorder+25, buttonTopBorder+365); width = 900; height = 800; int start = 25; int rowHt = 20; for(int j=0;j<=rows;++j) g2.drawLine(start, start + j*rowHt, height+start,start + j*rowHt); int rowWid = 20; for(int k=0;k<=cols;++k) g2.drawLine(start+k*rowWid, start, start + k*rowWid, width+start); if (cur != null) setCursor(cur); } public void setRect(int x, int y) { if(draggingSP) singlePallet.setLocation(x, y); else if(draggingCSF) caseFlow.setLocation(x, y); else if(draggingCDF) caseFlow.setLocation(x, y); else if(draggingUB) caseFlow.setLocation(x, y); else if(draggingPP) caseFlow.setLocation(x, y); else if(draggingMod) caseFlow.setLocation(x, y); else if(draggingKD) caseFlow.setLocation(x, y); repaint(); } public static void main(String[] args) { DNDTest test = new DNDTest(); new GraphicDragController2(test); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(1200,1000); f.setLocation(100,100); f.setVisible(true); } } class GraphicDragController2 extends MouseInputAdapter { DNDTest component; Point offset = new Point(); public GraphicDragController2(DNDTest gdad) { component = gdad; component.addMouseListener(this); component.addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); Rectangle r; if(component.singlePallet.contains(p)) { r = component.singlePallet; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingSP = true; } else if(component.caseFlow.contains(p)) { r = component.caseFlow; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingCSF = true; } else if(component.caddyFlow.contains(p)) { r = component.caddyFlow; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingCDF = true; } else if(component.unloadBulk.contains(p)) { r = component.unloadBulk; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingUB = true; } else if(component.prePick.contains(p)) { r = component.prePick; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingPP = true; } else if(component.module.contains(p)) { r = component.module; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingMod = true; } else if(component.kd.contains(p)) { r = component.kd; offset.x = p.x - r.x; offset.y = p.y - r.y; component.draggingKD = true; } else { r = null; component.draggingSP = component.draggingCSF = component.draggingCDF = component.draggingUB = component.draggingPP = component.draggingMod = component.draggingKD = false; } if(r.contains(p)) { } } public void mouseReleased(MouseEvent e) { component.draggingSP = component.draggingCSF = component.draggingCDF = component.draggingUB = component.draggingPP = component.draggingMod = component.draggingKD = false; } public void mouseDragged(MouseEvent e) { if(component.draggingSP) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingCSF) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingCDF) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingUB) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingPP) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingMod) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } else if(component.draggingKD) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; component.setRect(x, y); } } }
- 07-15-2009, 08:24 PM #5
Java Code:C:\jexp>java DNDTest Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at GraphicDragController2.mousePressed(DNDTest.java:207)Console output for this:Java Code:// member variable declaration Rectangle singlePallet; protected void paintComponent(Graphics g) { //... // single pallet location // declares and instantiates a local variable which // hides member variable of same name and whose value // remains null Rectangle singlePallet = new Rectangle(spX,spY,spW,spW); // check value of local variable: System.out.println("singlePallet = " + singlePallet); // check value of member variable: System.out.println("this.singlePallet = " + this.singlePallet); g2.draw(singlePallet); ... }
Solution: remove local variable declaration, that is, change this:Java Code:C:\jexp>java DNDTest singlePallet = java.awt.Rectangle[x=900,y=30,width=20,height=20] this.singlePallet = null singlePallet = java.awt.Rectangle[x=900,y=30,width=20,height=20] this.singlePallet = null
to thisJava Code:Rectangle singlePallet = new Rectangle(spX,spY,spW,spW);
Java Code:singlePallet = new Rectangle(spX,spY,spW,spW);
- 07-15-2009, 08:30 PM #6
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
Alright, I fixed that part. Thank you very much. Is there a way to make a copy of the element and then drag the copy instead of the actual rectangle. I've been messing around with that, and tried to make a new rectangle, but then nothing would move. Just curious, if you have spare time.
Thanks,
Shawna
- 07-15-2009, 08:55 PM #7
Is there a way to make a copy of the element and then drag the copy instead of the actual rectangle
Yes. There are possibilities. Where or which class do you want to keep it in? How do you want to use it?
- 07-15-2009, 09:02 PM #8
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
It doesn't really matter to me which class it is in, whichever is easiest is fine. This is going to be a layout that the user can drag the components from the right over to the left onto the grid. I want them to essentially "snap" to the grid, and that is what I am working on, now. I need the capability for multiple "pallets," and so I was thinking of having a list of each of these objects, so that later on I can have a save function that stores all of the positions of the grid. Anyways, I just need to be able to have multiple copies of each pallet on the grid, with only one occupying each grid square at a time, with no overlaps. I'm going to implement a few other ways for the user to add elements to the grid, but right now I am just starting with DnD.
Essentially, I want one copy of each graphic to remain on the right hand of the screen, and anytime the user clicks on that particular object, they create a copy, and then move the copy to the desired location.
- 07-15-2009, 09:22 PM #9
Okay.
When the user initiates a drag on one of your right–side rectangles create a new Rectangle of the same size at the dragging location and add it to your graphic component. Move it along with dragging. At mouseReleased: calculate the location for the drop on the grid. Set the location of the rectangle and add it to an array or List/ArrayList whose contents are drawn each trip thru paintComponent. Remove the rectangle from the panel and repaint.
- 07-15-2009, 09:27 PM #10
Member
- Join Date
- Jul 2009
- Posts
- 9
- Rep Power
- 0
Similar Threads
-
Drag and Drop(URGENT)
By simmi in forum AWT / SwingReplies: 5Last Post: 07-10-2009, 11:16 AM -
Drag and drop
By thayalan in forum AWT / SwingReplies: 1Last Post: 02-16-2009, 03:04 PM -
Can we make Java Report just by Drag and Drop?
By freezea in forum Enterprise JavaBeans (EJB)Replies: 1Last Post: 12-21-2008, 01:48 PM -
Drag and drop
By abhivenugopal in forum JavaServer Pages (JSP) and JSTLReplies: 0Last Post: 01-30-2008, 02:10 PM -
help drag and drop in JTabbedPane
By RO86 in forum AWT / SwingReplies: 0Last Post: 08-14-2007, 01:22 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks