0

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).

The layout of my application

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();
      }
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
quantum285
  • 1,032
  • 2
  • 11
  • 23
  • from Canvas's API doc: "The paint method must be overridden in order to perform custom graphics on the canvas." Have you tried that? – Ingo Leonhardt Jan 07 '15 at 12:50
  • That works but I didn't mean to post the Canvas sample code, as isn't there a problem with using canvas with swing? – quantum285 Jan 07 '15 at 12:52
  • 1
    Canvas is old AWT code. If you want to do custom painting in Swing, then do custom painting in Swing. Subclass JPanel and override its paintComponent() method, as documented in the tutorial: http://docs.oracle.com/javase/tutorial/uiswing/painting/ – Gimby Jan 07 '15 at 13:25

1 Answers1

1

I'm using a BufferStrategy to prepare the image before showing it.

There is no need to use a BufferStrategy to prepare the image. Swing painting is fast enough.

You are attempting to paint 5 ovals. Thousands of ovals could be painting an you won't notice a performance problem.

Just follow the link given by Gimby above and use standard Swing painting.

In case you are interested, check out Custom Painting Approaches for an example that show how to dynamically paint Rectangles in a JPanel as the user manually adds a Rectangle.

camickr
  • 321,443
  • 19
  • 166
  • 288