2012-04-08 14 views
2

なぜグリッドは更新されませんか? ボタンは行と列を編集し、Grid mzeのセルを更新しますが、そうではありません。 +ボタンをHに合わせると、mzeは26x25グリッドに更新されますが、何も起こらず、コンソールにColumns + 1だけが表示されます。キャンバスは再ペイントするボタンをクリックしても更新されません

 import javax.swing.*; 
import java.awt.*; 
import javax.swing.GroupLayout.*; 
import javax.swing.LayoutStyle.ComponentPlacement; 
import javax.swing.border.LineBorder; 
import javax.swing.border.MatteBorder; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 

public class csc extends JFrame{ 

    public csc(String s){ 
    super(s); 
    setSize(800,600); 

    //code generated from eclipse 
    JPanel panel = new JPanel(); 
    panel.setBackground(SystemColor.control); 
    GroupLayout groupLayout = new GroupLayout(getContentPane()); 
    groupLayout.setHorizontalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(panel, GroupLayout.DEFAULT_SIZE, 774, Short.MAX_VALUE) 
       .addContainerGap()) 
    ); 
    groupLayout.setVerticalGroup(
     groupLayout.createParallelGroup(Alignment.LEADING) 
      .addGroup(groupLayout.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(panel, GroupLayout.DEFAULT_SIZE, 550, Short.MAX_VALUE) 
       .addContainerGap()) 
    ); 

    JPanel scCont = new JPanel(); 
    scCont.setBorder(new MatteBorder(0, 0, 1, 1, (Color) new Color(0, 0, 0))); 

    JPanel cplCont = new JPanel(); 
    GroupLayout gl_panel = new GroupLayout(panel); 
    gl_panel.setHorizontalGroup(
     gl_panel.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_panel.createSequentialGroup() 
       .addGap(27) 
       .addComponent(scCont, GroupLayout.PREFERRED_SIZE, 475, GroupLayout.PREFERRED_SIZE) 
       .addGap(18) 
       .addComponent(cplCont, GroupLayout.PREFERRED_SIZE, 235, GroupLayout.PREFERRED_SIZE) 
       .addContainerGap(19, Short.MAX_VALUE)) 
    ); 
    gl_panel.setVerticalGroup(
     gl_panel.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_panel.createSequentialGroup() 
       .addGroup(gl_panel.createParallelGroup(Alignment.LEADING) 
        .addGroup(gl_panel.createSequentialGroup() 
         .addGap(131) 
         .addComponent(cplCont, GroupLayout.PREFERRED_SIZE, 249, GroupLayout.PREFERRED_SIZE)) 
        .addGroup(gl_panel.createSequentialGroup() 
         .addGap(35) 
         .addComponent(scCont, GroupLayout.PREFERRED_SIZE, 480, GroupLayout.PREFERRED_SIZE))) 
       .addContainerGap(35, Short.MAX_VALUE)) 
    ); 
    Grid mze = new Grid(4,4,400,400); 
    cscPanel cpl = new cscPanel(this,mze); 
    scCont.setLayout(new BorderLayout(0, 0)); 
    mze = new Grid(cpl.rows,cpl.cols,400,400); 
    mze.setForeground(SystemColor.desktop); 
    scCont.add(mze); 

    GroupLayout gl_cplCont = new GroupLayout(cplCont); 
    gl_cplCont.setHorizontalGroup(
     gl_cplCont.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_cplCont.createSequentialGroup() 
       .addContainerGap() 
       .addComponent(cpl, GroupLayout.DEFAULT_SIZE, 215, Short.MAX_VALUE) 
       .addContainerGap()) 
    ); 
    gl_cplCont.setVerticalGroup(
     gl_cplCont.createParallelGroup(Alignment.LEADING) 
      .addGroup(gl_cplCont.createSequentialGroup() 
       .addContainerGap(GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE) 
       .addComponent(cpl, GroupLayout.PREFERRED_SIZE, 419, GroupLayout.PREFERRED_SIZE)) 
    ); 
    cplCont.setLayout(gl_cplCont); 
    panel.setLayout(gl_panel); 
    getContentPane().setLayout(groupLayout); 
    //end of eclipse generated code 
    pack(); 
    setResizable(false); 
    setLocationRelativeTo(null); 
    setDefaultCloseOperation(EXIT_ON_CLOSE);  
    } 

    public static void main(String[] args){ 
    csc MakeMaze = new csc("Catch Torchic!"); 
    MakeMaze.setVisible(true); 

    } 
} 

