2017-08-05 41 views
0

我正在研究繪製網格單元格的程序。每個單元都有指定的顏色和四種狀態之一:通過點擊它們來更改網格單元格的顏色

  1. STARTCELL //marked by yellow color 
    
  2. ENDCELL //marked by red color 
    
  3. EMPTYCELL //marked by white color 
    
  4. BLOCKEDCELL // marked by black color 
    

在開始的時候有一個黃色的單元格,一個紅細胞和其餘的是白色的。

我希望能夠通過點擊和一些研究之後改變細胞的顏色和狀態我找到了解決辦法:

@Override 
public void mouseClicked(MouseEvent e) { 
    // TODO Auto-generated method stub 
    for (Cell item : gridCells) { 
     if (item.getCellShape().contains(e.getPoint())&& item.getCellState()==CellState.EMPTYCELL) { 
      item.setCellColor(mouseColor); 
      if(mouseColor==startCellColor){ 
       item.setCellState(CellState.STARTCELL); 
      }else if(mouseColor==endCellColor){ 
       item.setCellState(CellState.ENDCELL); 
      }else{ 
       item.setCellState(CellState.BLOCKEDCELL); 
      } 
     } 
    } 
    repaint(); 
} 

唯一的問題是,應該只有一個STARTCELL 和一個ENDCELL在當時我無法找到一種方法來正確更改未點擊單元格的狀態。

我試過很多次,並結束了與此:

@Override 
public void mouseClicked(MouseEvent e) { 
    // TODO Auto-generated method stub 
    for (Cell item : gridCells) { 
     if (item.getCellShape().contains(e.getPoint())&& item.getCellState()==CellState.EMPTYCELL) { 
      item.setCellColor(mouseColor); 
      if(mouseColor==startCellColor){ 
        gridCells.get(numberOfStartCell-1).setCellColor(cellColor); 
        gridCells.get(numberOfStartCell-1).setCellState(CellState.EMPTYCELL); 
        numberOfStartCell=item.getNumberOfCell(); 
        item.setCellState(CellState.STARTCELL); 
      }else if(mouseColor==endCellColor){ 
       item.setCellState(CellState.ENDCELL); 
      }else{ 
       item.setCellState(CellState.BLOCKEDCELL); 
      } 
      areaTest.setText(Integer.toString(numberOfStartCell)); 
     } 
    } 
    repaint(); 
} 

遺憾的是它並沒有正常工作。第一次點擊後,新單元格的顏色變爲黃色,舊單元格變爲白色,變量numberOfStartCell的值也發生變化。但是在第二次點擊等之後,唯一改變的部分是numberOfStartCell

這裏是MCV: 我猜沒有什麼可以解釋的前兩類。

Class Main

import java.awt.EventQueue; 

public class Main { 
    public static void main(String[] args) { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       new MyFrame(); 
      } 
     }); 
    } 
} 

Class MyFrame

import java.awt.Dimension; 
import java.awt.Toolkit; 

import javax.swing.JFrame; 

public class MyFrame extends JFrame { 

    int width = 750; 
    int height = 750; 
    MyPanel panel; 

    public MyFrame() { 
     super("Przeszukiwanie"); 
     setSize(width,height); 
     setResizable(false); 

     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); 
     double screenWidth = screenSize.getWidth(); 
     double ScreenHeight = screenSize.getHeight(); 
     int x = ((int)screenWidth-width)/2; 
     int y = ((int)ScreenHeight-height)/2; 

     setLocation(x,y); 
     setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
     setVisible(true); 
     panel=new MyPanel(); 
     add(panel); 
     pack(); 
    } 
} 

Class MyPanel負責事件處理和圖形用戶界面。

import java.awt.BorderLayout; 
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 

import javax.swing.ButtonGroup; 
import javax.swing.JButton; 
import javax.swing.JLabel; 
import javax.swing.JPanel; 
import javax.swing.JRadioButton; 
import javax.swing.JTextField; 

public class MyPanel extends JPanel implements ActionListener{ 

    private JButton createButton; 

