Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Bypass errors in either of the codes
Bypass errors in either of the codes [message #1772676] Wed, 13 September 2017 18:24 Go to next message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
I want to bypass System.out.println() and instead output in GUI.

I have framed two versions of the same program to bypass the System.out method.

(1) In the following first one it shows the graphics window, but no graphics, and error messages, given below, the program, and the error messages below the program:
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;

/*
 *Simple countdown timer demo of java.util.Timer facility.
 */

public class Countdown {
    public static void main(final String args[]) {
    	//---------------------------------
    	Frame f = new Frame("Draw string")  {
    	    public void paint (Graphics g) {
    	       Graphics2D g2 = (Graphics2D) g;

    	       Font font = new Font("Arial", Font.BOLD, 48);
    	       g2.setFont(font);                     
    	       g2.setColor(Color.RED);
    	       g2.drawString(Integer.toString(i), 40, 80);
    	}
    	}; 
    	   
    	   f.setVisible(true);
    	   f.setSize(550, 120);
    	// ---------------------------------
        if (args.length != 1) {
            System.err.println("Usage: java Countdown <time in secs>");
            System.exit(1);
        }
        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
        	
            int i = Integer.parseInt(args[0]);
            public void run() {
            	/*
            	//System.out.println(i--);
                */
            	i--;
            	            	
                if (i< 0)
                    timer.cancel();
            }
        }, 0, 1000);
    }
} 

Error Messages:
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
	i cannot be resolved to a variable

	at Countdown$1.paint(Countdown.java:28)
	at sun.awt.RepaintArea.paintComponent(RepaintArea.java:264)
	at sun.awt.X11.XRepaintArea.paintComponent(XRepaintArea.java:73)
	at sun.awt.RepaintArea.paint(RepaintArea.java:240)
	at sun.awt.X11.XComponentPeer.handleEvent(XComponentPeer.java:694)
	at java.awt.Component.dispatchEventImpl(Component.java:4725)
	at java.awt.Container.dispatchEventImpl(Container.java:2103)
	at java.awt.Window.dispatchEventImpl(Window.java:2587)
	at java.awt.Component.dispatchEvent(Component.java:4475)
	at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:675)
	at java.awt.EventQueue.access$300(EventQueue.java:96)
	at java.awt.EventQueue$2.run(EventQueue.java:634)
	at java.awt.EventQueue$2.run(EventQueue.java:632)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:116)
	at java.awt.EventQueue$3.run(EventQueue.java:648)
	at java.awt.EventQueue$3.run(EventQueue.java:646)
	at java.security.AccessController.doPrivileged(Native Method)
	at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:105)
	at java.awt.EventQueue.dispatchEvent(EventQueue.java:645)
	at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:275)
	at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:200)
	at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:190)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:185)
	at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:177)
	at java.awt.EventDispatchThread.run(EventDispatchThread.java:138)
Exception in thread "AWT-EventQueue-0" java.lang.Error: Unresolved compilation problem: 
	i cannot be resolved to a variable


I know partially why there exists a problem with the variable i. It is defined within the loop, not out.

How to report the loop value of i outside of the loop?

(2) The second form of the code:
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.util.Timer;
import java.util.TimerTask;



/*
 *Simple countdown timer demo of java.util.Timer facility.
 */

public class Countdown {
    public static void main(final String args[]) {
    	
        if (args.length != 1) {
            System.err.println("Usage: java Countdown <time in secs>");
            System.exit(1);
        }
        final Timer timer = new Timer();
        timer.scheduleAtFixedRate(new TimerTask() {
        	
            int i = Integer.parseInt(args[0]);
            public void run() {
            	/*
            	//System.out.println(i--);
                */
            	i--;
            	//---------------------------------
            	Frame f = new Frame("Draw string")  {
            	    public void paint (Graphics g) {
            	       Graphics2D g2 = (Graphics2D) g;

            	       Font font = new Font("Arial", Font.BOLD, 48);
            	       g2.setFont(font);                     
            	       g2.setColor(Color.RED);
            	       g2.drawString(Integer.toString(i), 40, 80);
            	}
            	}; 
            	   
            	   f.setVisible(true);
            	   f.setSize(550, 120);
            	// ---------------------------------            	
                if (i< 0)
                    timer.cancel();
            }
        }, 0, 1000);
    }
}

This code runs without errors, but opens multiple windows. I know why. Because it is within the loop.
Which is why I removed the drawString outside of the loop in the first code, However, the first code runs into problems, as stated. The panel is opened, but nothing is drawn on the panel.

There is a reply in the Stack Overflow. But it is beyond my scope of understanding without explicit examples.

[Updated on: Thu, 14 September 2017 04:07]

Report message to a moderator

Re: Bypass errors in either of the codes [message #1772677 is a reply to message #1772676] Wed, 13 September 2017 18:49 Go to previous messageGo to next message
Nitin Dahyabhai is currently offline Nitin DahyabhaiFriend
Messages: 3413
Registered: July 2009
Senior Member

This isn't a programming support forum, it's for questions specific to Eclipse. You probably want something like the Beginner forum at JavaRanch: https://coderanch.com/f/33/java

As you've already surmised, your problem has to do with a variable's scope. https://docs.oracle.com/javase/tutorial/information/glossary.html#Scope


Nitin Dahyabhai
Eclipse Web Tools Platform
Re: Bypass errors in either of the codes [message #1772699 is a reply to message #1772677] Thu, 14 September 2017 05:01 Go to previous message
RajibKumar Bandopadhyay is currently offline RajibKumar BandopadhyayFriend
Messages: 58
Registered: July 2014
Member
Nitin Dahyabhai wrote
This isn't a programming support forum ... ... Beginner forum at JavaRanch ...
I am aware,

In CodeRanch I have had multiple ill-experiences. There indeed are a few gentle human beings there, but they can't be approached by private messaging. Alas!

Please have a look at their general attitude, here: CodeRanch Private Moosages
We don't have 'private' messages. The closest we have is 'purple mooseages'. This is where you send a message to one other individual, and it is made available for all of the staff to see too, so they can forward it to their co-workers for a good laugh. But since you tend to write the world's most boring messages, the staff ends up rarely looking at yours.
Would you call it humour? It appears as satire/humiliation to me.
  • Attachment: additional
    (Size: 0.25KB, Downloaded 44 times)

[Updated on: Fri, 15 September 2017 01:51]

Report message to a moderator

Previous Topic:How to fix bugs in Eclipse
Next Topic:Eclipse fails to initialy run makefile
Goto Forum:
  


Current Time: Sun Jun 16 06:31:07 GMT 2019

Powered by FUDForum. Page generated in 0.03201 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top