你好,我有一个有粘滞便笺的程序。你点击它们来捡起它们然后再次点击将它们放在某个地方。我的问题是当两个或多个粘滞便笺在彼此之上时,两者都被拾起我只想要拿起最上面的一个。我怎么能解决这个问题,到目前为止是我的代码:
public class PhoneMsg {
public int x, y, id, hour, minute;
public boolean drag = false;
public String name, lastname, msg, msg2, msg3;
public Rectangle rx = new Rectangle(x + 290, y, 20, 20);
public Rectangle rdrag = new Rectangle(x, y, 310, 20);
public boolean remove;
private Image img;
public PhoneMsg(int x, int y, String name, String lastname, int hour, int minute, int id) {
this.x = x;
this.y = y;
this.name = name;
this.lastname = lastname;
this.hour = hour;
this.minute = minute;
this.id = id;
rdrag = new Rectangle(x, y, 310, 20);
rx = new Rectangle(x + 290, y, 20, 20);
genMsg();
}
public void tick() {
rx = new Rectangle(x + 290, y, 20, 20);
rdrag = new Rectangle(x, y, 310, 20);
if (rx.intersects(Comp.mx, Comp.my, 1, 1)) {
if (Comp.ml) {
remove = true;
for (int i = 0; i < play.ph.pp.toArray().length; i++) {
// play.ph.pp.get(i).canreadtxt = true;
}
}
}
// dragging
if (drag) {
x = Comp.mx - 140;
y = Comp.my - 10;
}
if (msg == null) {
genMsg();
}
}
public void render(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
RenderingHints rh = new RenderingHints(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_GASP);
g2.setRenderingHints(rh);
// display
g.setColor(new Color(19, 165, 34));
g.fillRect(x, y, 310, 150);
g.setColor(new Color(27, 53, 16));
g.drawRect(x, y, 310, 150);
g.setColor(new Color(27, 53, 16));
g.drawRect(x, y, 310, 20);
// Exit part
g.setColor(new Color(27, 53, 16));
g.drawRect(rx.x, rx.y, rx.width, rx.height);
g.setColor(Color.red);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString("X", rx.x + 7, rx.y + 15);
// name
g.setColor(Color.black);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString("" + name + " " + lastname + "'s Recent Messages", x + 5, y + 15);
// details
String msg11 = String.format("%02d:%02d", hour, minute);
String msg21 = String.format("%02d:%02d", hour, minute);
String msg31 = String.format("%02d:%02d", hour, minute);
if(play.hud.wifi >= 1){
g.setColor(Color.CYAN);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString(" " + msg, x + 2, y + 38);
g.setColor(Color.white);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString(" " + msg2, x + 2, y + 58);
g.setColor(Color.cyan);
g.setFont(new Font("italic", Font.BOLD, 12));
g.drawString(" " + msg3, x + 2, y + 78);
}else if(play.hud.wifi <= 0){
g.setColor(Color.red);
g.setFont(new Font("italic", Font.PLAIN, 18));
g.drawString("Lost Connection", x +90, y +85);
g.setColor(Color.red);
g.setFont(new Font("italic", Font.PLAIN, 18));
g.drawString("_________________", x +70, y +88);
}
}
} 这是鼠标监听器中允许你拿起便签的部分:
// dragging msg's
for (int i1 = 0; i1 < play.ph.pm.toArray().length; i1++) {
if (play.ph.pm.get(i1).drag == false && play.holding == false) {
if (play.ph.pm.get(i1).rdrag.contains(Comp.mx, Comp.my)) {
play.ph.pm.get(i1).drag = true;
play.holding = true;
}
} else {
play.ph.pm.get(i1).drag = false;
play.holding = false;
}
}
答案 0 :(得分:0)
如果您退出for循环,您可以获取代码在重叠时获得的第一个便笺。
if (play.ph.pm.get(i1).rdrag.contains(Comp.mx, Comp.my)) {
play.ph.pm.get(i1).drag = true;
play.holding = true;
break; // Found sticky note, exits loop
}
如果这与你的else语句冲突,只需创建一个标志。
// dragging msg's
boolean gotCard = false;
for (int i1 = 0; i1 < play.ph.pm.toArray().length; i1++) {
if (play.ph.pm.get(i1).drag == false && play.holding == false && gotNote == false) { // Added gotNote check
if (play.ph.pm.get(i1).rdrag.contains(Comp.mx, Comp.my)) {
play.ph.pm.get(i1).drag = true;
play.holding = true;
gotNote = true; // Set flag to true
}
} else {
play.ph.pm.get(i1).drag = false;
play.holding = false;
}
}
但是你应该有一个z-index实现,以便能够分辨出哪个便签在顶部,然后按照z-index对你的数组进行排序。