Newbie question: added a viewport, now what?


I’ve just started on my first Juce app. Comming from an MFC background Juce seems a lot cleaner and lighter. :smiley:

In my application I have a lot of ‘stuff’ to draw. Let’s say this requires an area of 4000x4000 pixels. My application window is much smaller, so the logical thing (?) would be to add a ViewPort, which I did using the Jucer. I also found out that you can add a component to the viewport with the setViewedComponent function. But what would be the best component to add in my situation ?

For now I have created a class inheriting from ‘Component’ and used this as parameter for setViewedComponent. In the constructor of the new class I use setSize(4000, 4000). That all seems to work well. But where do I draw to ? Is the paint method of my class called automagically ?

In the Jucer you can set the content field of a Viewport to point to some Jucer file. That didn’t seem to work for me. What kind of file should I specify here?


Well, answering my own question, here’s what I did:

I created a class inheriting from DrawableImage. This class is added to the viewport with the setViewedComponent method.
Added a member variable of type Image to my class. In the class constructor I initialize the image to the required size. I also added an OnPaint function where I create a Graphics object from the Image variable. All drawing is done with the new graphics object. When the main window’s paint function is called I call my Onpaint function and hey presto I’ve got scrollable vieport content.

Only question left for me: is there a way to have the paint function called automatically ?

Alright, let’s start again from the beginning.
The paint(Graphics& g) function is called “automagically”. This means that if your class is inheriting from Component, the Juce windowing system will automatically call that function for every visible Component of your window.
That said, you don’t need to create your own Graphics object, since it will already be provided by the paint function.
And you don’t need a class inheriting from DrawableImage either.
Just use a plain and simple Component, like this:

[code]class MyComponent : public Component



void paint(Graphics& g)
//Do your painting here, for example:
g.setColour(Colours::black); //Choose a color
g.fillAll(); //Paint everything with the chosen color


And now create your Component and add it to your viewport:

addAndMakeVisible(myComponent = new MyComponent()); addAndMakeVisible (theViewport = new Viewport()); theViewport->setViewedComponent(myComponent);

you’ll probably need to set their bounds too.

Hope this helps!

Thanks for your reply. Your solution certainly works. I originally started with something similar, but I guess I forgot to add a call to addAndMakeVisible and setBounds for my derived class.

Also the order of doing things is important of course. I can’t get the Jucer to generate my derived class before the viewport yet, but that’s another topic.