2016-05-30 59 views
0

我正在使用Swing繪製的圖形在Java中進行Connect 4遊戲。 我遇到的問題是,如果我點擊指定列下的按鈕將另一個圖塊添加到該列,那麼它下面的圖標消失。此外,顏色從紅色變爲黃色,但隨後保持黃色而不是變回紅色(假設每個奇怪的點擊爲紅色,直到41,黃色則每次點擊到42)。Java Swing Drawn Shapes消失

下面是代碼的片段:

import java.awt.BorderLayout; 
import java.awt.Color; 
import java.awt.Dimension; 
import java.awt.EventQueue; 
import java.awt.Font; 
import java.awt.Graphics; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
    import javax.swing.border.EmptyBorder; 
import javax.swing.JButton; 
import java.awt.event.ActionListener; 
import java.awt.event.ActionEvent; 
import java.awt.FlowLayout; 

public class Connect_4 extends JFrame { 

private JPanel contentPane; 
public int countClicks = 0; 
public boolean check = false; 
public boolean check2 = false; 
public boolean flag1; 
public boolean flag2; 
public boolean flag3; 
public boolean flag4; 
public boolean flag5; 
public boolean flag6; 
public boolean flag7; 
public int btn1Count = 0; 
public int btn2Count = 0; 
public int btn3Count = 0; 
public int btn4Count = 0; 
public int btn5Count = 0; 
public int btn6Count = 0; 
public int btn7Count = 0; 
public boolean flag; 

public static void main(String[] args) { 
    EventQueue.invokeLater(new Runnable() { 
     public void run() { 
      try { 
       Connect_4 frame = new Connect_4(); 
       frame.setVisible(true); 
      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
     } 
    }); 
} 

public void paint(Graphics g) { 
    super.paint(g); 

    g.setColor(Color.red); 
    Font font = new Font("Serif", Font.PLAIN, 20); 
    g.setFont(font); 
    g.drawString("C O N N E C T 4", 34, 47); 

    g.setColor(Color.blue); 
    g.fillRect(34, 50, 140, 120); 

    g.setColor(Color.black); 
    // ~~~~row 1~~~~ 
    g.drawRect(34, 50, 20, 20); 
    g.drawRect(54, 50, 20, 20); 
    g.drawRect(74, 50, 20, 20); 
    g.drawRect(94, 50, 20, 20); 
    g.drawRect(114, 50, 20, 20); 
    g.drawRect(134, 50, 20, 20); 
    g.drawRect(154, 50, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 52, 16, 16); 
    g.fillOval(56, 52, 16, 16); 
    g.fillOval(76, 52, 16, 16); 
    g.fillOval(96, 52, 16, 16); 
    g.fillOval(116, 52, 16, 16); 
    g.fillOval(136, 52, 16, 16); 
    g.fillOval(156, 52, 16, 16); 
    g.setColor(Color.black); 
    // ~~~~row 2~~~~ 
    g.drawRect(34, 70, 20, 20); 
    g.drawRect(54, 70, 20, 20); 
    g.drawRect(74, 70, 20, 20); 
    g.drawRect(94, 70, 20, 20); 
    g.drawRect(114, 70, 20, 20); 
    g.drawRect(134, 70, 20, 20); 
    g.drawRect(154, 70, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 72, 16, 16); 
    g.fillOval(56, 72, 16, 16); 
    g.fillOval(76, 72, 16, 16); 
    g.fillOval(96, 72, 16, 16); 
    g.fillOval(116, 72, 16, 16); 
    g.fillOval(136, 72, 16, 16); 
    g.fillOval(156, 72, 16, 16); 
    g.setColor(Color.black); 
    // ~~~~row 3~~~~ 
    g.drawRect(34, 90, 20, 20); 
    g.drawRect(54, 90, 20, 20); 
    g.drawRect(74, 90, 20, 20); 
    g.drawRect(94, 90, 20, 20); 
    g.drawRect(114, 90, 20, 20); 
    g.drawRect(134, 90, 20, 20); 
    g.drawRect(154, 90, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 92, 16, 16); 
    g.fillOval(56, 92, 16, 16); 
    g.fillOval(76, 92, 16, 16); 
    g.fillOval(96, 92, 16, 16); 
    g.fillOval(116, 92, 16, 16); 
    g.fillOval(136, 92, 16, 16); 
    g.fillOval(156, 92, 16, 16); 
    g.setColor(Color.black); 
    // ~~~~row 4~~~~ 
    g.drawRect(34, 110, 20, 20); 
    g.drawRect(54, 110, 20, 20); 
    g.drawRect(74, 110, 20, 20); 
    g.drawRect(94, 110, 20, 20); 
    g.drawRect(114, 110, 20, 20); 
    g.drawRect(134, 110, 20, 20); 
    g.drawRect(154, 110, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 112, 16, 16); 
    g.fillOval(56, 112, 16, 16); 
    g.fillOval(76, 112, 16, 16); 
    g.fillOval(96, 112, 16, 16); 
    g.fillOval(116, 112, 16, 16); 
    g.fillOval(136, 112, 16, 16); 
    g.fillOval(156, 112, 16, 16); 
    g.setColor(Color.black); 
    // ~~~~row 5~~~~ 
    g.drawRect(34, 130, 20, 20); 
    g.drawRect(54, 130, 20, 20); 
    g.drawRect(74, 130, 20, 20); 
    g.drawRect(94, 130, 20, 20); 
    g.drawRect(114, 130, 20, 20); 
    g.drawRect(134, 130, 20, 20); 
    g.drawRect(154, 130, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 132, 16, 16); 
    g.fillOval(56, 132, 16, 16); 
    g.fillOval(76, 132, 16, 16); 
    g.fillOval(96, 132, 16, 16); 
    g.fillOval(116, 132, 16, 16); 
    g.fillOval(136, 132, 16, 16); 
    g.fillOval(156, 132, 16, 16); 
    g.setColor(Color.black); 
    // ~~~~row 6~~~~ 
    g.drawRect(34, 150, 20, 20); 
    g.drawRect(54, 150, 20, 20); 
    g.drawRect(74, 150, 20, 20); 
    g.drawRect(94, 150, 20, 20); 
    g.drawRect(114, 150, 20, 20); 
    g.drawRect(134, 150, 20, 20); 
    g.drawRect(154, 150, 20, 20); 
    g.setColor(Color.white); 
    g.fillOval(36, 152, 16, 16); 
    g.fillOval(56, 152, 16, 16); 
    g.fillOval(76, 152, 16, 16); 
    g.fillOval(96, 152, 16, 16); 
    g.fillOval(116, 152, 16, 16); 
    g.fillOval(136, 152, 16, 16); 
    g.fillOval(156, 152, 16, 16); 
    g.setColor(Color.black); 
    g.drawLine(174, 170, 174, 187); 
    g.drawLine(34, 170, 34, 187); 
    g.drawLine(164, 187, 184, 187); 
    g.drawLine(24, 187, 44, 187); 



    if (flag1 == true && check == true && btn1Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 152, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 152, 16, 16); 
    } 

    if (flag1 == true && check == true && btn1Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 132, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 132, 16, 16); 
    } 