    private ButtonGroup cellColorGroup; 
    private JRadioButton startingNode; 
    private JRadioButton endingNode; 
    private JRadioButton obstacleNode; 

    private JTextField rows; 
    private JTextField columns; 

    private JLabel labelRows; 
    private JLabel labelColumns; 

    private String strRowsField; 
    private String strColumnsField; 

    private JPanel panelButtons; 
    private GridPanel panelGrid; 
    private JPanel panelSettings; 

    public MyPanel(){ 

     setPreferredSize(new Dimension(700, 600)); 
     setLayout(new BorderLayout()); 

     strRowsField="10"; 
     strColumnsField="10"; 

     panelButtons=new JPanel(); 
     panelSettings=new JPanel(); 
     panelGrid=new GridPanel(Integer.parseInt(strColumnsField),Integer.parseInt(strRowsField)); 

     panelSettings.setLayout(new GridLayout(6,2)); 

     createButton= new JButton("Create"); 

     cellColorGroup=new ButtonGroup(); 
     startingNode=new JRadioButton("Strating Node"); 
     endingNode=new JRadioButton("Ending Node"); 
     obstacleNode=new JRadioButton("Remove/Add Obstacle", true); 

     cellColorGroup.add(startingNode); 
     cellColorGroup.add(endingNode); 
     cellColorGroup.add(obstacleNode); 

     createButton.addActionListener(this); 
     startingNode.addActionListener(this); 
     endingNode.addActionListener(this); 
     obstacleNode.addActionListener(this); 

     columns=new JTextField(strColumnsField,2); 
     rows=new JTextField(strRowsField,2); 
     labelRows=new JLabel("Number of rows"); 
     labelColumns= new JLabel("Number of columns"); 

     panelButtons.add(createButton); 

     panelSettings.add(labelColumns); 
     panelSettings.add(columns); 
     panelSettings.add(labelRows); 
     panelSettings.add(rows); 
     panelSettings.add(startingNode); 
     panelSettings.add(endingNode); 
     panelSettings.add(obstacleNode); 

     add(panelButtons,BorderLayout.SOUTH); 
     add(panelGrid,BorderLayout.WEST); 
     add(panelSettings,BorderLayout.EAST); 

    } 
    @Override 
    public void actionPerformed(ActionEvent e) { 
     if (e.getSource() == createButton) { 
      panelGrid.setcNodes(Integer.parseInt(columns.getText())); 
      panelGrid.setrNodes(Integer.parseInt(rows.getText())); 
      panelGrid.getGridCells().clear(); 
      panelGrid.repaint(); 
     }else if(e.getSource() == startingNode){ 
      panelGrid.setMouseColor(panelGrid.getStartCellColor()); 

     }else if(e.getSource() == endingNode){ 
      panelGrid.setMouseColor(panelGrid.getEndCellColor()); 

     }else if(e.getSource() == obstacleNode){ 
      panelGrid.setMouseColor(panelGrid.getObstacleCellColor()); 
     } 
    } 
} 

Class GridPanel負責繪製節點,邊和單元格。

import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.Graphics; 
import java.awt.Graphics2D; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 
import java.awt.event.MouseMotionListener; 
import java.awt.geom.Line2D; 
import java.awt.geom.Rectangle2D; 
import java.util.ArrayList; 

import javax.swing.JPanel; 

public class GridPanel extends JPanel implements MouseListener, MouseMotionListener { 

    private int cNodes;//Number of nodes across 
    private int rNodes;//Number of nodes along 
    private int nodeX;//X coordinate of the first node 
    private int nodeY;//Y coordinate of the first node 
    private int width=330; 
    private int height=330; 
    private int circleX;//Center of circle X 
    private int circleY;//Center of circle Y 
    private Color cellColor; 
    private Color startCellColor; 
    private Color endCellColor; 
    private Color obstacleCellColor; 
    private Color mouseColor; 
    private int numberOfStartCell; 

