1

I'm creating an application in which I have a class with a main frame (JFrame), and a class which is a JPanel subclass called OvalPanelClass. I say main frame because the user has the option to open up another JFrame (window). This OvalPanelClass is dynamic and displays its images using a BufferStrategy. It is both launched into a separate JFrame on some occasions and appears at the bottom right section of the main frame at some points too, so I didn't feel it made sense to make this class an inner class of the class containing the main frame.

The problem is that as this JPanel is not a part of a class with a JFrame it cannot make the call to get the BufferStrategy and so on. So to get over this I tried passing a reference to the main frame to the OvalPanelClass constructor but I'm not getting results. My question is what is flawed with my reasoning?

FrameClass

import java.awt.BorderLayout;
import java.awt.Dimension;

import javax.swing.JFrame;
import javax.swing.JPanel;


public class FrameClass extends JFrame {

    public static void main(String[] args) {
        FrameClass test = new FrameClass();
        test.setup();
    }

    void setup() {
        setPreferredSize(new Dimension(800, 800));
        JPanel background = new JPanel(new BorderLayout());
        getContentPane().add(background);
        setVisible(true);
        OvalPanelCanvas shape = new OvalPanelCanvas(this);
        //shape.setup(this);
        background.add(BorderLayout.CENTER, shape);
        pack();
        shape.repaint();
    }
}

OvalPanelCanvas

import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class OvalPanelCanvas extends JPanel {
    JFrame frame;
    BufferStrategy bs;
  public OvalPanelCanvas(JFrame frame) {
      this.frame = frame;
      setOpaque(false);
  }
  public void paintComponent(Graphics g) {
      frame.createBufferStrategy(2);
      bs = frame.getBufferStrategy();
      Graphics bufferG = bs.getDrawGraphics();
      for (int i = 0; i < 5; i++) {
          int width = 50;
          int height = 50;
          bufferG.setColor(new Color(i*5,i*6,i*50));
          bufferG.fillOval(0, 0, width*i, height*i);
      }
      bufferG.dispose();
      bs.show();
  }
}

Thanks for your time! I'm being vague about what the project is and have stripped away what I felt were irrelevant details but if you need some more context let me know.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
quantum285
  • 1,032
  • 2
  • 11
  • 23
  • 1
    Ahhh, JPanel will use the Swing rendering engine, which won't be connected to your BufferStrategy...this means that unless the JPanel is realised (attached to a container attached to a native peer) it won't be painted. The problem with your approach is it's the wrong approach – MadProgrammer Jan 07 '15 at 01:18
  • *"I say main frame because the user has the option to open up another JFrame (window)."* See [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/q/9554636/418556) – Andrew Thompson Jan 07 '15 at 03:37
  • But why is it the wrong approach and what's a better one? – quantum285 Jan 07 '15 at 08:46

1 Answers1

0

I'm having a similar issue. I think it has more to do with not using Canvas vs. using JPanel. For instance I have images on a sprite sheet that I cannot use because my class extends JFrame and not Canvas. The reason that being is because I am using the JFrame addon from within eclipse that automatically extends JFrame on the creation of the JFrame in the window class.