File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes and onto graphics Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "and onto graphics" Watch "and onto graphics" New topic
Author

and onto graphics

Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

Hello all ! I have a small problem with the repaint() method.
I have a graphical component consisting of about 6 filled rectangles and 3 filled ovals, each with a random color. Which is why I'd like to have it 'repaint itself' every X units of time.
I tried by entering



after drawing all my figures, within the same component method. It compiles and works, but the only problem is that it isn't taking my time parameter into account, and repaints at light speed :p
(An instance of the component is then called into a JFrame since I hadn't mentioned it)

Second question, what is best between JFrame and Applet? if they're both good for different cases, could you specify which? thanks! xD

Jake
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19654
    
  18

Jake Mauve wrote:Hello all ! I have a small problem with the repaint() method.
I have a graphical component consisting of about 6 filled rectangles and 3 filled ovals, each with a random color. Which is why I'd like to have it 'repaint itself' every X units of time.
I tried by entering



after drawing all my figures, within the same component method. It compiles and works, but the only problem is that it isn't taking my time parameter into account, and repaints at light speed :p
(An instance of the component is then called into a JFrame since I hadn't mentioned it)

The argument isn't an interval, it's a maximum time. If the repaint can finish sooner it will.

What you want is a javax.swing.Timer. Inside the ActionListener you call repaint(). The Timer will make sure the code is called on a regular basis.

Second question, what is best between JFrame and Applet? if they're both good for different cases, could you specify which? thanks! xD

What is best between an apple and a pear?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

ok, I've got



could you please tell me where I 'put it in'?
and, please don't quote a message when its already printed right up, what else would you be answering to? :p and please dont answer a question with another one =) cause the point is, in both cases the answer depends on context rather than taste xD I like using applets cause I find it makes it a bit easier not having to call my component object the same way and that in practical terms it lets we have more than one applet window, as in, it opens a new one each time. But, I dont know enough about either applets or frames to know their advantages/disadvantages. I've read a bit on it, even had a lecture on it this past week but hearing from other programmers is good too xD
Thanks!
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

question =) (I thought it better to post here instead of making a new thread in the forum)

I have a problem running my applets in both Internet Explorer and Firefox, I get console errors. But running them through BlueJ goes no problem. I only ask because I'd like to be able to send my applets to other people without making them install blueJ xD

the first line in the error message is:
java.lang.UnsupportedClassVersionError: HouseForSaleAppletTest : Unsupported major.minor version 51.0

let me know if you need the whole thing, was trying to keep it short ^^

thanks!
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

Jake Mauve wrote:ok, I've got



could you please tell me where I 'put it in'?
and, please don't quote a message when its already printed right up, what else would you be answering to? :p and please dont answer a question with another one =) cause the point is, in both cases the answer depends on context rather than taste xD I like using applets cause I find it makes it a bit easier not having to call my component object the same way and that in practical terms it lets we have more than one applet window, as in, it opens a new one each time. But, I dont know enough about either applets or frames to know their advantages/disadvantages. I've read a bit on it, even had a lecture on it this past week but hearing from other programmers is good too xD
Thanks!


could someone please help me with this at least? thanks !
J
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
The unsupported class version means you tried to compile with a new version of Java™. It says here (look for bytes 6-7) that 51 means Java7. Your browser is trying to interpret that with an older version. You might do well, when compiling things to be deployed like that, to use the -target tags, which you can read about here (Java6 version).
I do not know whether you can compile something like new List<>() or try-with-resources or 0b0101_0101 in Java7 with a -target set to 5. I shall let you try it.
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

perfect, thanks! I'll look out the other details
as last favor, could someone please help with my previous question, about where to input the swing timer, because in the explanation it was explained in terms of the event method, but I have none and being a new-beginner, I'm a bit lost
thanks for all the help!
J
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38036
    
  22
Have you seen the Java Tutorials about Timers?
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

well believe it or not I did, that very page too (amongst others). Its just that all the examples and explanations (and tutorials) I found were too complex and technical, only good for programmers with a bit of understanding (or at least more than me). The more I read the more confused I got, because the different examples depend on the end result they try to get to which is most often a far more complex program than the simple paint methods I have, and then I find myself trying to decipher between method and object callings, different if conditions and other. Most of them being almost purely event-handling based contexts, which mine isn't

second question, frame dimension discrepancies:
let me explain. I have a 75 pixel wide square drawn at position (0,0). I have a second, same sized square starting at (0,125). So this second shape should 'end' at y=(y position + shape height)=125+75=200.
(Yes, I know the coordinates are always for the top left corner of the shape and not its center). But, when defining my JFrame to 200 in height, it comes out 'too short' to display both squares. I had to tweak it upto 238 (!) to get both my squares displayed in full, and same story for the width. My shapes add up to (75+50+150=) 275 in width, but I still had to increase the JFrame width definition to 290 and this doesnt make sense to me ..

Thanks for the help!
J
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

when defining my JFrame to 200 in height, it comes out 'too short'

The size of a JFrame includes its title bar. To set the size of the client area, set the preferred size of the contentPane and call pack().

Much better, use a JPanel, override getPreferredSize() to return an appropriate Dimension and either add the panel to the frame or set it as the frame's content pane. Then call pack()


luck, db
There are no new questions, but there may be new answers.
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

Awesome thanks ! =) I'll hopefully get both that and my timer problem done tonight xD
and also, thanks to the moderator who moved me over to the 'right' forum. I apologize if I went in the wrong one, being a new beginner (only been coding for under 8 weeks) I went straight for the 'beginning java'.
Jake Mauve
Ranch Hand