    private ArrayList<Cell> gridCells; 
    public GridPanel(int cNodes, int rNodes){ 

     setPreferredSize(new Dimension(width,height)); 

     this.cNodes=cNodes; 
     this.rNodes=rNodes; 

     if(cNodes>rNodes){ 
      nodeX=width/(cNodes+1);//Calculation of the x coordinate value of the first node 
      nodeY=height/(cNodes+1);//Calculation of the y coordinate value of the first node 
     }else{ 
      nodeX=width/(rNodes+1); 
      nodeY=height/(rNodes+1); 
     } 

     circleX=nodeX; 
     circleY=nodeY; 

     cellColor=Color.WHITE; 
     startCellColor=Color.YELLOW; 
     endCellColor=Color.RED; 
     obstacleCellColor=Color.BLACK; 

     gridCells=new ArrayList<Cell>(); 

     addMouseListener(this); 
     addMouseMotionListener(this); 

    } 

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

    } 

    public void drawCells(Graphics2D g2d){ 
     int number=0; 
     int c=0; 
     for(int i=0; i<rNodes; i++){ 
      for(int j=0; j<cNodes; j++){ 
       number++; 
       if(i==0 && j==cNodes-1){ 
        gridCells.add(new Cell(circleX,circleY,nodeX,endCellColor,new Rectangle2D.Double(circleX-(nodeX/2),circleY-(nodeX/2),nodeX,nodeX),CellState.ENDCELL,number)); 

       } 
       else if(i==rNodes-1 && j==0){ 
        gridCells.add(new Cell(circleX,circleY,nodeX,startCellColor,new Rectangle2D.Double(circleX-(nodeX/2),circleY-(nodeX/2),nodeX,nodeX),CellState.STARTCELL,number)); 
       numberOfStartCell=number; 
       } 
       else { 
        gridCells.add(new Cell(circleX,circleY,nodeX,cellColor,new Rectangle2D.Double(circleX-(nodeX/2),circleY-(nodeX/2),nodeX,nodeX),CellState.EMPTYCELL,number)); 
       } 
       g2d.setPaint(gridCells.get(c).getCellColor()); 
       g2d.fill(gridCells.get(c).getCellShape()); 
       g2d.setPaint(Color.BLACK); 
       g2d.draw(gridCells.get(c).getCellShape()); 
       if(j<(cNodes-1)){ 
        circleX+=nodeX; 
       } 
       c++; 
      } 
      circleX=nodeX; 
      if(i<(rNodes-1)){ 
       circleY+=nodeY; 
      } 
     } 
     circleX=nodeX; 
     circleY=nodeY; 
    } 

    public void setMouseColor(Color mouseColor) { 
     this.mouseColor = mouseColor; 
    } 

    public void setcNodes(int cNodes) { 
     this.cNodes = cNodes; 
    } 

    public void setrNodes(int rNodes) { 
     this.rNodes = rNodes; 
    } 

    public Color getObstacleCellColor() { 
     return obstacleCellColor; 
    } 

    public Color getStartCellColor() { 
     return startCellColor; 
    } 

    public Color getEndCellColor() { 
     return endCellColor; 
    } 

    public ArrayList<Cell> getGridCells() { 
     return gridCells; 
    } 

    @Override 
    public void mouseDragged(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mouseMoved(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mouseClicked(MouseEvent e) { 
     // TODO Auto-generated method stub 
     for (Cell item : gridCells) { 
      if (item.getCellShape().contains(e.getPoint())&& item.getCellState()==CellState.EMPTYCELL) { 
       item.setCellColor(mouseColor); 
       if(mouseColor==startCellColor){ 
         gridCells.get(numberOfStartCell-1).setCellColor(cellColor); 
         gridCells.get(numberOfStartCell-1).setCellState(CellState.EMPTYCELL); 
         numberOfStartCell=item.getNumberOfCell(); 
         item.setCellState(CellState.STARTCELL); 
       }else if(mouseColor==endCellColor){ 
        item.setCellState(CellState.ENDCELL); 
       }else{ 
        item.setCellState(CellState.BLOCKEDCELL); 
       } 
      } 
     } 
     repaint(); 
    } 

    @Override 
    public void mouseEntered(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mouseExited(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mousePressed(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 

    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     // TODO Auto-generated method stub 
    } 
} 

Class Cell有關於單元格(狀態,顏色,編號..)的信息。

import java.awt.Color; 
    import java.awt.Rectangle; 
    import java.awt.Shape; 
    import java.awt.geom.AffineTransform; 
    import java.awt.geom.PathIterator; 
    import java.awt.geom.Point2D; 
    import java.awt.geom.Rectangle2D; 

    public class Cell implements Shape{ 

     private int centerX; 
     private int centerY; 
     private double side; 
     private Color cellColor; 
     private double x; 
     private double y; 
     private Shape cellShape; 
     private CellState cellState; 
     private int numberOfCell; 

     public Cell(int centerX, int centerY, int side, Color cellColor,Shape cellShape, CellState cellState,int numberOfCell){ 
      super(); 
      this.centerX=centerX; 
      this.centerY=centerY; 
      this.side=side; 
      this.cellColor=cellColor; 
      this.cellShape=cellShape; 
      this.cellState=cellState; 
      this.numberOfCell=numberOfCell; 
     } 
    public Shape getCellShape() { 
     return cellShape; 
    } 

    public int getNumberOfCell() { 
     return numberOfCell; 
    } 

    public Color getCellColor() { 
     return cellColor; 
    } 

    public void setCellColor(Color cellColor) { 
     this.cellColor = cellColor; 
    } 

    public void setCellShape(Shape cellShape) { 
     this.cellShape = cellShape; 
    } 

    public double getSide() { 
     return side; 
    } 
    public double getX() { 
     return x; 
    } 
    public double getY() { 
     return y; 
    } 
    public CellState getCellState() { 
     return cellState; 
    } 

    public void setCellState(CellState cellState) { 
     this.cellState = cellState; 
    } 

    @Override 
    public boolean contains(Point2D p) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean contains(Rectangle2D r) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean contains(double x, double y) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean contains(double x, double y, double w, double h) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public Rectangle getBounds() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public Rectangle2D getBounds2D() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public PathIterator getPathIterator(AffineTransform at) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public PathIterator getPathIterator(AffineTransform at, double flatness) { 
     // TODO Auto-generated method stub 
     return null; 
    } 
    @Override 
    public boolean intersects(Rectangle2D r) { 
     // TODO Auto-generated method stub 
     return false; 
    } 
    @Override 
    public boolean intersects(double x, double y, double w, double h) { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

Class CellState

public enum CellState { 

    STARTCELL,ENDCELL,EMPTYCELL,BLOCKEDCELL; 
} 
+1

建議:1)嘗試爲您的程序使用MVC結構,2)將代碼提取到有效的[mcve]中,並在此處發佈該代碼,以便我們可以編譯和運行它,首先體驗問題,明白它。是的,這是要求你付出額外的努力,但是這會讓你的問題更容易回答,並且從長遠來看幫助你。 –

+0

.............你好? –

+0

感謝您的意見。我會盡快添加MCVE。問候。 –

回答

0

我不能給一個引經據典的答案,沒有你MCVE,但我可以給你一個大的建議。請注意,選擇空單元格和阻塞單元格在邏輯上與選擇開始和結束單元格非常不同,因此您的代碼應以非常不同的方式完成這些任務。我要做的是在鼠標監聽器中,檢查按下哪個按鈕。如果左按鈕(標準按鈕),切換單元格阻塞/空狀態(但只有當它不是開始或結束單元格時)。如果按下鼠標右鍵(或者如果沒有鼠標按鍵,則爲alt-mouse),然後顯示一個彈出菜單以允許用戶選擇單元格作爲開始或結束單元格。用於選擇開始或結束的代碼將與空白/塊切換代碼分開,將首先遍歷模型,清除以前的開始或結束單元格,然後進行設置。

如果您想要一個更好的更完整的解決方案,請再次將您的有效Minimal, Complete, and Verifiable example代碼發佈到您的問題(不在鏈接中)。

好運。

相關問題