Java 拖放问题
该程序是在网格线中拖放图像 这是实际代码:Java 拖放问题,java,swing,drag-and-drop,Java,Swing,Drag And Drop,该程序是在网格线中拖放图像 这是实际代码: 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() {
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();
}
}
import java.awt.*;
导入java.awt.event.*;
导入java.awt.geom.*;
导入java.awt.image.buffereImage;
导入javax.swing.*;
公共类DragAndDrop扩展JFrame
{
帆布;
公共DragAndDrop()
{
超级();
Container=getContentPane();
挫折地面(颜色:白色);
画布=新的瓦特画布(800600,45,40);
容器。添加(画布);
JPanel面板=新的JPanel();
panel.setLayout(新网格布局(1,2));
container.add(面板,BorderLayout.SOUTH);
addWindowListener(新的WindowAdapter(){
公共无效窗口关闭(WindowEvent e){
系统出口(0);
}
});
设置大小(12001000);
setVisible(真);
}
类扩展了JPanel
{
光标curSP、curCSF、curCDF、curUB、curPP、curMod、curKD、cur;
int buttonLeftBorder=900;
int buttonoporder=20;
双spX=buttonLeftBorder,spY=ButtonPorder+10,spW=20,spH=20;//单托盘坐标
双csFX=buttonleftorder,csFY=buttonoporder+90,csFW=80,csFH=40;//案例流坐标
双cdFX=buttonleftorder,cdFY=buttonoporder+150,cdFW=40,cdFH=40;//caddyFlow坐标
双ubX=buttonLeftBorder,ubY=ButtonTopOrder+210,ubW=80,ubH=20;//卸载批量坐标
双ppX=buttonLeftBorder,ppY=ButtonPorder+250,ppW=80,ppH=20;//预点击坐标
double modX=buttonleftorder,modY=buttonoporder+290,modW=40,modH=40;//模块坐标
双kdX=buttonLeftBorder,kdY=ButtonPorder+350,kdW=20,kdH=20;//kd坐标
int-x1,y1,x2,y2;
整数宽度、高度、行数、列数;
矩形单托盘;
矩形二维病例流;
矩形二维caddyFlow;
矩形体;
矩形二维预切片;
矩形二维模块;
矩形2dkd;
矩形2D选择形状;
矩形2D边界REC;
公共区域(内部w、内部h、内部r、内部c)
{
挫折地面(颜色:白色);
设置尺寸(宽度=w,高度=h);
行=r;
cols=c;
addMouseListener(新的MyMouseListener());
addMouseMotionListener(新的MyMouseMotionListener());
setTitle(“仓库分配模型”);
}
公共空间涂料(图g)
{
Font insideDrawing=新字体(“TimeRoman”,Font.PLAIN,9);
Font boldDescription=新字体(“TimeRoman”,Font.BOLD,10);
图形2d g2=(图形2d)g;
//单托盘定位
单托盘=新矩形2D.双托盘(spX、spY、spW、spW);
g2.牵引(单托盘);
g2.setFont(内部绘图);
g2.拉丝(“xxx”,钮扣左订单+05,钮扣左订单+15);
g2.设置字体(粗体描述);
g2.拉绳(“单托盘通道”,按钮左订单+25,按钮下订单+20);
//多托盘
g2.setFont(内部绘图);
g2.拉丝(“xxx”,按钮左订单+05,按钮左订单+55);
g2.drawRect(按钮左键顺序,按钮右键顺序+50,20,20);
g2.drawRect(按钮左键顺序+20,按钮右键顺序+50,20,20);
g2.drawRect(按钮左键顺序+40,按钮右键顺序+50,20,20);
g2.drawRect(按钮左键顺序+60,按钮右键顺序+50,20,20);
g2.设置字体(粗体描述);
g2.拉丝(“流线(4-深示例)”,按钮左订单+85,按钮左订单+60);
//案例流程
caseFlow=新矩形2D.Double(csFX、csFY、csFW、csFH);
g2.setFont(内部绘图);
g2.拉丝(“案例流程”,按钮左订单+20,按钮左订单+115);
g2.绘制(案例流);
g2.设置字体(粗体描述);
g2.拉丝(“标准箱流架”,按钮左订单+85,按钮左订单+115);
//球童流
caddyFlow=新矩形2D.双(cdFX、cdFY、cdFW、cdFH);
g2.setFont(内部绘图);
g2.拉丝(“球童”,纽扣左订单+10,纽扣左订单+170);
g2.拉丝(“流动”,按钮左键订单+12,按钮左键订单+180);
g2.绘制(caddyFlow);
g2.设置字体(粗体描述);
g2.抽绳(“标准球童流架”,左键订单+45,右键订单+175);
//卸料门
unloadBulk=新矩形2D.Double(ubX、ubY、ubW、ubH);
g2.setFont(内部绘图);
g2.设置颜色(颜色为黑色);
g2.填充(卸载散装);
g2.设置颜色(颜色为白色);
g2.拉绳(“卸载/散装门”,按钮左订单+3,按钮下订单+223);
g2.设置颜色(颜色为黑色);
g2.设置字体(粗体描述);
g2.拉丝(“卸载/散装门”,按钮左订单+85,按钮下订单+223);
//预选交易
prePick=新矩形2D.双(ppX、ppY、ppW、ppH);
g2.setFont(内部绘图);
g2.填充(预填充);
g2.设置颜色(颜色为白色);
g2.拉丝(“PP/Trad”,按钮左订单+20,按钮左订单+263);
g2.设置字体(粗体描述);
g2.设置颜色(颜色为黑色);
g2.抽绳(“预取模块”,按钮左订单+85,按钮下订单+263);
//带斑点图案的模块
模块=新矩形2D.Double(modX、modY、modW、modH);
g2.setFont(内部绘图);
BuffereImage bi=新的BuffereImage(5,5,BuffereImage.TYPE_INT_RGB);
Graphics2D big=bi.createGraphics();
大。设置颜色(颜色。白色);
big.fillRect(0,0,5,5);
大颜色(颜色:浅灰色);
大椭圆形(2,2,2,2);
TexturePaint tp=新的TexturePaint(bi,新矩形(5,5));
public void mouseDragged(MouseEvent e) {
//...
canvas.repaint();
}