Draw on QT Component

Hi

first of all let me explain my program:

I have a java program which is consist of different classes. first class (lets call it A) is the main class which create for me the main java frame ( i use swing package for creating interface).2nd class(lets call it B) implements a JPanel which as 3 buttons circle, line, rectangle. 3rd class imeplents a JPanel too which consists of a Quicktime palyer component for playing a movie. Class A adds panels are created by class B and C to itself and create the main UI.

I wanna let user draw on the QTcomponent when the movie is paused. I mean that during watching the movie maybe user wants to draw a line or circle.so she has to pause the movie and then draw a line , circle or rectangle depends on what is selected.

the problem is that I can not draw on QTcomponent. I tried many ways:

I implement MouseListener for QT and wrote code for drawing but always drawing is on the back of movie which is invisible for user not on the front.

the other way, I add another panel on the top of QTcomponent by OverlayLayout which this panel is transparent but again QTComponent is on the top.

The last way I did, is get the Glasspane of frame in Class A, then implement MouseListner for it and activate it. but again QTcomponet is always on top :(

On all those ways my code work properly but I dont know why QT is always on top

I wanna ask you do u have any idea how I can solve my problem? How can I create a panel on th QT and let user to draw on the movie (as this panel is transparet the user thinks that she is drawing on the movie.)

Or do you have any idea how I can implemnt this feature for the user?

[1709 byte] By [Saamia] at [2007-10-3 2:49:41]
# 1

Hey Saami. Got some mixed news for you... Some good and some bad.

Bad news first... (I like to finish on a high note. :-) )

QTComponent is implemented using AWT. This has some advantages for speed which is probably why it was done by quicktime. So lets have a think about that AWT fact. It's a heavyweight component. What does that mean? Basically it means problems when you try to integrate it into a Swing application. Swing is written entirely in Java in contrast to AWT, and is considered to be "Lightweight".

So anything you do using swing will be lightweight programming. Mixing heavyweight AWT with lightweight Swing is not recommended for one reason: When 2 components are put in the same place in screen, the heavyweight component will ALWAYS come out on top of the other one- even if it is not meant to...

JPanel is lightweight, and so is GlassPane. QTComponent is heavyweight and will always be above these no matter what. This will also cause problems if you have say a JPopupMenu trying to go over QTComponent too... It is a bit annoying I know but there is ways around this annoying problem.

Ok the good news. :-)

QTJ has a SWING version of QT component. That means that it is light weight and fully compatible with all of the things you are used to in Swing. I suggest you look at the api about the Swing player. Must warn you though- the drawback to using Swing is the verything that makes it good- the lightweight. In order to make it lightweight, Swing copies the frame to an offscreen buffer then re-copies it to an onscreen image. Not a problem if you are using todays computers, but if you need to use your application on a slow computer, you can start to appreciate the problem when you do this operation 30 times per second... etc.

Sorry about the essay, but in short (haha) your question was not easily answered with a short answer.

I hope this gives you some direction.

Jason Barraclough.

fireman.sparkeya at 2007-7-14 20:38:28 > top of Java-index,Desktop,Core GUI APIs...
# 2

here is some code I used to get a Swing version of Quicktime player so you can get started.

Quicktime player as AWT heavyweight component:

QTComponent qtc = QTFactory.makeQTComponent(Movie);

Component c = qtc.asComponent();

and as a JComponent

QTJComponent qtjc = QTFactory.makeQTJComponent(Movie);

JComponent jc = qtjc.asJComponent();

Good luck!!!

fireman.sparkeya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 3
thanx for your kindly reply. as you said I used QTComponent not QTJcomponent.Do you any source give me for implementing QT in Swing ?Do you think if i change QT to Swing mode then I can use GlassPane to draw on the top of QTJComponent?
Saamia at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 4

Hey Saami.

I myself do not have any source code to hand out as I have been using AWT version for the moment. The way I got around doing what you wanted to do was to grab the image of the frame, and then turn it into an awt.Image object to draw on instead, separately from the video.

It should work if you change it all into Swing mode as the QTJComponent is lightweight and fully swing compatible.

There is a really cool book by O'Reilly that does have some code snippets for general public viewing on the internet. Its called Quicktime For Java- a developer's notebook.

