TheLovelySausage
TheLovelySausage

Reputation: 4094

Java Drawing to a JPanel (debugging)

I'm trying to draw a basic object to a JPanel although it doesn't seem to be working.

I'm certain I am doing something wrong with where the paint method is being called

import javax.swing.*;
import java.awt.*;
import java.awt.image.*;

public class testGui {

   static colors   gc_colors;
   static gui      gc_gui;

   public static void main(String[] args) {

      gc_colors = new colors();
      gc_gui = new gui();

      gc_gui.cv_frame.setVisible(true);

   }

   public static class colors {

      Color   cv_ltGrey;
      Color   cv_mdGrey;
      Color   cv_dkGrey;

      public colors() {

         cv_ltGrey = Color.decode("#DDDDDD");
         cv_mdGrey = Color.decode("#CCCCCC");
         cv_dkGrey = Color.decode("#111111");

      }

   }

   public static class gui {

      JFrame   cv_frame;
      JPanel   cv_panel;
      JPanel   cv_content;

      public gui() {

         cv_frame = new JFrame();
         cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
         cv_frame.setTitle("Test GUI");
         cv_frame.setSize(600, 400);
         cv_frame.setLayout(new FlowLayout());

         cv_panel = new JPanel();
         cv_panel.setBackground(gc_colors.cv_ltGrey);
         cv_panel.setPreferredSize(new Dimension(500, 300));

         cv_frame.add(cv_panel);

         cv_content = new content();
         cv_panel.add(cv_content);

      }

   }

   public static class content extends JPanel {

      public void paint(Graphics graphic) {
         super.paint(graphic);
         draw(graphic);
      }

      public void update() {
         repaint();
      }

      public void draw(Graphics graphic) {

         Graphics2D graphic2D = (Graphics2D) graphic;
         graphic2D.setPaint(gc_colors.cv_ltGrey);
         graphic2D.fillRect(10, 10, 100, 100);

      }

   }

}

I have a class for my gui which I am adding a JPanel to (a light grey one). Which I am then trying to add my drawing to using a JPanel extended class called content.

When I run it though it seems to create the grey JPanel which I want but the drawing is just a tiny white square and I'm not sure why.

Upvotes: 0

Views: 639

Answers (2)

TheLovelySausage
TheLovelySausage

Reputation: 4094

I've decided to just leave out the second JPanel altogether. It was too much of a hassle to put the JPanel inside of another JPanel so instead I am only going to use a single JPanel

  public static class gui {

  JFrame   cv_frame;
  JPanel   cv_panel;
  JPanel   cv_content;

  public gui() {

     cv_frame = new JFrame();
     cv_frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
     cv_frame.setTitle("Test GUI");
     cv_frame.setSize(600, 400);
     cv_frame.setLayout(new FlowLayout());

     cv_content = new content();
     cv_content.setBackground(gc_colors.cv_ltGrey);
     cv_content.setPreferredSize(new Dimension(500, 300));
     cv_frame.add(cv_content);

  }

  }

Upvotes: 0

MadProgrammer
MadProgrammer

Reputation: 347194

So, you content panel has a default preferred size of 0x0, FlowLayout honours the preferredSize of its components (with a little margin), hence the reason why you have a nice little small white rectangle.

What you need to do is override the getPreferredSize method of the content panel and return a suitable size, for example

Example

public static class content extends JPanel {

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(120, 120);
    }

    public void paint(Graphics graphic) {
        super.paint(graphic);
        draw(graphic);
    }

    public void update() {
        repaint();
    }

    public void draw(Graphics graphic) {

        Graphics2D graphic2D = (Graphics2D) graphic;
        graphic2D.setPaint(gc_colors.cv_ltGrey);
        graphic2D.fillRect(10, 10, 100, 100);

    }

}

Upvotes: 1

Related Questions