Who can find out the error in this program???
4 posters
Page 1 of 1
Who can find out the error in this program???
Hello everyone,
This is a very normal code of java.Tothing special.however,there is an error in this code...Let's see who can find it....
This is a very normal code of java.Tothing special.however,there is an error in this code...Let's see who can find it....
- Code:
package myTest;
import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
;
public class MyDrawing extends JPanel {
int[] Xpoints = { 30, 40, 90, 230, 245, 230, 130 }, Ypoints = { 30, 40, 90,
230, 245, 230, 130 };
public void letsDraw() {
Graphics g = getGraphics();
for (int i = 0; i < 5; i++) {
g.setColor(Color.BLACK);
g.drawLine(Xpoints[i], Ypoints[i], Xpoints[i + 1], Ypoints[i + 1]);
}
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setSize(500, 500);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawing d = new MyDrawing();
frame.add(d);
frame.setVisible(true);
d.letsDraw();
}
}
BIT0201-farhan- Alpha Release
- Course(s) :
- BIT
Blood Group : AB+
Posts : 45
Points : 95
Re: Who can find out the error in this program???
A very nice one
I am impressed at your devilish code
Anyone who tries to fix it will need... 2/3 tries I suppose
Keep it up Farhan. I realized that I almost forgot one of the basic things after fixing your code
I am impressed at your devilish code
Anyone who tries to fix it will need... 2/3 tries I suppose
Keep it up Farhan. I realized that I almost forgot one of the basic things after fixing your code
Re: Who can find out the error in this program???
I dont find any error! Did you mean a bug? I mean why the lines are not drawn?
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Who can find out the error in this program???
I didn't find any problem except an extra semicolon.
Whatever I know about getGraphics() is that, Graphics object will be null until something is visible.
Mohaimin said about bug!
I think, we should be clear about the three terms, error, bug and defect.
Error: Is an undesirable deviation from requirements. Something like, we are trying to add 3 +2 and expecting 5, but getting 6 somehow.
Bug: Is an error found BEFORE the application goes into production or missing Functionality.
Defect :Is an error found AFTER the application goes into production or missing Requirement
Btw LordAmit, you can tell us, what is the problem going on here.
Whatever I know about getGraphics() is that, Graphics object will be null until something is visible.
Mohaimin said about bug!
I think, we should be clear about the three terms, error, bug and defect.
Error: Is an undesirable deviation from requirements. Something like, we are trying to add 3 +2 and expecting 5, but getting 6 somehow.
Bug: Is an error found BEFORE the application goes into production or missing Functionality.
Defect :Is an error found AFTER the application goes into production or missing Requirement
Btw LordAmit, you can tell us, what is the problem going on here.
Re: Who can find out the error in this program???
*looks around*
Erm, who are you talking about? I don't see any lord around here :/
Then again (yeah, again and again and again... I can keep this up all the day), it can be considered
Anyway, the point of saying all these was that people still can not agree about the definitions about bug, defect, error, fault and failure. It is actually dependant on your workplace.
http://www.sqatester.com/bugsfixes/bugdefecterror.htm
Now, I have already explained the problem here haven't I?
Both Farhan, and I do not see any lines being drawn on JFrame in current code.
Erm, who are you talking about? I don't see any lord around here :/
- The problem with the code should be in error category. :
- Because although the code is correct(I guess minor issues like semi colones can be ignored), it does not show any drawing in the JFrame.
- Again, it can be in bug category:
- because a simple function like this should be a module of a big program. Which is yet to be developed
Then again (yeah, again and again and again... I can keep this up all the day), it can be considered
- a defect:
- as well. Because it is a standalone module, and there is no other functionality specified here. The developer(Farhan) developed it, and found an error.
Anyway, the point of saying all these was that people still can not agree about the definitions about bug, defect, error, fault and failure. It is actually dependant on your workplace.
http://www.sqatester.com/bugsfixes/bugdefecterror.htm
Now, I have already explained the problem here haven't I?
Both Farhan, and I do not see any lines being drawn on JFrame in current code.
Re: Who can find out the error in this program???
Im clear about error, bug and defect. Though already I was.
Btw why we cant see any lines being drawn on JFrame in current code ?
Btw why we cant see any lines being drawn on JFrame in current code ?
Re: Who can find out the error in this program???
Like I said, you and anyone else can be clear about bug, defect and error and at the same time, both you and him may define them in different ways.
We should just use problem
Anyway, that is an interesting question. The solution according to me is to use a separate thread to delay the drawing.
Otherwise, it might be drawn too fast.
The probable reason why we can not see it when we do not delay it, is that the drawMethod might not be able to keep up with the for loop.
But you are the programmer, Should not you explain things instead of asking?
We should just use problem
Anyway, that is an interesting question. The solution according to me is to use a separate thread to delay the drawing.
Otherwise, it might be drawn too fast.
The probable reason why we can not see it when we do not delay it, is that the drawMethod might not be able to keep up with the for loop.
But you are the programmer, Should not you explain things instead of asking?
Re: Who can find out the error in this program???
BIT0122-Amit wrote:
Keep it up Farhan. I realized that I almost forgot one of the basic things after fixing your code
you said, you fixed it, and so asked, how did you solve it.
Re: Who can find out the error in this program???
The solution according to me is to use a separate thread to delay the drawing.
Farhan is not asking for help, rather challenging others.
In such topics, I don't like giving direct codes as answers, rather the concepts.
Re: Who can find out the error in this program???
I was not very clear that Farhan was throwing a challenge or was asking for help.
I disliked the code at first glance because I knew it will not work.
The letsDraw() method works as it should. Still we cannot see the lines because those are erased after drawn. This is because the paintComponent() method is being executed after the letsDraw() method is executed. I suppose you know, paintComponent() erases all drawings on the component.
I dont think so.Now, I have already explained the problem here haven't I?
I disliked the code at first glance because I knew it will not work.
The letsDraw() method works as it should. Still we cannot see the lines because those are erased after drawn. This is because the paintComponent() method is being executed after the letsDraw() method is executed. I suppose you know, paintComponent() erases all drawings on the component.
Last edited by BIT0102-Mohaimin on Mon Nov 22, 2010 8:13 pm; edited 1 time in total (Reason for editing : There were two major typing mistakes)
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Who can find out the error in this program???
I was sure because of this sentence:
Anyway, when I read that code, I felt that something was familiar. And a quick search proved so.
https://iitdu.forumotion.com/programming-and-brainstorming-chamber-f91/using-graphics-to-draw-on-a-swing-awt-component-t845.htm
As you can see, he followed your code (including the name letsdraw), followed almost identical structure, have an identical integer array, and simply removed the Thread.sleep part.
So what I thought is like this:
1. He learnt from your code.
2. He copied it.
3. He removed a seemingly innocent part from your code, and walla! the drawing is gone.
Now, I didn't see any major difference, but is the paintComponent() method executed in your code?
And nah, I actually forgot what paintComponent() is.
Let's see who can find it....
Anyway, when I read that code, I felt that something was familiar. And a quick search proved so.
https://iitdu.forumotion.com/programming-and-brainstorming-chamber-f91/using-graphics-to-draw-on-a-swing-awt-component-t845.htm
As you can see, he followed your code (including the name letsdraw), followed almost identical structure, have an identical integer array, and simply removed the Thread.sleep part.
So what I thought is like this:
1. He learnt from your code.
2. He copied it.
3. He removed a seemingly innocent part from your code, and walla! the drawing is gone.
Now, I didn't see any major difference, but is the paintComponent() method executed in your code?
And nah, I actually forgot what paintComponent() is.
Re: Who can find out the error in this program???
As you can see, he followed your code (including the name letsdraw), followed almost identical structure, have an identical integer array, and simply removed the Thread.sleep part.
I saw that topic you gave link yesterday and thought that I should have locked that and hang a message that "you should not learn from this".
As much I can remember, I posted that topic for some emergency, perhaps we needed that for some reason....
Oh, I just recalled, it was for the scheduling project Mr Ahmedul Kabir sir gave us.
Whatever, I am locking and hanging the message in the title post.
Thanks anyway.
Now about the current problem,
Using the sleep is a solution of course. But that is the worst solution I can imagine. I used that in the linked post just to make the program work, understanding nothing.
There is something like EventDispatchThread associated with swing. This thing is really important for clear understanding of swing. I hope I will write a tutorial on EventDispatchThread after exam (I mean late December this year).
Now, basic is, the swing activity is done under EvenDispachThread, not the main thread. You may have noticed it when got an exception in a swing app. Now I understand why that sleep made the program work. The EventDispatchThread was calling the components' paintcomponent() methods and the main thread was sleeping! After waking up, main thread executed the letsDraw() method. So, the program seems fine!
Now resize the window. The lines will be vanished. Guess why
Last edited by BIT0129-tabassum on Wed Nov 24, 2010 3:24 am; edited 2 times in total (Reason for editing : Please use sir/madam after teachers name, its sound insulting when you do not use it after a faculty :))
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Re: Who can find out the error in this program???
I have a wish tor write a complete and brief tutorial on Swing components.
even already started.
I don't know when it will be finished. But for sure, I will complete it someday.
I can share my thought today..
Five things you need to know while working with swing. They are -
1. Events
2. Painting
3. Rendering
4. Double-Buffering
5. Threading
Im going to write on the five category basically. I have written a paragraph on event. Lets see what I have written .
Lets assume some one rings your doorbell. It is an event. In life, you encounter events that force you to suspend other activities and respond to them immediately. In Java it is something like that. In event Java represent activities that goes on user and application. You press a button, it creates an event. In Java all events are put onto a queue. The object java.awt.EventQueue is responsible for pulling the events off of this queue and dispatching them as appropriate. The dispatch mechanism of EventQueue is run on a single thread, called the Event Dispatch Thread (EDT).
you people can suggest me whether I'm going on right way or not.....
even already started.
I don't know when it will be finished. But for sure, I will complete it someday.
I can share my thought today..
Five things you need to know while working with swing. They are -
1. Events
2. Painting
3. Rendering
4. Double-Buffering
5. Threading
Im going to write on the five category basically. I have written a paragraph on event. Lets see what I have written .
Lets assume some one rings your doorbell. It is an event. In life, you encounter events that force you to suspend other activities and respond to them immediately. In Java it is something like that. In event Java represent activities that goes on user and application. You press a button, it creates an event. In Java all events are put onto a queue. The object java.awt.EventQueue is responsible for pulling the events off of this queue and dispatching them as appropriate. The dispatch mechanism of EventQueue is run on a single thread, called the Event Dispatch Thread (EDT).
you people can suggest me whether I'm going on right way or not.....
Re: Who can find out the error in this program???
Thats an excellent way to explain event!
++
++
BIT0102-Mohaimin- Programmer
- Course(s) :
- BIT
Blood Group : B+
Posts : 415
Points : 715
Similar topics
» find the mistake
» find mistake
» Find your computer's Gender : Male or Female?
» XAMPP Error: Warning: file_get_contents(lang.tmp)
» C program solutions
» find mistake
» Find your computer's Gender : Male or Female?
» XAMPP Error: Warning: file_get_contents(lang.tmp)
» C program solutions
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Tue Sep 29, 2015 2:45 pm by Sophiawood
» Cisco EHWIC SFP/GE WAN Card
Mon Sep 07, 2015 11:08 am by Sophiawood
» Huawei S1700-28GFR-4P-AC Price
Thu Jun 25, 2015 2:31 pm by Sophiawood
» teach yourself C++ / Herbert Schildt Solutions
Wed Jun 03, 2015 1:52 pm by Abdullah Al Noman
» teach yourself c by herbert schildt pdf
Wed May 13, 2015 11:01 pm by Raquib Ridwan
» ASA 5506X With Firepower ASA5506-K9
Fri Apr 10, 2015 4:31 pm by Sophiawood
» New Trends in Deal Business
Tue Feb 03, 2015 9:38 pm by nersoa
» PoE Power Allocation for WS-C2960S-24PS-L
Wed Nov 05, 2014 11:12 am by Sophiawood
» How to cure back pain
Fri Oct 31, 2014 7:15 pm by Bergen Guildford