Simon Schulz
Simon Schulz

Reputation: 37

Java JFrame Graphics dont show up

public class FrameTest extends JFrame {

    private JPanel contentPane;
    private JPanel spielPane;
    private JPanel infoPane;
    private JButton btnStart;
    private JLabel lblRunde;
    private Monster monster1;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    FrameTest frame = new FrameTest();
                    frame.setVisible(true);
                    Monster m = new Monster();
                    frame.repaintSpielPanel();
                }
                catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public FrameTest() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 800, 600);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(null);
        setContentPane(contentPane);

        spielPane = new JPanel();
        spielPane.setBounds(6, 6, 566, 566);
        spielPane.setLayout(null);
        spielPane.setBackground(Color.yellow);
        contentPane.add(spielPane);

        infoPane = new JPanel();
        infoPane.setBounds(578, 6, 216, 566);
        infoPane.setLayout(null);
        infoPane.setBackground(Color.yellow);
        contentPane.add(infoPane);

        btnStart = new JButton("Start");
        btnStart.setBounds(44, 6, 117, 29);
        infoPane.add(btnStart);

        lblRunde = new JLabel("Runde");
        lblRunde.setBounds(77, 47, 61, 16);
        infoPane.add(lblRunde);

        monster1 = new Monster();
        monster1.setVisible(true);
        spielPane.add(monster1);
    }

    private class Monser extends JLabel {
        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.RED);
            g.fillRect(0, 0, 10, 10);
        }
    }

    public void repaintSpielPanel() {
        spielPane.repaint();
    }
}

I tried to put a Rectangle into my frame, but it isn't there. I'm just starting to program, so probably I just don't know how to get it onto the screen. Pls help!

Upvotes: 0

Views: 74

Answers (1)

Hovercraft Full Of Eels
Hovercraft Full Of Eels

Reputation: 285405

  1. Don't paint in a JLabel which isn't opaque. Instead do so in a JPanel.
  2. Don't use null layouts and setBounds(...). While null layouts and setBounds() might seem to Swing newbies like the easiest and best way to create complex GUI's, the more Swing GUI'S you create the more serious difficulties you will run into when using them. They won't resize your components when the GUI resizes, they are a royal witch to enhance or maintain, they fail completely when placed in scrollpanes, they look gawd-awful when viewed on all platforms or screen resolutions that are different from the original one.
  3. Most important, you never add an instance of your drawing component, here Monser, to anything. To see that this is true, search your code for any calls to new Monser(). If a constructor isn't being called, an instance isn't going to be created.
  4. Also note that you're creating a Monster instance, and this is a class that you've not shown us.
  5. You're adding it to a null layout using JPanel, and so since it has no size, it won't show at all. Again, don't use null layouts.

For example:

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;

import javax.swing.*;

@SuppressWarnings("serial")
public class MyTest extends JPanel {
   private static final Color BG = Color.yellow;
   private static final int GAP = 8;
   private JButton btnStart = new JButton("Start");
   private JLabel lblRunde = new JLabel("Runde", SwingConstants.CENTER);
   private MonsterPanel monsterPanel = new MonsterPanel(600, 600, BG);

   public MyTest() {
      JPanel buttonPanel = createButtonPanel();
      buttonPanel.setBackground(BG);


      setLayout(new BorderLayout(GAP, GAP));
      setBorder(BorderFactory.createEmptyBorder(GAP, GAP, GAP, GAP));
      add(monsterPanel, BorderLayout.CENTER);
      add(buttonPanel, BorderLayout.LINE_END);
   }

   private JPanel createButtonPanel() {
      JPanel btnPanel = new JPanel();
      btnPanel.setBackground(BG);

      JPanel gridPanel = new JPanel(new GridLayout(0, 1, 0, 5));
      gridPanel.setOpaque(false);
      gridPanel.add(btnStart);
      gridPanel.add(lblRunde);

      btnPanel.add(gridPanel);
      return btnPanel;
   }

   private static void createAndShowGui() {
      MyTest mainPanel = new MyTest();

      JFrame frame = new JFrame("MyFrameTest");
      frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
      frame.getContentPane().add(mainPanel);
      frame.pack();
      frame.setLocationByPlatform(true);
      frame.setVisible(true);
   }

   public static void main(String[] args) {
      SwingUtilities.invokeLater(new Runnable() {
         public void run() {
            createAndShowGui();
         }
      });
   }
}

class MonsterPanel extends JPanel {
   public static final int MONST_WIDTH = 10;
   public static final Color MONST_COLOR = Color.red;
   private int prefW;
   private int prefH;
   private int monstX = 0;
   private int monstY = 0;

   public MonsterPanel(int prefW, int prefH, Color bkgrnd) {
      this.prefW = prefW;
      this.prefH = prefH;
      setBackground(bkgrnd);
   }

   public void setMonstXY(int x, int y) {
      this.monstX = x;
      this.monstY = y;
      repaint();
   }

   @Override
   protected void paintComponent(Graphics g) {
      super.paintComponent(g);
      g.setColor(MONST_COLOR);
      g.fillRect(monstX, monstY, MONST_WIDTH, MONST_WIDTH);
   }

   @Override
   public Dimension getPreferredSize() {
      if (isPreferredSizeSet()) {
         return super.getPreferredSize();
      }
      return new Dimension(prefW, prefH);
   }
}

Upvotes: 1

Related Questions