1

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;

    }

  }
}
Peter Stone
  • 37
  • 1
  • 6
  • 2
    Override `paintComponent` rather than `paint`. See [this](http://stackoverflow.com/questions/9389187/difference-between-paint-paintcomponent-and-paintcomponents-in-swing) and [this](https://docs.oracle.com/javase/tutorial/uiswing/painting/) – copeg Aug 09 '16 at 22:14
  • 2
    And call the super painting method within your override. – Hovercraft Full Of Eels Aug 09 '16 at 22:15
  • 1
    And don't ignore layout managers when planning your GUI. Remember that a JFrame uses a BorderLayout by default (actually its contentPane does). – Hovercraft Full Of Eels Aug 09 '16 at 22:15
  • 2
    (1) Call `frame.setVisible(true)` only once and as the final line in `main`. (2) Don't set size and location, use `pack()` after you add all the components to the frame. (3) You are adding the label after the panel, so it will override it. Try adding the label as `add(label, BorderLayout.SOUTH)`. (4) Override `paintComponent` instead of `paint`. – user1803551 Aug 09 '16 at 22:15
  • I need to have the labels at a specific location on top of the squares, would that be a problem? – Peter Stone Aug 10 '16 at 02:16

1 Answers1

0

I found a valid solution to my problem. I used label.setBounds(positionx,positiony,boundx,boundy). I'm trying to make a simple game that uses a refreshing JPanel, and it's working.

Peter Stone
  • 37
  • 1
  • 6
  • Java GUIs have to work on different OS', screen size, screen resolution etc. using different PLAFs in different locales. As such, they are not conducive to pixel perfect layout. Instead use layout managers, or [combinations of them](http://stackoverflow.com/a/5630271/418556) along with layout padding and borders for [white space](http://stackoverflow.com/a/17874718/418556). – Andrew Thompson Sep 27 '17 at 02:37