class cscPanel extends JPanel{ 
    static final int MIN_CELLS = 3; 
    static final int MAX_W_CELLS = 50; 
    static final int MAX_H_CELLS = 50; 
    static final int MAX_DELAY = 500; 
    static final int MIN_DELAY = 5; 
    int rows = 25; int cols = 25; private int genDelay = 50; private int solveDelay = 50; 
    JButton bGen; 
    JButton bmm; 
    JButton bSolve; 
    JButton bLoad; 
    JButton bRowsPlus; 
    JButton bRowsMinus; 
    JButton bColsPlus; 
    JButton bColsMinus; 
    private TextField tfRows; 
    private TextField tfCols; 
    private JPanel rowsPanel; 
    private JPanel colsPanel; 
    private JPanel rowsBtnPanel; 
    private JPanel colsBtnPanel; 
    private Grid mze; 
    private csc Fr; 
    private JPanel p = new JPanel(); 
    private cscButtonHandler handler; 

    cscPanel(csc Fr, Grid mze) { 
    this.Fr = Fr; 
    this.mze = mze; 

    setLayout(new GridLayout(11, 1, 0, 3)); 

    this.rowsBtnPanel = new JPanel(); 
    this.colsBtnPanel = new JPanel(); 
    this.rowsBtnPanel.setLayout(new GridLayout(1, 2)); 
    this.colsBtnPanel.setLayout(new GridLayout(1, 2)); 
    this.rowsBtnPanel.add(this.bRowsMinus = new JButton("-")); 
    this.rowsBtnPanel.add(this.bRowsPlus = new JButton("+")); 
    this.colsBtnPanel.add(this.bColsMinus = new JButton("-")); 
    this.colsBtnPanel.add(this.bColsPlus = new JButton("+")); 
    this.rowsPanel = new JPanel(); 
    this.rowsPanel.setLayout(new BorderLayout(2, 0)); 
    this.rowsPanel.add("West", new Label("W")); 
    this.rowsPanel.add("Center", this.tfRows = new TextField(3)); 
    this.rowsPanel.add("East", this.rowsBtnPanel); 
    this.colsPanel = new JPanel(); 
    this.colsPanel.setLayout(new BorderLayout(2, 0)); 
    this.colsPanel.add("West", new Label("H")); 
    this.colsPanel.add("Center", this.tfCols = new TextField(3)); 
    this.colsPanel.add("East", this.colsBtnPanel); 
    this.tfRows.setEditable(false); 
    this.tfCols.setEditable(false); 

    add(this.bLoad = new JButton("LOAD")); 
    add(this.rowsPanel); 
    add(this.colsPanel); 
    add(this.bGen = new JButton("RANDOM")); 
    add(this.bSolve = new JButton("SOLVE")); 
    add(this.bmm = new JButton("MAIN MENU")); 

    this.tfRows.setText(Integer.toString(this.rows)); 
    this.tfCols.setText(Integer.toString(this.cols)); 
    this.bSolve.setEnabled(false); 

    handler = new cscButtonHandler(Fr,this,mze); 
     bGen.addActionListener(handler); 
     bmm.addActionListener(handler); 
     bSolve.addActionListener(handler); 
     bLoad.addActionListener(handler); 
     bRowsPlus.addActionListener(handler); 
     bRowsMinus.addActionListener(handler); 
     bColsPlus.addActionListener(handler); 
     bColsMinus.addActionListener(handler); 
    } 

