2013-04-08 100 views
0

在您閱讀本文之前,我只想說任何幫助對我非常有幫助,現在。我非常絕望。我花了至少一個星期的時間試圖在Eclipse中完成這項工作,我想我錯過了一些明顯的東西,我真的需要你的幫助。很奇怪的鼠標監聽器和If語句

現在,我的鼠標監聽器包含每個'room'的if語句和那些if語句中的double for循環,它們告訴JFrame在JFrame的某個區域內是否單擊鼠標以重新繪製相應的'房間'。

現在讓我們假設房間#4可導致要麼#5或#6。從#4#6#顯示沒有問題。現在從#5>#4出現問題。出於某種原因,第4號房間的活動區域出現在第5號(不應該),所以現在當我點擊之前的活動區域時,我可以去#4或#6。

我已經試過這對於其他的房間「和問題不會出現在其中。我已經得出結論,它可能需要在與多條路徑連接的房間之間前後進行操作。我附加了一個視覺通路和孤立的代碼,使事情變得更簡單(數字只是房間#的)。

MouseAdapter mouseHandler = new MouseAdapter() 
     { 
      public void mouseClicked(MouseEvent e) 
      { 
      //System.out.println(e.getPoint()); 
      if(n==6)//R6 
      { 
       for(int i = 116;i<132;i++)//3a 
       { 
        if(e.getX() == i) 
        { 
         for(int j = 388;j<404;j++) 
         { 
          if(e.getY() == j) 
          { 
          n = 7;//3b 
           return; 
          } 
         } 
        } 
       } 
       for(int i = 116;i<132;i++)//2b 
        { 
         if(e.getX() == i) 
         { 
          for(int j = 308;j<324;j++) 
          { 
           if(e.getY() == j) 
           { 
           n = 4;//2a 
            return; 
           } 
          } 
         } 
        } 
       for(int i = 580;i<596;i++)//8a 
       { 
        if(e.getX() == i) 
        { 
         for(int j = 372;j<388;j++) 
         { 
          if(e.getY() == j) 
          { 
          n = 2;//8b 
           return; 
          } 
         } 
        } 
       } 
      } 
      if(n==5)//R5 
      { 
       for(int i = 220;i<268;i++)//1b 
        { 
         if(e.getX() == i) 
         { 
          for(int j = 437;j<485;j++) 
          { 
           if(e.getY() == j) 
           { 
            n = 4;//1a 
            return; 
           } 
          } 
         } 
        } 
      } 
      if(n==4)//R4 
      { 
       for(int i = 179;i<244;i++)//2a 
        { 
         if(e.getX() == i) 
         { 
          for(int j = 403;j<468;j++) 
          { 
           if(e.getY() == j) 
           { 
           n = 6;//2b 
            return; 
           } 
          } 
         } 
        } 
       for(int i = 436;i<500;i++)//1a 
         { 
          if(e.getX() == i) 
          { 
           for(int j = 403;j<468;j++) 
           { 
            if(e.getY() == j) 
            { 
            n = 5;//1b 
             return; 
            } 
           } 
          } 
         } 
       for(int i = 274;i<406;i++)//A2 
        { 
         if(e.getX() == i) 
         { 
          for(int j = 193;j<276;j++) 
          { 
           if(e.getY() == j) 
           { 
            n = 0;//A1 
            return; 
           } 
          } 
         } 
        } 
      } 
      if(n==0)//R0 
      { 
       for(int i = 459;i<493;i++)//A1 
       { 
        if(e.getX() == i) 
        { 
         for(int j = 110;j<133;j++) 
         { 
          if(e.getY() == j) 
          { 
           n = 4;//A2 
           return; 
          } 
         } 
        } 
       } 
      } 
      repaint(); 
      //http://stackoverflow.com/questions/5654208/making-a-jbutton-invisible-but-clickable 
      } 
      public void mouseMoved(MouseEvent e) 
      { 
       // = e.getPoint(); 
       //repaint(); 
      } 
     }; 

     addMouseMotionListener(mouseHandler); 
     addMouseListener(mouseHandler); 

enter image description here

+2

你必須在你的代碼中的錯誤。因爲你沒有顯示任何代碼,所以我們可以幫助你超越我。我知道我們很聰明,但不是那麼聰明。 – 2013-04-08 01:55:08

+1

給我一下,我會很快添加代碼。編輯:好吧,我補充說,如果你能看到我做錯了什麼,它會幫助我很多。 – SelfDeceit 2013-04-08 01:56:26

+2

在發佈代碼之前,您應該進行一些*嚴重的*調試。我會使用一個調試器或'println(...)'語句來顯示程序變量的狀態,試圖找出事情不正常的原因。否則,您可能會發布與您的問題無關的代碼。 – 2013-04-08 01:59:23

回答

3

你似乎是想看看是否鼠標點擊了一個區域內,你似乎想用嵌套的for循環來做到這一點。不要爲此使用for循環。如果是塊,請使用它。最簡單的方法是創建矩形,並使用其contains(Point p)方法查看鼠標是否已在其中單擊。

+1

+1即使我的評論更快;) – MadProgrammer 2013-04-08 02:03:24

