I decided to stop making my calculator application for and decided to stick with Java for now. I'm trying to draw graphics for a Connect 4 game. I did a Tic-Tac-Toe game and actually implemented an A.I. without help so I wanted to tackle on something easier than checkers, but harder than Tic-Tac-Toe. The problem is that when I try to invoke the paint() method, nothing seems to appear.
Note: You need the Control class because the Control has the main event and will not executes without it.
Why isn't this working? Did I forget to declare something?
Code:
import javax.swing.*;
import java.awt.*;
import javax.swing.JPanel;
public class Connect4 {
Control control;
Drawings drawings = new Drawings();
JFrame window = new JFrame("Connect 4");
//Row 1
JPanel row1 = new JPanel();
JCheckBox AI = new JCheckBox("AI");
JButton start = new JButton("Start");
//Row 2
JPanel row2 = new JPanel();
JButton[] rowButton = new JButton[7];
Dimension rowButtonSize = new Dimension(32,32);
public Connect4(Control control)
{
this.control = control;
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setSize(300,365);
GridLayout layout = new GridLayout(9,1,1,1);
window.setLayout(layout);
FlowLayout layout1 = new FlowLayout();
row1.setLayout(layout1);
row1.add(AI);
row1.add(start);
window.add(row1);
FlowLayout layout2 = new FlowLayout();
row2.setLayout(layout2);
for(int x = 0; x<7;x++)
{
rowButton[x] = new JButton(Integer.toString(x+1));
row2.add(rowButton[x]);
rowButton[x].setPreferredSize(rowButtonSize);
rowButton[x].setEnabled(false);
}
window.add(row2);
window.add(drawings);
drawings.repaint();
window.setVisible(true);
}
}
class Drawings extends JPanel
{
//Paint
int changingX = 20;
int changingY = 100;
public Drawings()
{
}
public void paint(Graphics g)
{
super.paint(g);
g.setColor(Color.black);
g.drawLine(changingX, 100, changingX, 350);
}
public void paintComponent(Graphics graphic)
{
Graphics2D graphic2d = (Graphics2D) graphic;
graphic2d.setColor(Color.black);
graphic2d.drawLine(changingX, 100, changingX, 350);
}
}
Note: You need the Control class because the Control has the main event and will not executes without it.
Code:
public class Control {
Connect4 game = null;
public Control(Connect4 game)
{
game = new Connect4(this);
this.game=game;
}
public static void main(String[] args) {
Control control = new Control(null);
}
}
Why isn't this working? Did I forget to declare something?