IITDU Forum
Would you like to react to this message? Create an account in a few clicks or log in to continue.

Who can find out the error in this program???

4 posters

Go down

Java Who can find out the error in this program???

Post by BIT0201-farhan Wed Oct 27, 2010 1:31 am

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.... Very Happy

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
BIT0201-farhan
Alpha Release
Alpha Release

Course(s) :
  • BIT

Blood Group : AB+
Posts : 45
Points : 95

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0122-Amit Thu Oct 28, 2010 6:33 am

A very nice one Very Happy
rep++

I am impressed at your devilish code Razz Razz
Anyone who tries to fix it will need... 2/3 tries I suppose Very Happy Very Happy

Keep it up Farhan. I realized that I almost forgot one of the basic things after fixing your code Very Happy Very Happy
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0102-Mohaimin Mon Nov 22, 2010 2:25 pm

I dont find any error! Did you mean a bug? I mean why the lines are not drawn?
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0112-Rokon Mon Nov 22, 2010 3:22 pm

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.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0122-Amit Mon Nov 22, 2010 3:40 pm

*looks around*

Erm, who are you talking about? I don't see any lord around here :/



The problem with the code should be in error category. :

Again, it can be in bug category:

Then again (yeah, again and again and again... I can keep this up all the day), it can be considered
a defect:

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.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0112-Rokon Mon Nov 22, 2010 4:08 pm

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 ?
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0122-Amit Mon Nov 22, 2010 4:32 pm

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 Very Happy

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?


BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0112-Rokon Mon Nov 22, 2010 5:02 pm

BIT0122-Amit wrote:


Keep it up Farhan. I realized that I almost forgot one of the basic things after fixing your code Very Happy Very Happy

you said, you fixed it, and so asked, how did you solve it.
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0122-Amit Mon Nov 22, 2010 5:09 pm

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.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0102-Mohaimin Mon Nov 22, 2010 8:08 pm

I was not very clear that Farhan was throwing a challenge or was asking for help.

Now, I have already explained the problem here haven't I?
I dont think so.

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
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0122-Amit Mon Nov 22, 2010 10:40 pm

I was sure because of this sentence:

Let's see who can find it.... Very Happy

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.
BIT0122-Amit
BIT0122-Amit
Founder
Founder

Course(s) :
  • BIT

Blood Group : O+
Posts : 4187
Points : 6605

https://iitdu.forumotion.com

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0102-Mohaimin Tue Nov 23, 2010 2:20 am

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 Wink


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
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0112-Rokon Tue Nov 23, 2010 8:11 am

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.....
BIT0112-Rokon
BIT0112-Rokon
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : O+
Posts : 673
Points : 1269

http://blog.codexplo.org

Back to top Go down

Java Re: Who can find out the error in this program???

Post by BIT0102-Mohaimin Tue Nov 23, 2010 12:29 pm

Thats an excellent way to explain event!
++
BIT0102-Mohaimin
BIT0102-Mohaimin
Programmer
Programmer

Course(s) :
  • BIT

Blood Group : B+
Posts : 415
Points : 715

Back to top Go down

Java Re: Who can find out the error in this program???

Post by Sponsored content


Sponsored content


Back to top Go down

Back to top

- Similar topics

 
Permissions in this forum:
You cannot reply to topics in this forum