+0

@MadProgrammer:是的,但你可能會創建併發布一些漂亮的代碼。這是一件好事。 – 2013-04-08 02:04:09

+0

我看了一眼if/loops並快速關閉了標籤;) – MadProgrammer 2013-04-08 02:05:29

4

這是爲了演示Rectangle的使用和Room自包含的概念。

每個Room可以具有1個或多個存在時,每個出口通向另一個房間。這實質上是一種鏈接列表。

Rectangle爲每個位置被動態計算,使得更容易呈現和控制。這個例子基本上只會顯示活動房間和連通房間。每次點擊一個房間時,該房間就會成爲活動房間(位於中間)並呈現它已經存在。

enter image description here

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.FontMetrics; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.Rectangle; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 
import java.util.HashMap; 
import java.util.Map; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class Rooms { 

    public static void main(String[] args) { 
     new Rooms(); 
    } 

    public Rooms() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { 
       } 

       JFrame frame = new JFrame("Testing"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new BorderLayout()); 
       frame.add(new RoomsPane()); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class RoomsPane extends JPanel { 

     private Room currentRoom; 

     public RoomsPane() { 

      currentRoom = new Room("0", new Dimension(100, 100), null, null); 
      currentRoom.addExit(ExitPoint.East, createRoom(1, currentRoom, ExitPoint.West)); 

      addMouseListener(new MouseAdapter() { 
       @Override 
       public void mouseClicked(MouseEvent e) { 
        if (getBounds(currentRoom).contains(e.getPoint())) { 
         // current room 
        } else { 
         for (Room room : currentRoom.getRooms()) { 
          if (getBounds(room).contains(e.getPoint())) { 
           currentRoom = room; 
           repaint(); 
           break; 
          } 
         } 
        } 
       } 
      }); 

     } 

     protected boolean chance() { 
      return Math.round(Math.random() * 1) == 0 ? false : true; 
     } 

     public Room createRoom(int count, Room parent, ExitPoint entryPoint) { 
      int width = (int) Math.round(Math.random() * 90) + 10; 
      int height = (int) Math.round(Math.random() * 90) + 10; 
      Room room = new Room(String.valueOf(count), new Dimension(width, height), parent, entryPoint); 
      if (count < 10) { 
       count++; 

       if (chance() && !room.hasExit(ExitPoint.North)) { 
        room.addExit(ExitPoint.North, createRoom(count, room, ExitPoint.South)); 
       } 
       if (chance() && !room.hasExit(ExitPoint.East)) { 
        room.addExit(ExitPoint.East, createRoom(count, room, ExitPoint.West)); 
       } 
       if (chance() && !room.hasExit(ExitPoint.South)) { 
        room.addExit(ExitPoint.South, createRoom(count, room, ExitPoint.North)); 
       } 
       if (chance() && !room.hasExit(ExitPoint.West)) { 
        room.addExit(ExitPoint.West, createRoom(count, room, ExitPoint.West)); 
       } 

      } 
      return room; 
     } 

     @Override 
     public Dimension getPreferredSize() { 
      return new Dimension(200, 200); 
     } 

     public Rectangle getBounds(Room room) { 

      Rectangle bounds = null; 
      // Can't get the bounds for a room not linked to current room 
      if (currentRoom.isLinkedTo(room) || currentRoom == room) { 
       Dimension size = currentRoom.getSize(); 
       int width = getWidth() - 1; 
       int height = getHeight() - 1; 
       int x = (width - size.width)/2; 
       int y = (height - size.height)/2; 
       if (currentRoom != room) { 

        switch (currentRoom.getExit(room)) { 
         case North: 
          y -= room.getSize().height; 
          x = (width - room.getSize().width)/2; 
          break; 
         case South: 
          y += size.height; 
          x = (width - room.getSize().width)/2; 
          break; 
         case East: 
          x += size.width; 
          y = (height - room.getSize().height)/2; 
          break; 
         case West: 
          x -= room.getSize().width; 
          y = (height - room.getSize().height)/2; 
          break; 
        } 

        size = room.getSize(); 

       } 
       bounds = new Rectangle(x, y, size.width, size.height); 
      } 
      return bounds; 

     } 

     @Override 
     protected void paintComponent(Graphics g) { 
      super.paintComponent(g); 
      Graphics2D g2d = (Graphics2D) g.create(); 

      FontMetrics fm = g2d.getFontMetrics(); 

      g2d.setColor(Color.GRAY); 
      for (Room room : currentRoom.getRooms()) { 
       Rectangle bounds = getBounds(room); 
       g2d.draw(bounds); 
       g2d.drawString(
         room.getName(), 
         bounds.x + ((bounds.width - fm.stringWidth(room.getName()))/2), 
         bounds.y + ((bounds.height - fm.getHeight())/2) + fm.getAscent()); 
      } 

      Rectangle bounds = getBounds(currentRoom); 
      g2d.setColor(Color.BLUE); 
      g2d.draw(bounds); 
      g2d.drawString(
        currentRoom.getName(), 
        bounds.x + ((bounds.width - fm.stringWidth(currentRoom.getName()))/2), 
        bounds.y + ((bounds.height - fm.getHeight())/2) + fm.getAscent()); 

      g2d.dispose(); 
     } 
    } 

    public enum ExitPoint { 

     North, 
     South, 
     East, 
     West 
    } 

    public class Room { 

     private Dimension size; 
     private Map<ExitPoint, Room> exists; 
     private String name; 

     public Room(String name, Dimension dim, Room parent, ExitPoint entryPoint) { 
      size = new Dimension(dim); 
      exists = new HashMap<>(4); 
      this.name = name; 
      if (parent != null) { 
       addExit(entryPoint, parent); 
      } 
     } 

     public String getName() { 
      return name; 
     } 

     public Dimension getSize() { 
      return size; 
     } 

     public void addExit(ExitPoint point, Room room) { 
      exists.put(point, room); 
     } 

     public boolean isLinkedTo(Room room) { 
      return exists.values().contains(room); 
     } 

     public Room[] getRooms() { 
      return exists.values().toArray(new Room[exists.size()]); 
     } 

     public boolean hasExit(ExitPoint ep) { 
      return exists.containsKey(ep); 
     } 

     public ExitPoint getExit(Room room) { 
      ExitPoint ep = null; 
      for (ExitPoint exit : exists.keySet()) { 
       if (exists.get(exit) == room) { 
        ep = exit; 
        break; 
       } 
      } 
      return ep; 
     } 
    } 
}