Ich mache ein Spiel von Connect 4 in Java mit Swing malte Grafiken. Das Problem, das ich habe, ist, dass, wenn ich auf eine Schaltfläche unter einer bestimmten Spalte klicke, um eine weitere Kachel zu dieser Spalte hinzuzufügen, die darunterliegende verschwindet. Außerdem wechselt die Farbe von Rot zu Gelb, bleibt dann aber gelb, anstatt zurück zu Rot zu wechseln (bei jedem ungeraden Klick sollte sie rot sein, bis 41 und bei jedem gleichmäßigen Klick bis 42). HierJava Swing Drawn Shapes verschwinden
ist ein Clip des Codes:
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;
}
}
});
Ich weiß nicht, warum die vorherige Kachel in einer Säule verschwindet auch nicht, wie es aufhören zu machen. Ist das Problem repaint()? Sind es die if-Anweisungen in der Paint-Methode? Bitte helfen Sie.
Repaint() ist nicht die Funktion, die Sie anrufen sollten –
Was sollte ich stattdessen verwenden? –