Hello I’m new to graphics and am in the process of bringing myself up to speed on java 2d. I am now looking at how to use the BufferStratergy to manage the offscreen buffer.
I’ve written a test program but nothing appears in my canvas/frame. Any idea what I’m doing wrong? I’m sure it must be something trivial. I’m using Java 1.6.0_6 on windows XP [code] package waterfall; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.image.BufferStrategy; import java.util.Random; public class Waterfall05 extends java.awt.Canvas implements Runnable { final int WATERFALL_WIDTH = 701; final int WATERFALL_HEIGHT = 300; Random rnd = new Random(); final int width; final int height; BufferStrategy bufferStratergy=null; Color[] newLine = null; int numLines=0; public Waterfall05() { setPreferredSize(new java.awt.Dimension(701, 300)); setIgnoreRepaint(true); Dimension size = getPreferredSize(); width = size.width;//- insets.left - insets.right; height = size.height;// - insets.top - insets.bottom; newLine = new Color[0]; ActionListener taskPerformer = new ActionListener() { public void actionPerformed(ActionEvent evt) { generateNewData(); } }; javax.swing.Timer t = new javax.swing.Timer(500, taskPerformer); t.start(); } private void generateNewData() { newLine = new Color[WATERFALL_WIDTH]; for(int i=0; i<WATERFALL_WIDTH; i++) { newLine[i] = getColor(); } if (numLines<300) numLines++; } private Color getColor() { return new Color(rnd.nextInt(0x1000000)); } private void render() { System.out.println("render!"); if (bufferStratergy==null) { bufferStratergy = getBufferStrategy(); System.out.println("....created buffer stratergy..."); } // Render single frame do { // The following loop ensures that the contents of the drawing buffer // are consistent in case the underlying surface was recreated do { // Get a new graphics context every time through the loop // to make sure the strategy is validated Graphics graphics = bufferStratergy.getDrawGraphics(); // Render to graphics render(graphics); // Dispose the graphics graphics.dispose(); // Repeat the rendering if the drawing buffer contents // were restored } while (bufferStratergy.contentsRestored()); // Display the buffer bufferStratergy.show(); // Repeat the rendering if the drawing buffer was lost } while (bufferStratergy.contentsLost()); } private void render(Graphics g) { Graphics2D g2d = (Graphics2D)g; System.out.println("painting frame"); g2d.setColor(Color.ORANGE); g2d.fillRect(0, 0, width, height); } private void renderX(Graphics g) { Graphics2D g2d = (Graphics2D)g; System.out.println("painting frame: "+g2d); g2d.copyArea(0, 0, width, height-1, 0, 1); for(int i=0; i<newLine.length; i++) { g2d.setColor(newLine[i]); g2d.drawRect(i, 0, 1, 1); } } public void startRendering() { System.out.println("startRendering...."); Thread thread = new Thread(this); thread.start(); } public void run() { while (true) { render(); try { Thread.sleep(300); } catch (InterruptedException e) { System.out.println("interrupt"); } } } } package waterfall; import java.awt.Canvas; public class TestWaterfall05 extends javax.swing.JFrame { /** Creates new form TestWaterfall05 */ public TestWaterfall05() { initComponents(); Waterfall05 waterfall = new Waterfall05(); this.getContentPane().add(waterfall); this.setSize(waterfall.getPreferredSize().width, waterfall.getPreferredSize().height); //this.setIgnoreRepaint(true); waterfall.createBufferStrategy(2); waterfall.setVisible(true); waterfall.startRendering(); } /** This method is called from within the constructor to * initialize the form. * WARNING: Do NOT modify this code. The content of this method is * always regenerated by the Form Editor. */ @SuppressWarnings("unchecked") // <editor-fold defaultstate="collapsed" desc="Generated Code"> private void initComponents() { setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane()); getContentPane().setLayout(layout); layout.setHorizontalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 400, Short.MAX_VALUE) ); layout.setVerticalGroup( layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING) .addGap(0, 300, Short.MAX_VALUE) ); pack(); }// </editor-fold> /** * @param args the command line arguments */ public static void main(String args[]) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { new TestWaterfall05().setVisible(true); } }); } } [/code] [Message sent by forum member 'ser207' (ser207)] http://forums.java.net/jive/thread.jspa?messageID=292552 =========================================================================== To unsubscribe, send email to [EMAIL PROTECTED] and include in the body of the message "signoff JAVA2D-INTEREST". For general help, send email to [EMAIL PROTECTED] and include in the body of the message "help".