    if (flag1 == true && check == true && btn1Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 112, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 112, 16, 16); 
    } 

    if (flag1 == true && check == true && btn1Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 92, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 92, 16, 16); 
    } 

    if (flag1 == true && check == true && btn1Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 72, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 72, 16, 16); 
    } 

    if (flag1 == true && check == true && btn1Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(36, 52, 16, 16); 
    } 
    if (flag1 == true && check2 == true && btn1Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(36, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag2 == true && check == true && btn2Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 152, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 152, 16, 16); 
    } 

    if (flag2 == true && check == true && btn2Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 132, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 132, 16, 16); 
    } 

    if (flag2 == true && check == true && btn2Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 112, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 112, 16, 16); 
    } 

    if (flag2 == true && check == true && btn2Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 92, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 92, 16, 16); 
    } 

    if (flag2 == true && check == true && btn2Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 72, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 72, 16, 16); 
    } 

    if (flag2 == true && check == true && btn2Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(56, 52, 16, 16); 
    } 
    if (flag2 == true && check2 == true && btn2Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(56, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag3 == true && check == true && btn3Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 152, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 152, 16, 16); 
    } 

    if (flag3 == true && check == true && btn3Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 132, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 132, 16, 16); 
    } 

    if (flag3 == true && check == true && btn3Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 112, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 112, 16, 16); 
    } 

    if (flag3 == true && check == true && btn3Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 92, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 92, 16, 16); 
    } 

    if (flag3 == true && check == true && btn3Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 72, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 72, 16, 16); 
    } 

    if (flag3 == true && check == true && btn3Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(76, 52, 16, 16); 
    } 
    if (flag3 == true && check2 == true && btn3Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(76, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag4 == true && check == true && btn4Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 152, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 152, 16, 16); 
    } 

    if (flag4 == true && check == true && btn4Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 132, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 132, 16, 16); 
    } 

    if (flag4 == true && check == true && btn4Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 112, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 112, 16, 16); 
    } 

    if (flag4 == true && check == true && btn4Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 92, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 92, 16, 16); 
    } 

    if (flag4 == true && check == true && btn4Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 72, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 72, 16, 16); 
    } 

    if (flag4 == true && check == true && btn4Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(96, 52, 16, 16); 
    } 
    if (flag4 == true && check2 == true && btn4Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(96, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag5 == true && check == true && btn5Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 152, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 152, 16, 16); 
    } 

    if (flag5 == true && check == true && btn5Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 132, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 132, 16, 16); 
    } 

    if (flag5 == true && check == true && btn5Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 112, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 112, 16, 16); 
    } 

    if (flag5 == true && check == true && btn5Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 92, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 92, 16, 16); 
    } 

    if (flag5 == true && check == true && btn5Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 72, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 72, 16, 16); 
    } 

    if (flag5 == true && check == true && btn5Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(116, 52, 16, 16); 
    } 
    if (flag5 == true && check2 == true && btn5Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(116, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag6 == true && check == true && btn6Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 152, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 152, 16, 16); 
    } 

    if (flag6 == true && check == true && btn6Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 132, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 132, 16, 16); 
    } 

    if (flag6 == true && check == true && btn6Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 112, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 112, 16, 16); 
    } 

    if (flag6 == true && check == true && btn6Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 92, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 92, 16, 16); 
    } 

    if (flag6 == true && check == true && btn6Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 72, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 72, 16, 16); 
    } 

    if (flag6 == true && check == true && btn6Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(136, 52, 16, 16); 
    } 
    if (flag6 == true && check2 == true && btn6Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(136, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

    if (flag7 == true && check == true && btn7Count == 1) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 152, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 1) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 152, 16, 16); 
    } 

    if (flag7 == true && check == true && btn7Count == 2) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 132, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 2) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 132, 16, 16); 
    } 

    if (flag7 == true && check == true && btn7Count == 3) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 112, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 3) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 112, 16, 16); 
    } 

    if (flag7 == true && check == true && btn7Count == 4) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 92, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 4) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 92, 16, 16); 
    } 

    if (flag7 == true && check == true && btn7Count == 5) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 72, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 5) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 72, 16, 16); 
    } 

    if (flag7 == true && check == true && btn7Count == 6) 
    { 
    g.setColor(Color.red); 
    g.fillOval(156, 52, 16, 16); 
    } 
    if (flag7 == true && check2 == true && btn7Count == 6) 
    { 
    g.setColor(Color.yellow); 
    g.fillOval(156, 52, 16, 16); 
    } 

    //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

} 

