Welcome to Dream.In.Code
Become a Java Expert!

Join 150,386 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,123 people online right now. Registration is fast and FREE... Join Now!




Applet Not Repainting object

 
Reply to this topicStart new topic

Applet Not Repainting object, i just wanted to move a ball, but turned into an etch a sketch

HowiChoose2Live
23 Jan, 2008 - 08:30 AM
Post #1

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 8

My Ball wont repaint. Im just trying to get a basic understanding of keylisteners in applets. I know its probably just something simple. Any help would be appreciated.


CODE
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
public class Key1 extends JApplet implements KeyListener{
public int cx = 500, cy = 300;



    public void init()
    {
        addKeyListener(this);
        setSize (1000, 600);
        setBackground(Color.white);
      
    }
    
   public void paint(Graphics page){
    
        page.setColor(Color.black);
        page.fillOval(cx - 13, cy - 13, 26, 26);
        

    }
    
        //Called when the key is pressed down.
    public void keyPressed(KeyEvent e){
      
    if(e.getKeyCode()==37) {//check if the Keycode is 37 which is left
    cx = cx - 5;//move left
    repaint();
}
    if(e.getKeyCode()==38) {//check if the Keycode is 38 which is left
    cy = cy - 5;//move up
    repaint();
}
    if(e.getKeyCode()==39) {//check if the Keycode is 39 which is left
    cx = cx + 5;//move right
    repaint();
}
    if(e.getKeyCode()==40) {//check if the Keycode is 40 which is left
    cy = cy + 5;//move down
    repaint();
}

}
    
    //Called when the key is released    
    public void keyReleased(KeyEvent e){
    
    }
    //Called when a key is typed
    public void keyTyped(KeyEvent e){
    
    
}
  
}  

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Applet Not Repainting Object
23 Jan, 2008 - 10:09 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,011



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
It repaints for me. Please check again, or explain in detail what happens when you start the applet.
User is offlineProfile CardPM
+Quote Post

baavgai
RE: Applet Not Repainting Object
23 Jan, 2008 - 10:15 AM
Post #3

Dreaming Coder
Group Icon

Joined: 16 Oct, 2007
Posts: 2,290



Thanked: 136 times
Dream Kudos: 475
Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua, Cheese

My Contributions
The canvas is "helping" you by only redrawing what it feels it needs to, it leaves everthing else alone. Since you haven't drawn over your old drawing, it's still good. This kind of behavior is hit or miss in java, some components do it by default, some don't.

Try explicitly clearing, like so
CODE

public void paint(Graphics page){
    page.clearRect(cx-13-5, cy-13-5, 26 + 10, 26 + 10);
    page.setColor(Color.black);
    page.fillOval(cx - 13, cy - 13, 26, 26);
}


Hope this helps.

User is online!Profile CardPM
+Quote Post

HowiChoose2Live
RE: Applet Not Repainting Object
23 Jan, 2008 - 11:31 AM
Post #4

New D.I.C Head
*

Joined: 9 Jan, 2008
Posts: 8

Well it repaints, it's just that it leaves the old image up as well so instead of getting a moving ball, i just draw a line. was gonna attempt to just erase the old one like baavgai suggested but i was just wondering if there was an easier way and perhaps if i forgot something simple to fix it.
User is offlineProfile CardPM
+Quote Post

gl3thr0
RE: Applet Not Repainting Object
25 Jan, 2008 - 11:56 AM
Post #5

D.I.C Head
**

Joined: 27 Oct, 2007
Posts: 229



Thanked: 5 times
My Contributions
a simpler way ?? theres a more basic way if u dont understand wt clearRect() does.. u could simply clear the whole screen.. and then repaint. but its esseffiecient and causes the applet to do more work then it needs bt w/e its 2 lines


***
the MAIN differance between clearRect and fillRect is that clearRect will use the color of ur bkBround while fill rect will only use the color u tell it to use
***

CODE

page.setColor(Color.white);
page.fillRect(0, 0, this.size().width,this.size().height);


//and then the rest of ur code  page.setColor(Color.black)....


this works it would be smarter to use clearRect() then fillRect()


so u could use
CODE

page.fillClear(0, 0, this.size().width,this.size().height);


//and then the rest of ur code  page.setColor(Color.black)....




anyway i hope this helped
there is one more way but its more complicated u have to override the method update().. (which is called by repaint(). so if ur interested look into it

User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 04:58PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month