    void setPlusMinusEnable() { 
    this.bRowsPlus.setEnabled(this.rows < 50); 

    this.bRowsMinus.setEnabled(this.rows > 3); 

    this.bColsPlus.setEnabled(this.cols < 50); 

    this.bColsMinus.setEnabled(this.cols > 3); 
    } 

    void setGenEnable(boolean paramBoolean){ 
    this.bGen.setEnabled(paramBoolean); } 

    void setSolveEnable(boolean paramBoolean) { this.bSolve.setEnabled(paramBoolean); } 
} 

class cscButtonHandler implements ActionListener{ 
    csc a; 
    cscPanel b; 
    Grid c; 

    public cscButtonHandler(csc a,cscPanel b, Grid c){ 
     this.a = a; 
     this.b = b; 
     this.c = c; 
    } 

    public void actionPerformed(ActionEvent e){ 
     if(e.getSource() == b.bmm) 
      {System.out.println("Back to MainMenu");} 
     else if(e.getSource() == b.bSolve) 
      {System.out.println("Solve Maze");} 
     else if(e.getSource() == b.bLoad) 
      {System.out.println("Open Load Dialog");} 
     else if(e.getSource() == b.bRowsPlus) 
      {System.out.println("Rows + 1"); 
      b.rows++; 
      c.repaint(b.rows,b.cols,400,400);} 
     else if(e.getSource() == b.bRowsMinus) 
      {System.out.println("Rows - 1"); 
      b.rows--; 
      c.repaint(b.rows,b.cols,400,400);} 
     else if(e.getSource() == b.bColsPlus) 
      {System.out.println("Columns + 1"); 
      b.cols++; 
      c.repaint(b.rows,b.cols,400,400);} 
     else if(e.getSource() == b.bColsMinus) 
      {System.out.println("Columns - 1"); 
      b.cols--; 
      c.repaint(b.rows,b.cols,400,400);} 
     else if(e.getSource() == b.bGen) 
      {System.out.println("Generating Randomly");} 
    } 

} 
class Grid extends Canvas{ 

    Cell[][] maze; 
    int rows; 
    int cols; 

    public Grid(int rows, int cols, int h, int w) { 
     this.rows = rows; 
     this.cols = cols; 
     maze = new Cell[rows][cols]; 
     setPreferredSize(new Dimension(h,w)); 
    } 

    public void paint(Graphics g) { 
     int k; 
     double width = getSize().width; 
     double height = getSize().height; 

     double htOfRow = height/(rows); 
     for (k = 0; k < rows; k++) 
      g.drawLine(0, (int) (k * htOfRow) , (int) width, (int) (k * htOfRow)); 

     double wdOfRow = width/(cols); 
     for (k = 0; k < cols; k++) 
      g.drawLine((int) (k*wdOfRow) , 0,(int) (k*wdOfRow) , (int) height); 
    } 
} 

class Cell {} 
+2

1)SwingとAWTコンポーネントを混在させないでください。 2)すぐにより良い助けを得るために、[SSCCE](http://sscce.org/)を投稿してください。 (300 +行のコードは 'S'hortではありません。)3)質問ごとに1つの質問をするのがよい。 –

+1

私はdown_votingの理由がないと思う – mKorbel

+0

大丈夫、コードショットを作るだろう; –

答えて

0

あり、このコードを持ついくつかの問題がありますが、私が見ることができる再描画を有する少なくとも一つの問題は、実際にrepaintは、ピクセル座標を受け入れるときrepaintへの呼び出しは、グリッドセル座標どのように見えるかを渡しているということです。

+0

私は再描画を、私が新しいグリッドの作成に使用したのと同じパラメータを渡して呼び出しました。 –

+0

'cscPanel'は' invalidate() 'をいつ呼び出すべきかを理想的に決めるべきです。 –

+0

私はあなたの話しを私に伝えません。 –

関連する問題