Bypass errors in either of the codes [message #1772676] |
Wed, 13 September 2017 14:24  |
Eclipse User |
|
|
|
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 00:07] by Moderator
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.08059 seconds