If you browse through that book or look for code snippets on the web about it it will answer how to implement it in Swing with examples.

The key point is to use the code snippet I provided in the last message to display the QTJComponent instead. The rest of the methods are basically the same as QTComponent.

Best of luck

Jason Barraclough

fireman.sparkeya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 5

Hey

I did as you said but now have a new problem. when I create my JComponent by using QTJComponent the factory does not have a moviecontroller bar. I mean the bar in bottom of frame which has buttons for play/pause and stop is hidden. Would you please tell guide me Hhow I can create that component (maybe can call it slider ).

Thanx

Saamia at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 6

Hey saami!

Great to here you have managed to get this far. Yes, the disadvantage with using the Swing version of QTJComponent is that there is indeed no movie controller bar. I'm not particularly sure as to why they didn't include a movie controller in the swing version. The stuff I have read seems to suggest that they may have avoided it as it was too difficult to create a Swing wrapper.

I myself have personally not created a movie controller like a slider. However I have created a controller that frame-by-frame advances and rewinds a movie. What you could do, is create a slider that fires slider events leading to frame advance/rewind and scale it to the movie length. I have just released source code on SourceForge.net.

Goto www.sf.net/projects/physmo.

Download the source package and investigate the contents of the controller panel. The source is fairly well labeled, and you should be able to adapt that to slider events.

Hope this helps you out mate!!!

Regards Jason Barraclough.

fireman.sparkeya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 7

Hey Jason,

Thanks for all previous help

Actually I did all those things and implement QT as Swing Component. As we dont have the movie controller in Swing I have to implement by myself. I add Jslider to my panel. My problem is that How I can sync the Jslider and movie?

I could not find any listener for the QTComponent while the movie is Playing for updating the position of JSlider knob. Do you have any idea How I can do that?

I need a listener or API which runs when the Movie is playing. Then I can update the Slider by correct value.

I found some APIs are called when the rate is changed or the movie starts and stops but could not an API which runs while the movie is shown

Apprciate if guide me again.

Cheers

Saami

Saamia at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 8

Hi Friends!

I also have a same project as Saami and I want to draw on my movie but I'm very new to Swing and also to Quicktime. I don't know how to use a glass pane on the movie. My application is similar to MS Paint of Windows, so user can draw any shape in the drawing area and also play a movie. I want to draw over the movie while movie is playing/stop.

As you guys already worked on it , I have a hope that you will help me out to solve this problem.

Your any help , suggestions ,link or code snippet would be helpful to me.

I appreciate any help on this topics.

Thanks

Kirti

Kirti_Mistrya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 9

Hello Saami

Apologies for late reply- been very busy with uni.

Ok, what you need to do is have it so that the movie fires events every time period and these update the slider. Ok, first some bad news- there is no direct API for what you are after, you are going to have to create a hack to get around it. Thats ok because the hack is not too difficult.

Here goes.

You need to create a Swing timer that probes the movie every say 150msec. This is located in javax.swing.Timer package.

eg. javax.swing.Timer myTimer = new javax.swing.Timer(150, this);

the this keyword is just referring to the class you have the action listener handler in. So in you public void actionPerformed(ActionEvent e) method, you will have something like this:

if(e.getSource()==myTimer)

{

int Seconds = Movie.getTime()/Movie.getTimeScale();

int seconds = Seconds%60;

int minutes = Seconds/60;

...

...

...

}

now you have the current time that you are into the movie. If you get the total length of the movie, you should be able to express this value as a fraction of the total length of the movie, and convert that into a position on your JSlider bar. Obviously if you wish to improve the number of ticks and make it smoother, make the 150msec level smaller.

Once again, sorry for the delay.

Take care

Jason Barraclough.

fireman.sparkeya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 10

hey there kirti!

Well, to tell the truth, I have a feeling that Saami is your best bet now as far as code snippets go. He is practically doing exactly what you are doing from your description.

Alas I have no snippets of code doing just that, but I have a project using QTJava that I maintain. If you are after code snippets to help learn Java Swing and/or QTJava you can check them out on sourceforge (I know it is a shameful plug)