Joined: Oct 25, 2011
Posts: 45

well .. im only more confused than when starting this thread. Now I'm mixing all graphical concepts together (JPanel, JFrame, JApplet ..etc).

so back to the initial problem.
I have my main 'graphics drawing class' extending JComponent, using paintComponent as the class's main method. All my shapes and colors are defined in it. Thats it for methods in this 'source' class.
I have then another class to View/Test it, putting the component into a JFrame.

The codes look something like:

Component class



and then in my tester class/file :



I tried sticking in the minimum code required for the repaint and timer functionalities :



But I wasnt sure where exactly. I was getting compilation errors when putting it into the component source code, it the repaint() method in the actionPerformed() method wasn't being recognized even though I had imported everything I needed. And trying it in the tester class wasnt giving me anything when launching the applet, it seemed to 'block' somehow.

So could someone please help me. All I'm trying to do is to get my drawings 'repainted' every X unit of time because I formulated the fill color with random colors so its different everytime it gets repainted.
the closest I got was getting it to redraw frames when sticking the frame.add(component) into the actionPerformed() method from the Tester class, but that wasnt what I wanted as it just kept generated superimposing new frames over each other. Please give the final, concise result, without confusing me with "oh, use this instead of that, and then use this method after calling A into B but not before C. and then .. ooh, no, much better, do this instead, and check out this other fourth new method from the API or try googling 'some other new concept'. I footnote this by specifying that I started posting in Newbeginner forum as thats what I am and got moved over to the Swing/Graphics. I'm just saying since it could have an affect on the answer.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

repaint() method in the actionPerformed() method wasn't being recognized

An ActionListener doesn't have a repaint() method. Call repaint() on the component that is to be repainted. Since I don't see any code in your main(...) method that constructs any visible GUI, only you know what that component might be.

Please give the final, concise result, without confusing me with ...

We are NotACodeMill. To get better help sooner, post a SSCCE (Short, Self Contained, Compilable and Executable) example that demonstrates the problem.
 
Consider Paul's rocket mass heater.
 
subject: and onto graphics
 
Similar Threads
Questions regarding to using applets and JComponents
Border to JPanel is displacing JPanel which has Video Component
How can I get the feature ALWAYS_ON_TOP with the swing JFrames.
Basic JPanel
Problem with JTree inside JScrollPane inside JPanel