Swing, Eclipse, and multiple PApplets fail
in
Integration and Hardware
•
2 years ago
Hi there,
I'm having a bit of trouble using Java Swing, Eclipse, and two PApplets in Swing JFrames. Wondering if anybody can help me out... I've attached some pseudocode below that I think covers all the bases.
Basically, via its constructor, the second class PApplet Bar receives a copy of the first class PApplet Foo. The code compiles fine, all imports work OK, the JFrames render correctly, and Eclipse doesn't complain.
Bar can see Foo; the first println statement in Bar's draw() matches the corresponding println in Foo's draw(); both have the same output. The getNumber() method works perfectly in Foo. However, when Bar attempts to access foo.getNumber(), Eclipse throws a null pointer exception.
All methods are public. I've tried accessing static variables and loads of other stuff, but nothing seems to get Bar to see inside Foo.
Thanks, I'd appreciate any help or ideas. I'm at my wits end, and have tried everything I can think of.
---
- import javax.swing.*;
- import processing.core.*;
- ...
- public class Application extends JFrame {
- ...
- public static void main(String[] args) {
- EventQueue.invokeLater(new Runnable() {
- public void run() {
- Application frame = new Application();
- ...
- }
- })
- }
- public Application() {
- ...
- Foo foo = new Foo();
- Bar bar = new Bar(foo);
- JPanel canvas1 = new JPanel();
- foo.init();
- canvas1.add(foo, "");
- JPanel canvas2 = new JPanel();
- bar.init();
- canvas2.add(bar, "");
- }
- }
- // new class file
- import processing.core.*;
- public class Foo extends PApplet {
- public static int number = 10;
- public int getNumber() {
- return number;
- }
- public void setup() {
- }
- public void draw() {
- println(this);
- println(getNumber());
- }
- }
- // new class file
- import processing.core.*;
- import Foo;
- public class Bar extends PApplet {
- public Bar(PApplet foo_) {
- foo = foo_;
- }
- PApplet foo;
- public void setup() {
- ...
- }
- public void draw() {
- println(foo);
- println(foo.getNumber());
- }
- }
1