Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!

Megs Maquito

Ranch Hand
+ Follow
since May 18, 2005
Cows and Likes
Cows
Total received
0
In last 30 days
0
Total given
0
Likes
Total received
0
Received in last 30 days
0
Total given
0
Given in last 30 days
0
Forums and Threads
Scavenger Hunt
expand Ranch Hand Scavenger Hunt
expand Greenhorn Scavenger Hunt

Recent posts by Megs Maquito

Virendrasinh Gohil wrote:

Megs Maquito wrote:Hmmm. that seemed to do the trick here's the result:

L is 20 i is 0
.......
L is 140 i is 13
outside the for loop
.................

Question is: why would i need to put something outside the for loop? is the i % == 2 somehow telling the class to repaint itself or is it the sequence after the JFrame declaration in main() method? I have other classes that have a for loop that does not have a statement outside the for loop and works perfectly. Here's an example:
....
...
...


No, putting print statement outside of for loop will not prevent executing the paintComponent() method multiple times. I just mentioned that to show you that it's not the "for" loop which is creating problem but it is the event of request to repaint the JPanel. And that is what exactly happened when you re-executed the application. Whenever there is a repaint (either manual or system generated) event, your paintComponent() will get executed.

I could put the for loop in the main method but I want the drawing to be repainted if i resize the JFrame, thus I put the for loop in the paintComponent() method.


Now, if you want to recalculate the length of line on every paintComponent() method, you need to calculate using actual numbers (and not just keep increasing the line using for loop). That means, if the frame/panel is reduced in size, the line size should use new width/heigth values to decrease the line length. This way, let it be multiple repaint event to occur for same window, it will always calculate the lineLength correctly.



OMG you're a genius. However, having the spiral recalculate will entail some heavy duty math (at least for me ) I was able to create what I wanted whilst not growing with the frame. I did what you earlier suggested (that is to put the lineLength calculation outside the paintComponent()). And here's the full code:


Yeah I know, it may seem very elementary for you . I'm just starting out. Thanks so much for your help.

Virendrasinh Gohil = javaGenius(int x);
public int javaGenius (int n){int intelligence = n * infinity; return intelligence};
11 years ago
Ok, now this is becoming really weird. I did a javac SpiralLine.java (without changing anyting) and java SpiralLIne and here's the result:


L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
outside the for loop
L is 160 i is 0
L is 160 i is 1
L is 180 i is 2
L is 180 i is 3
L is 200 i is 4
L is 200 i is 5
L is 220 i is 6
L is 220 i is 7
L is 240 i is 8
L is 240 i is 9
L is 260 i is 10
L is 260 i is 11
L is 280 i is 12
L is 280 i is 13
outside the for loop

I will put the i % == 2 outside of the paintComponent() method and check the results again. Again many thanks for all your help. It made things a lot clearer. I was going crazy trying to find out where my logic is wrong.
11 years ago

Virendrasinh Gohil wrote:Seems like your paintComponent() getting called multiple times. (Try to put one more sysout outside of for loop to prove).

This is event based and whenever there is a need of painting the component, this method will be called. Now depending upon what do you want to do, you can add appropriate logic there in paintComponent method. If you want to recalculate (or something else) on every paint event, you can do it in paintComponent() method. (Not sure from your code what are you trying to do with lineLength in paintComponent method.) If it is a one time activity you may want to reconsider refactoring your code.



Hmmm. that seemed to do the trick here's the result:

L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
outside the for loop

Thanks so much for all your quick replies. Basically the lineLength should increase every other time it goes through the loop. I will be using that variable to alter the drawLine() method so the output will be a line spiralling outwards. I could put the for loop in the main method but I want the drawing to be repainted if i resize the JFrame, thus I put the for loop in the paintComponent() method.

Question is: why would i need to put something outside the for loop? is the i % == 2 somehow telling the class to repaint itself or is it the sequence after the JFrame declaration in main() method? I have other classes that have a for loop that does not have a statement outside the for loop and works perfectly. Here's an example:

11 years ago
I'm really confused why the for loop is working multiple times. When the counter reaches the limit it goes back to zero. For this purpose I omitted the code that draws the actual line. I need to resolve this issue first before I include the algorithm for the line. Thanks so much in advance.



here's the result:

