I'm trying to replicate an existing game for learning purposes. The code below creates a JFrame with squares that will be filled with labels and images, however, the "Start" label seems to replicate itself. I have some experience with Java, but I'm still a student. (Nearly no experience with Swing). I added the label to the frame instead of the panel because the squares I drew hide the label. Thanks :D
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Test{
public static void main(String[] args){
JFrame frame = new JFrame("Miau");
MyPanel panel = new MyPanel();
frame.setVisible(true);
frame.setSize(600,600);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.add(panel);
frame.setVisible(true);
JLabel labelstart = new JLabel("Start");
frame.add(labelstart);
labelstart.setLocation(100, 100);
labelstart.setSize(30,14);
}
}
class MyPanel extends JPanel {
public void paint(Graphics g) {
g.setColor(Color.black);
//g.fillRect(10,10,570,100);
int posx = 10;
int posy = 120;
g.drawRect(10,10,570,100);
g.drawRect(posx,posy,570,430);
int size = 5;
int width = 570/size;
int height = 430/size;
for(int m=0;m<size;m++){
for(int n=0;n<size;n++){
g.drawRect(posx,posy,width,height);
posx += width;
}
posx = 10;
posy += height;
}
}
}