We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Intercepting resize events with Swing
Page Index Toggle Pages: 1
Intercepting resize events with Swing (Read 416 times)
Intercepting resize events with Swing
Jan 29th, 2009, 11:41pm
 
I am not entirely sure how to intercept and handle resize events using the Processing API and Swing. My program is based on SwingExample.java and EmbeddedWithSlider.java in Chapter 11 of Visualizing Data, but not all resize events are handled correctly. I would really appreciate any suggestions to get this right. Thanks in advance.

Below is a stripped down version of my program to illustrate the idea. A number of random positions are recalculate every time MyPlot is resized and then drawn to screen as small red disks. My reasoning is that the resize event should be handled correctly for MyPlot, like any other class that inherits from Component, right? It often doesn't completely repaint or just shows the white background (especially after maximizing).

MyFrame.java
------------

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


public class MyFrame extends JFrame {
 public MyFrame() {
 super( "My Frame" );

 setLayout( new BorderLayout() );
 MyPlot plot = new MyPlot();
 add( plot, BorderLayout.CENTER );
 plot.init();
       
 pack();
 setVisible( true );
}

 public static void main( String[] args ) {
   new MyFrame();
 }
}

MyPlot.java
-----------

import java.awt.event.*;
import processing.core.*;

public class MyPlot extends PApplet
 implements ComponentListener {

 float[][] positions;
 int dataSize = 2000;
 int border = 20;
 Boolean dirty = true;

 public void setup() {
   size( 800, 600 );
   noLoop();
   addComponentListener( this );

   positions = new float[2][dataSize];

   smooth();
   noStroke();
   fill( 255, 0, 0 );
 }

 public void draw() {
   if ( dirty ) {
     calcPositions();
     dirty = false;
   }

   background( 255 );
   for ( int i = 0; i < dataSize; i++ ) {
     ellipse( positions[0][i], positions[1][i], 5, 5 );
   }
 }

 private void calcPositions() {
   for ( int i = 0; i < dataSize; i++ ) {
     // Calculate random x- and y-positions
     positions[0][i] = random( border, width-border );
     positions[1][i] = random( border, height-border );
   }
 }

 // Implemented methods from ComponentListener
 // interface follow below

 public void componentHidden( ComponentEvent e ) {
   // Do nothing
 }

 public void componentMoved( ComponentEvent e ) {
   // Do nothing
 }

 public void componentResized( ComponentEvent e ) {
   // Intercept resize event, set flag to
   // recalculate positions and call redraw()
   dirty = true;
   redraw();
 }

 public void componentShown( ComponentEvent e ) {
   // Do nothing
 }
}
Page Index Toggle Pages: 1