I am in the process of making a basic chess game using java gui (swing, awt). I found though that 1, only the board drawn in JFrame initially shows up, and 2, while the image of a piece appears when I start dragging my cursor, the board disappears and just leaves the draggable image. How can I get the image to initially be visible and not remove the board when I drag it?
import javax.swing.JFrame;
import java.awt.Graphics;
import java.awt.Color;
public class BoardGUI extends JFrame {
public BoardGUI()
{
this.setSize(1000,1000);
this.setTitle("Chess");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
PieceImage king = new PieceImage("./king.png",500,800);
this.add(king);
this.setVisible(true);
}
public void paint(Graphics g)
{
int[] swap = {1,2};
String[] rowNum = {"8","7","6","5","4","3","2","1"};
String[] ColumnLetter = {"A","B","C","D","E","F","G","H"};
int rowCount = 0;
int columnCount = 0;
for(int i=100;i<900;i+=100)
{
for(int j=100;j<900;j+=100)
{
if(i==100)
{
g.setColor(Color.black);
g.drawString(rowNum[rowCount++],i-20,j+60);
}
if(j==100)
{
g.setColor(Color.black);
g.drawString(ColumnLetter[columnCount++],i+50,j+820);
}
if(swap[0] == 1)
g.setColor(Color.white);
else
g.setColor(Color.black);
swapColor(swap);
g.fillRect(i,j,100,100);
if(j==800)
swapColor(swap);
}
}
}
private void swapColor(int[] swap)
{
int temp = swap[0];
swap[0] = swap[1];
swap[1] = temp;
}
}
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.event.MouseInputAdapter;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.Graphics;
import java.awt.Point;
public class PieceImage extends JPanel {
private ImageIcon image;
private Point prevPos;
private Point pos;
public PieceImage(String imagePath, int xPos, int yPos)
{
if(imagePath==null)
return;
this.image = new ImageIcon(imagePath);
this.pos = new Point(xPos,yPos);
ClickListener clickListner = new ClickListener();
DragListener dragListener = new DragListener();
this.addMouseListener(clickListner);
this.addMouseMotionListener(dragListener);
}
private class ClickListener extends MouseAdapter {
public void mousePressed(MouseEvent e)
{
prevPos = e.getPoint();
}
}
private class DragListener extends MouseInputAdapter {
public void mouseDragged(MouseEvent e)
{
Point temp = e.getPoint();
pos.translate((int)(temp.getX() - prevPos.getX())-50, (int)(temp.getY() - prevPos.getY())-50);
prevPos = pos;
repaint();
}
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
image.paintIcon(this, g, (int)pos.getX(), (int)pos.getY());
}
}