L is 20 i is 0
L is 20 i is 1
L is 40 i is 2
L is 40 i is 3
L is 60 i is 4
L is 60 i is 5
L is 80 i is 6
L is 80 i is 7
L is 100 i is 8
L is 100 i is 9
L is 120 i is 10
L is 120 i is 11
L is 140 i is 12
L is 140 i is 13
L is 160 i is 0
L is 160 i is 1
L is 180 i is 2
L is 180 i is 3
L is 200 i is 4
L is 200 i is 5
L is 220 i is 6
L is 220 i is 7
L is 240 i is 8
L is 240 i is 9
L is 260 i is 10
L is 260 i is 11
L is 280 i is 12
L is 280 i is 13
L is 300 i is 0
L is 300 i is 1
L is 320 i is 2
L is 320 i is 3
L is 340 i is 4
L is 340 i is 5
L is 360 i is 6
L is 360 i is 7
L is 380 i is 8
L is 380 i is 9
L is 400 i is 10
L is 400 i is 11
L is 420 i is 12
L is 420 i is 13
L is 440 i is 0
L is 440 i is 1
L is 460 i is 2
L is 460 i is 3
L is 480 i is 4
L is 480 i is 5
L is 500 i is 6
L is 500 i is 7
L is 520 i is 8
L is 520 i is 9
L is 540 i is 10
L is 540 i is 11
L is 560 i is 12
L is 560 i is 13
11 years ago
sorry about that I just copied and pasted it from the catalina.out the word INFO was from the log itself.
14 years ago
Hi,

my servlet is in /webapps/jspbook/WEB-INF/classes/com/jsp

here is my web.xml located in /webapps/jspbook/WEB-INF/



this is the catalina.out

11-Aug-07 10:53:04 AM org.apache.catalina.startup.HostConfig checkResources
INFO: Reloading context [/jspbook]
11-Aug-07 10:53:04 AM org.apache.catalina.core.StandardContext stop
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/jspbook] has not been started
11-Aug-07 10:53:48 AM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: start: Starting web application at '/jspbook'
11-Aug-07 10:53:48 AM org.apache.catalina.core.StandardContext start
INFO: Container org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/jspbook] has already been started
11-Aug-07 10:53:48 AM org.apache.catalina.core.ApplicationContext log
INFO: HTMLManager: list: Listing contexts for virtual host 'localhost'
11-Aug-07 10:54:12 AM org.apache.catalina.core.ApplicationContext log
INFO: Marking servlet HelloWorld as unavailable
11-Aug-07 10:54:12 AM org.apache.catalina.core.ApplicationContext log
SEVERE: Error loading WebappClassLoader

delegate: false

repositories:

/WEB-INF/classes/

----------> Parent Classloader:

org.apache.catalina.loader.StandardClassLoader{urls=[file:/var/lib/tomcat5/shared/classes/], parent=org.apache.catalina.loader.StandardClassLoader{urls=[file:/var/lib/tomcat5/common/classes/,file:/var/lib/tomcat5/common/i18n/tomcat-i18n-es.jar,file:/var/lib/tomcat5/common/i18n/tomcat-i18n-ja.jar,file:/var/lib/tomcat5/common/i18n/tomcat-i18n-en.jar,file:/var/lib/tomcat5/common/i18n/tomcat-i18n-fr.jar,file:/usr/share/java/xml-commons-apis-1.3.02.jar,file:/usr/share/java/xerces-j2-2.7.1.jar,file:/usr/share/java/jakarta-commons-pool-1.3.jar,file:/usr/share/java/tomcat5/naming-factory-5.5.23 .jar,file:/usr/share/java/libgcj-4.1.1.jar,file:/usr/share/java/tomcat5-jsp-2.0-api-5.5.23.jar,file:/usr/share/java/ant-1.6.5.jar,file:/usr/share/java/libgcj-4.1.1.jar,file:/usr/share/java/classpathx-mail-1.3.1-monolithic-1.1.1.jar,file:/usr/share/eclipse/plugins/org.eclipse.jdt.core_3.2.3.v_686_R32x.jar,file:/usr/share/java/jakarta-commons-el-1.0.jar,file:/usr/share/java/jasper5-runtime-5.5.23.jar,file:/usr/share/java/tomcat5-servlet-2.4-api-5.5.23.jar,file:/usr/share/java/jakarta-commons-loggi ng-api-1.0.4.jar,file:/usr/share/java/mx4j/mx4j-3.0.1.jar,file:/usr/share/java/tomcat5/naming-resources-5.5.23.jar,file:/usr/share/java/jakarta-commons-dbcp-1.2.1.jar,file:/usr/share/java/classpathx-jaf-1.0.jar,file:/usr/share/java/jakarta-commons-collections-3.1.jar,file:/usr/share/java/jasper5-compiler-5.5.23.jar,file:/usr/share/java/geronimo/spec-jta-1.0.1B-rc2.jar], parent=gnu.gcj.runtime.SystemClassLoader{urls=[file:/usr/lib/jvm/java/lib/tools.jar,file:/usr/share/tomcat5/bin/bootstrap.jar,file:/usr/share/tomcat5/bin/commons-logging-api.jar,file:/usr/share/java/mx4j/mx4j-impl.jar,file:/usr/share/java/mx4j/mx4j-jmx.jar], parent=gnu.gcj.runtime.ExtensionClassLoader{urls=[], parent=null}}}}