www.sf.net/projects/physmo

www.sf.net/projects/avianfs

avianfs is practically Swing widgets.

physmo is QTJava stuff.

Good luck kirti!

fireman.sparkeya at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 11

Hi Jason,

Thanks. thanks for your solution. it is funny because exactly I did the same solution as you wrote before and handle the program 2 weeks ago.

Just I have a little problem ( maybe it is not problem). I put QTcomponent in a panel and add it to frame. For drawing on QT I get the graphic of QT's panel. The problem is that when user draw on QT and change between different windows in OS like Windows , drawing is gone. I mean if my program's window is covered by another window and then come back drawing is gone. Some how drawing is off-screen . No idea why !!! I dont need to save drawing at all but have to keep it till user presses a button.

Any idea !!!!!

Saami

Saamia at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 12

You have to do your drawing every time the window is redrawn. The usual way to do this, is to override the paintComponent() method of your panel that contains the TJComponent. Oh and it is really important to call super.paintComponent(g) as the first statement of your method.

public void paintComponent(Graphics g) {

super.paintComponent(g);

// Now do your drawing

}

Remember this method has to go into your subclass of the panel that holds the QTJComponent.

TimRyanNZa at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 13

Hi

Sorry I didnt get you. Let me explain :

I have a frame has one panel which has 2 sub panels :

1.includes QTcomponent

2.includes the buttons for controling movie

I get the Graphics of first panel , add MouseListener and draw on it.

For drawing I didnt use paint(). I just get the Graphics g and draw like g.drawLine ..... or g.drawRect ........

So where I should use your idea?

Saamia at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 14

Which is exactly why you have the problem - you're drawing directly onto the panel which doesn't save anything, so as soon as the window is redrawn for any reason, your stuff is lost.

Instead of drawing directly, get your mouse listener to store the coordinates of the drawing information to a buffer of some sort (eg List). Call panel.repaint() after you've stored each piece of drawing, and in your panel's paintComponent() method walk the list of drawing information and render it onto the Graphics object provided (using g.drawRect etc like you do now - that bit doesn't change).

That way, whenever the panel is repainted for any reason, your drawing will stay.

TimRyanNZa at 2007-7-14 20:38:29 > top of Java-index,Desktop,Core GUI APIs...
# 15

Great thanks for guiding

I have one more question:

Do you have any idea how I can clean the panel after user draws?

I mean maybe user wants to delete every thing and draw from beginning or for drawing Box (rectangular) while user dragging I should clear the previous box.

would you please tell me how can I do that?

I call repaint ( I dont have any class paint and also dont imeplemnt class pain). I have 2-3 seprate function for drawing different object. so when I just call reapint it doesnt clean my "Graphics g".

btw although my class which has these functionality doesnt have any panel or frame just panel's ghraphic from another class will send to this class for drawing, it iextends Jpanel because I have to use repaint();

Appreciate for your guidance

Saami

Saamia at 2007-7-21 9:59:43 > top of Java-index,Desktop,Core GUI APIs...
# 16

Hi!

I have a project like (not exactly) MS PowerPoint. In which I have PageSorter same like PowePoint's Slide Sorter. Which has a thumbnail of what I draw on my page. Sounds like store all information what ever drawn by user.

When I try to draw line or other shapes from outside the movie and drag it insidea movie area then it draw a line or circle behind a movie. But when I try to draw exctly on the movie it didn/t draw any thing.

I set MovieController to QtComponent and add QTComponent to JWindow and JWindow to JFrame.

Where am I wrong with this?

Appreciate for your reply.

Cheers!

Kirti

Kirti_Mistrya at 2007-7-21 9:59:43 > top of Java-index,Desktop,Core GUI APIs...
# 17
Please don't hijack someone else's thread - start a new one when you want to ask your own questoin.
TimRyanNZa at 2007-7-21 9:59:43 > top of Java-index,Desktop,Core GUI APIs...
# 18
Oh!I'm sorry for that. I'm thinking that its same subject so...I'm appologize for that.I'm going to post my new ThredHave a great timeCheers!Kirti
Kirti_Mistrya at 2007-7-21 9:59:44 > top of Java-index,Desktop,Core GUI APIs...