I am working on a program that allows the user to place pieces on a Go board, and updates to place the pieces on the nearest intersection to the user's click in a GUI. I've figured I use the repaint() method for this, (the math part of it should already be working) but I've struggled to manage to get anything actually updated on the canvas at all past its initial creation. Most forms of repaint I've seen online use either JPanel or Applets instead of Canvas, or paintComponent instead of paint, but using both regular paint and canvas is the only way I've managed to get the board to even initially be created. My code is as follows, and, for clarity, my exact problem is that the repaint call seemingly just does absolutely nothing.
mport java.io.IOException;
import java.util.EventListener;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.util.ArrayList;
import java.util.Random;
import java.awt.Graphics;
import java.applet.Applet;
import javax.swing.JButton;
import javax.swing.JFrame;
import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.GridLayout;
import java.awt.MouseInfo;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.*;
public class GoGui extends Canvas implements MouseListener{
public int myX=21;
public int myY=21;
public ArrayList <newPiece> pieces=new ArrayList<newPiece>();
public static void main(String[] args) {
GoGui myGui=new GoGui();
JFrame frame = new JFrame("Go Database");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(50, 50);
frame.setSize(800, 800);
myGui.setSize(800, 800);
Canvas canvas = new GoGui();
canvas.setSize(800, 800);
Color TAN = new Color(255,255,170);
frame.add(canvas);
canvas.setBackground(TAN);
canvas.addMouseListener(myGui);
frame.pack();
frame.setVisible(true);
}
public void paint(Graphics g) {
g.setColor(Color.BLACK);
int varcoor=20;
while(varcoor<781) {
g.drawLine(20, varcoor, 780, varcoor);
varcoor+=40;
}
varcoor=20;
while(varcoor<781) {
g.drawLine(varcoor, 20, varcoor, 780);
varcoor+=40;
}
g.fillOval(myX-20, myY-20, myX+20, myY+20);
}
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
myCoords=MouseInfo.getPointerInfo().getLocation();
myX=myCoords.x;
myY=myCoords.y;
placePiece((double) myX,(double) myY);
}
public void placePiece(double littleX, double littleY) {
littleX=(littleX-70)/40;
int bigX=(int) Math.round(littleX);
if(bigX<1) {
bigX=1;
}
if(bigX>18) {
bigX=18;
}
littleY=(littleY-70)/40;
int bigY=(int) Math.round(littleY);
if(bigY<1) {
bigY=1;
}
if(bigY>18) {
bigY=18;
}
myX=(bigX*40)+20;
myY=(bigY*40)+20;
repaint();
}
}
If anyone has experience with this specific Java concept and could point me in the right direction, it would be greatly appreciated.