public Connect_4() { 
    setTitle("Connect 4"); 
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    setBounds(100, 100, 210, 220); 
    contentPane = new JPanel(); 
    contentPane.setBorder(new EmptyBorder(5, 5, 5, 5)); 
    setContentPane(contentPane); 
    // getContentPane().setBackground(Color.RED); 
    setResizable(false); 
    contentPane.setLayout(new FlowLayout(FlowLayout.CENTER, 10, 137)); 

    JPanel panel = new JPanel(); 
    // panel.setBackground(Color.BLUE); 
    contentPane.add(panel, BorderLayout.CENTER); 

    JButton btn1 = new JButton(); 
    btn1.setPreferredSize(new Dimension(15, 10)); 
    panel.add(btn1); 
    btn1.addActionListener(new ActionListener() { 
     public void actionPerformed(ActionEvent e) { 
      countClicks++; 
      btn1Count++; 
      flag = true; 
      flag1 = true; 
      switch (countClicks) { 
      case 1: 
      case 3: 
      case 5: 
      case 7: 
      case 9: 
      case 11: 
      case 13: 
      case 15: 
      case 17: 
      case 19: 
      case 21: 
      case 23: 
      case 25: 
      case 27: 
      case 29: 
      case 31: 
      case 33: 
      case 35: 
      case 37: 
      case 39: 
      case 41: 
       check = true; 
       repaint(); 
       break; 
      case 2: 
      case 4: 
      case 6: 
      case 8: 
      case 10: 
      case 12: 
      case 14: 
      case 16: 
      case 18: 
      case 20: 
      case 22: 
      case 24: 
      case 26: 
      case 28: 
      case 30: 
      case 32: 
      case 34: 
      case 36: 
      case 38: 
      case 40: 
      case 42: 
       check2 = true; 
       repaint(); 
       break; 

      } 
     } 

    }); 

我不知道爲什麼在列前面的瓷磚消失,也沒有如何讓它停下來。問題重繪()?在paint方法中是if語句嗎?請幫忙。

+0

repaint()不是你應該調用的函數 –

+0

應該用什麼來代替? –

回答

1

Paint() - 此方法包含繪製此組件的說明。實際上,在Swing中,您應該更換paintComponent()而不是paint(),因爲油漆調用paintBorder(),paintComponent()paintChildren()。您不應該直接調用此方法,而應該調用repaint()。

repaint() - 此方法不能被覆蓋。它控制着update() -> paint()週期。你應該調用這個方法來獲得一個組件來重繪自己。如果你已經做了任何改變組件的外觀,但不是它的大小(如改變顏色,動畫等)然後調用這個方法。

validate() - 這告訴組件重新放置並重新繪製自己。如果你已經做了任何改變組件或它的任何孩子的大小(添加,刪除,調整兒童大小),你應該調用這種方法...我認爲調用revalidate()優先於在Swing中調用validate(),但.. 。

update() - 此方法負責清除組件並調用paint()。再次,您應該調用repaint()而不是直接調用此方法...如果您需要在動畫中執行快速更新,則應該重寫此方法以調用paint()方法...

updateUI() - 調用此方法方法,如果在組件可見後更改了組件的可插入外觀感覺。

注意:您在程序中使用switch case的方式並不是一個好的實現,使用變量(計數器)並按用戶單擊增加,然後使用if/while條件進一步實現。

+0

我討厭挑剔,但是如何將其中的一個實現到我的代碼中,以便在另一個下面繪製的圓圈不會消失? –

+0

因爲我正在使用repaint(),並且我覺得這是個問題,因爲我不想在下面的另一個INSTEAD的頂部畫一個圓,我想繪製一個以便它添加它。 –

+0

我有這個給你,如果它有幫助http://stackoverflow.com/questions/20387349/change-color-of-jcomponent-after-paintcomponent-has-finished –