public void pencil1()
        addMouseListener(new MouseAdapter(){
            public void mousePressed(MouseEvent e){
                oldX = e.getX();
                oldY = e.getY();
        //if the mouse is pressed it sets the oldX & oldY
        //coordinates as the mouses x & y coordinates
        addMouseMotionListener(new MouseMotionAdapter(){
            public void mouseDragged(MouseEvent e){
                currentX = e.getX();
                currentY = e.getY();
                if(graphics2D != null && k1==1)
                    graphics2D.drawLine(oldX, oldY, currentX,currentY);
                oldX = currentX;
                oldY = currentY;

hello, i'm having problem to understand the code. can somebody help me? this isn't my code, i'm just trying to learn

if this is not your code, don't try to "understand" it.
if you want to understand this: learn Java until you've reached the level that you can understand it. don't just copy-paste code you found on the web, ask someone to explain it, and think you learn something that way.
how much experience do you have in Java ?

well i dont have a lot experience in java because i have never learn it properly. my lecturer ask us to create an app using java for our final project but doesnt even teach us anything so i am stressed out right now

what kind of course are you talking ? no lecturer in his right mind expects you to write an app in a language you don't know.
by just copy pasting code into a working total, you are not writing any code, let alone something worth to be submitted as a final project.

multimedia computing. the things is, we are supposed to take the java subject on the next semester. on this semester, we are taking graphic subject and there is a bit of java in there. but the lecturer ask us to learn by our own. i was pretty devastated when the lecturer told us about the final project. most of us complained because we havent learn any java at all but the lecturer didnt care and ask us to proceed with the project 'by just copying codes from the internet'.

i did try to learn java by myself in order to write my own code but i realize that there is no time for that because the due date of submitting the project is very near and i am very afraid right now.

sorry for asking annoying question here though

so: your final project should be something you "copied from the net" ? seems to me, that course (at least this part of it) is money thrown away, and the lecturer either doesn't know/understand his responsibilities, or doesn't know Java himself.

what is the deadline for this project ? I'm sorry to tell you, but this is not a decent way to learn Java. let's face it: by working like this, you won't even be learning anything.

the point of this forum is to help people understand Java well enough to write code like this on their own, or to help them understand the errors in the code they're written.
we don't just provide ready-made code, for simple reasons:

  1. you won't learn anything, it's as simple as that.
  2. copy pasting code from the net is cheating, and, no matter what your lecturer tells you, will give them reason and right to throw you off campus.

learning Java is not something you do overnight, I'm afraid.