com.jspbook.HelloWorld
java.lang.ClassNotFoundException: com.jspbook.HelloWorld
at org.apache.catalina.loader.WebappClassLoader.loadClass(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.loader.WebappClassLoader.loadClass(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapper.loadServlet(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapper.allocate(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapperValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardContextValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardHostValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.valves.ErrorReportValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardEngineValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.connector.CoyoteAdapter.service(catalina-5.5.23.jar.souqoq08.so)
at org.apache.coyote.http11.Http11Processor.process(tomcat-http-5.5.23.jar.so)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(tomcat-http-5.5.23.jar.so)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(tomcat-util-5.5.23.jar.so)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(tomcat-util-5.5.23.jar.so)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(tomcat-util-5.5.23.jar.so)
at java.lang.Thread.run(libgcj.so.7rh)
11-Aug-07 10:54:12 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Allocate exception for servlet HelloWorld
java.lang.ClassNotFoundException: com.jspbook.HelloWorld
at org.apache.catalina.loader.WebappClassLoader.loadClass(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.loader.WebappClassLoader.loadClass(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapper.loadServlet(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapper.allocate(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardWrapperValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardContextValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardHostValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.valves.ErrorReportValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.core.StandardEngineValve.invoke(catalina-5.5.23.jar.souqoq08.so)
at org.apache.catalina.connector.CoyoteAdapter.service(catalina-5.5.23.jar.souqoq08.so)
at org.apache.coyote.http11.Http11Processor.process(tomcat-http-5.5.23.jar.so)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(tomcat-http-5.5.23.jar.so)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(tomcat-util-5.5.23.jar.so)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(tomcat-util-5.5.23.jar.so)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(tomcat-util-5.5.23.jar.so)
at java.lang.Thread.run(libgcj.so.7rh)

please help thanks in advance
[ August 12, 2007: Message edited by: Bear Bibeault ]
14 years ago
there's seems to be a way to lock it but no way to unlock it remotely. If I use Full-screen exclusive mode of awt do you guys think this will do the trick?
14 years ago
thanks for the quick reply Jesper
14 years ago
how can I create code that will lock the computer? I'm trying to create a software that can remotely lock/unlock a computer running on windows xp.
Basically if I know how to create a method to lock/unlock the computer locally (that is keyboard is unusable and screen is blank/blue) then I will call that method remotely through RMI.

TIA

BTW, computer will be on local network only.
14 years ago
thanks so much Jim, boy that makes my coding a lot easier
14 years ago

Whenever you create a Timer, it creates and starts a single thread of its own, which it uses to fun all the tasks you schedule for it, in order, at the times you schedule (or as soon as possible thereafter). A single Timer should never run its scheduled TimerTasks in parallel - but if you had two different Timers, they could run their TimerTasks in parallel.



does this mean that I can create an application that uses Timers for different objects without having to put my Timers in their own threads?

say for example I have a calulator program that I can only use for a fixed amount of time, one I start punching a key the timer automatically starts, I can continue using the calculator while the Timer runs on it's own thread without putting it in a new thread?
14 years ago
sorry if this has been asked. where can I download the java 6 api. I need to install it to my computer as we are having intermittent internet connection. TIA
14 years ago
Hello,

Pls. help

heres the error:

PayCalc.java:34: cannot find symbol
symbol : constructor JComboBox(int[])
location: class javax.swing.JComboBox
dayOfMonth = new JComboBox(days);

15 years ago
Isn't it true that the main() method is in the daemon thread? which is also the main thread?
hmmmm, shouldn't an array be initialized as:

int [] myArray = {1,2,4}//use{} instead of ()
15 years ago