I'm creating an application and as the picture below crudely illustrates there are 3 sections: a top menu (JPanel), a sidebar menu (another JPanel) and a 3rd part which I'll call the preview pane (the yellow part). The preview pane displays a pattern which is rendered based on parameters that the user can tweak in the sidebar menu (RGB type components etc).
As this preview pane has to draw multiple shapes before the pattern is complete I'm using a BufferStrategy to prepare the image before showing it. I've tried so many different ways to achieve this and read so many different opinions whether or not to use Canvas for my preview pane and as such have been sent back and forth from using JPanel to Canvas and so on but can't get this to work with either. What is the best way to achieve what I want? Keen to take recommendations for good resources/ books.
Here is my stripped down code so I can be told where I'm going wrong:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class FrameClass extends JFrame {
Thread t1;
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);
PatternCanvas shape = new PatternCanvas();
background.add(BorderLayout.CENTER, shape);
pack();
shape.repaint();
}
public class PatternCanvas extends Canvas {
BufferStrategy bs;
public void paintComponent(Graphics g) {
createBufferStrategy(2);
bs = getBufferStrategy();
System.out.println("have buffer strategy");
Graphics2D bufferG = (Graphics2D)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();
}
}
}