Not ".repaint" but .... ?

Another simple one ....
I have a button that calls the repaint method of MyCanvas "can" :
if (label == "Display") {
can.repaint();
Now, this seems to clear my canvas and draw it again. I want it to just draw over the top of what is there already. i have tried can.paint, but get an error:
"Error: methos paint() not found in class .....MyCanvas"
Am I not including something or what?
Any ideas?
this should be as simple as anything!

You need to define the factory method paint() in your class. The call to repaint will implicitly call this paint() method. What you can do is define the paint method with a single call to the superclass' paint method. For more on paint see: as a starting point.
http://java.sun.com/docs/books/tutorial/applet/overview/componentMethods.html

Similar Messages

  • Repaint() method 's not work but paint(getGraphics()) does, why?

    I've just read the following code ( in the Core Java 2 advance features vol 2 book):
      import java.awt.*;
      import java.awt.event.*;
       import java.awt.geom.*;
       import java.util.*;
       import javax.swing.*;
          Shows an animated bouncing ball.
      public class Bounce
         public static void main(String[] args)
            JFrame frame = new BounceFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.show();
         The frame with canvas and buttons.
      class BounceFrame extends JFrame
            Constructs the frame with the canvas for showing the
            bouncing ball and Start and Close buttons
         public BounceFrame()
            setSize(WIDTH, HEIGHT);
            setTitle("Bounce");
            Container contentPane = getContentPane();
            canvas = new BallCanvas();
            contentPane.add(canvas, BorderLayout.CENTER);
            JPanel buttonPanel = new JPanel();
            addButton(buttonPanel, "Start",
               new ActionListener()
                     public void actionPerformed(ActionEvent evt)
                        addBall();
            addButton(buttonPanel, "Close",
               new ActionListener()
                     public void actionPerformed(ActionEvent evt)
                       System.exit(0);
            contentPane.add(buttonPanel, BorderLayout.SOUTH);
            Adds a button to a container.
            @param c the container
            @param title the button title
            @param listener the action listener for the button
         public void addButton(Container c, String title,
            ActionListener listener)
            JButton button = new JButton(title);
            c.add(button);
            button.addActionListener(listener);
            Adds a bouncing ball to the canvas and makes
            it bounce 1,000 times.
         public void addBall()
            try
               Ball b = new Ball(canvas);
               canvas.add(b);
               for (int i = 1; i <= 1000; i++)
                  b.move();
                  Thread.sleep(5);
            catch (InterruptedException exception)
         private BallCanvas canvas;
         public static final int WIDTH = 450;
         public static final int HEIGHT = 350;
        The canvas that draws the balls.
    class BallCanvas extends JPanel
            Add a ball to the canvas.
            @param b the ball to add
       public void add(Ball b)
           balls.add(b);
        public void update(Graphics g) {
             super.update(g);
             System.out.println("Test");
        public void paintComponent(Graphics g)
          super.paintComponent(g);
          Graphics2D g2 = (Graphics2D)g;
          for (int i = 0; i < balls.size(); i++)
             Ball b = (Ball)balls.get(i);
             b.draw(g2);
            // System.out.println("Test");
        private ArrayList balls = new ArrayList();
        A ball that moves and bounces off the edges of a
       component
    class Ball
            Constructs a ball in the upper left corner
            @c the component in which the ball bounces
        public Ball(Component c) { canvas = c; }
           Draws the ball at its current position
           @param g2 the graphics context
        public void draw(Graphics2D g2)
           g2.fill(new Ellipse2D.Double(x, y, XSIZE, YSIZE));
          Moves the ball to the next position, reversing direction
          if it hits one of the edges
       public void move()
          x += dx;
          y += dy;
           if (x < 0)
              x = 0;
              dx = -dx;
         if (x + XSIZE >= canvas.getWidth())
             x = canvas.getWidth() - XSIZE;
              dx = -dx;
           if (y < 0)
             y = 0;
              dy = -dy;
           if (y + YSIZE >= canvas.getHeight())
              y = canvas.getHeight() - YSIZE;
              dy = -dy;
           //canvas.paint(canvas.getGraphics());//this would OK.
           canvas.repaint();//This not work, please tell me why?
        private Component canvas;
        private static final int XSIZE = 15;
       private static final int YSIZE = 15;
       private int x = 0;
       private int y = 0;
       private int dx = 2;
       private int dy = 2;
    }this program create a GUI containing a "add ball" button to creat a ball and make it bounce inside the window. ( this seems to be stupid example but it is just an example of why we should use Thread for the purpose ).
    Note: in the move() method, if i use canvas.repaint() then the ball is not redrawn after each movement. but if i use canvas.paint(canvas.getGraphics()) then everythings seem to be OK.
    Another question: Still the above programe, but If I create the ball and let it bounce in a separate thread, then canvas.repaint() in the move method work OK.
    Any one can tell me why? Thanks alot !!!

    I don't know why the one method works. Based on my Swing knowledge neither method should work. Did you notice that the JButton wasn't repainted until after the ball stopped bouncing. That is because of the following code:
    for (int i = 1; i <= 1000; i++)
        b.move();
        Thread.sleep(5);
    }This code is attempting to move the ball and then sleep for 5 milliseconds. The problem with this code is that you are telling the Event Thread to sleep. Normally painting events are added to the end of the Event Thread to allow for multiple painting requests to be combined into one for painting efficiency. When you tell the Thread to sleep then the GUI doesn't get a chance to repaint itself.
    More details about the EventThread can be found in the Swing tutorial:
    http://java.sun.com/docs/books/tutorial/uiswing/misc/threads.html
    This explains why the button isn't repainted, but it doesn't explain why the ball bounces. I don't know the answer to this for sure, but I do know that there is a method called paintImmediately(...) which causes the painting to be performed immediately without being added to the end of the Event Thread. This means the painting can be done before the Thread sleeps. Maybe the canvas.paint(....) is somehow invoking this method.
    When you click on the button this code is executed in the EvWell in Swing, all GUI painting is done on the Event Thread

  • Application's main window is not repainted

    Hello everybody.
    The system I am using
    Product Version: NetBeans IDE 6.5 (Build 200811100001)
    Java: 1.6.0_12; Java HotSpot(TM) Client VM 11.2-b01
    System: Windows Vista version 6.0 running on x86; Cp1251; ru_RU (nb)I created Java Desktop application (New project - Java Desktop Application (no CRUD)) and put a JTable like this:
    mainPanel[JPanel]
        jScrollPane1[JScrollPane]
            jTable1[JTable]
    I did not do anything more. But after building project I have a problem with my application - main window does not repaint itself after another window is positioned over it(calculator, for example). It repaints only after I resized main window or something like that ...
    What am I doing wrong?
    Thank you.

    I found a solution - to resolve this bug I have to set -Dsun.java2d.noddraw=true VM flag.
    I found the same bug in a database - [4139083|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4139083] and it's submit date is *15-MAY-1998* !!!
    There is also an issue about that kind of a problem - [6343853|http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6343853], submit date 31-OCT-2005.
    So, I guess unfortunately there are a lot of things to think about ...

  • Whole screen is not repainted, need debug tips

    We are running an application which uses some jogl for animaties.
    Sometimes when the animation is finished and we set the GLCanvas to invisible, the normal
    java JPanels are not repainted.
    Also alt-tab to another windows application and back will not force a redraw.
    The application itself is still working, clicking on a (now) not visible button will trigger
    the jogl animations, and these are show correctly.
    Also from logging it appear that the AWT-thread is not blocking.
    We are looking for tips that can help find us the problem from logging
    as it only appears a few times a week and it is not reproducable.

    The problem is that most of the time it works, but after some hours/days, the java panels are not repainted.
    There is no memory leak.
    paintComponent is not overwritten, a object is made visible to do the animation.
    Pseudo code:
    public class AnimateTilePopup extends JPanel {
    GLCanvas glCanvas;
    public AnimateTilePopup() {
    super(null);
    setOpaque(true);
    glCanvas = new GLCanvas();
    glCanvas.setLocation(0,0);
    glCanvas.setSize(width, height);
    glCanvas.setVisible(false);
    glCanvas.setFocusable(false);
    add(glCanvas);
    flipRendererZoom = new AnimateRendererZoom();
    glCanvas.addGLEventListener(flipRendererZoom);
    public startAnimation() {
    glCanvas.setVisible(true);
    this.setVisible(true);
    public stopAnimation() {
    glCanvas.setVisible(false);
    this.setVisible(false);
    So sometimes when we call stopAnimation(), the animation is stoppped, but the underlying java JPanels are not repainted.
    When we now call startAnimation, the animation is shown and when stopped, no repaint for the normal JPanels.
    Most times it is working perfectly, only sometimes it failes.
    What can be the cause or what logging can be gather that might gives us a clue how to fix this problem.

  • Can you repaint certain elements (ie a ball) while NOT repainting the board

    I'm really just starting out writing java applets. I don't use Applet, i use JApplet because the coding is much more familiar. I understand that this might be a bad decision and if it is, please tell me! Anyway, my question is: Can I have the paint method (called via repaint()) paint the board one time, then only repaint the ball and it's constant change in position? I want to do this because the only way I can think of to have constant motion of the ball (I'm trying to simulate Pong) is by adding repaint() to the end of the paint function. This causes the applet to repaint very rapidly and everything gets all cut up and looks like crap. Any ideas on how I can make it run smoother? my code is teh following, if it helps (I realize that it isn't really commented, I am very messy and I do apologize):
    import java.util.Random;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.Date;
    public class NewProj extends JApplet implements ActionListener, MouseListener, MouseMotionListener, KeyListener{
    /** Creates a new instance of NewProj */
    private int xBar1,yBar1,xBar2,yBar2;
    private String code;
    private int bar1H, bar2H, yMovement, xMovement;
    private int frame;
    private int ballX,ballY;
    private boolean hitTop,hitBottom,hitLeft,hitRight,hitPaddle1,hitPaddle2;
    private int score1,score2;
    private Date date;
    public NewProj() {
    date = new Date();
    score1=0;
    score2=0;
    ballX=175;
    ballY=125;
    yMovement=ballY+3;
    xMovement=ballX+3;
    frame=0;
    bar1H=40;
    bar2H=40;
    code="";
    xBar1=30;
    yBar1=130;
    xBar2=320;
    yBar2=130;
    this.addMouseMotionListener(this);
    this.addMouseListener(this);
    this.addKeyListener(this);
    public void paint(Graphics g){
    super.paint(g);
    g.drawString("Score: "+score1,220,20);
    g.drawString("Score: " + score2,80,20);
    if(frame==25){
    //initiate a and xMovement as moving linearly with a slope of 1.
    yMovement=ballY+3;
    xMovement=ballX+3;
    if(ballX<=xBar1+8 && ballX>=xBar1 && ballY<=yBar1+bar1H && ballY>=yBar1){
    hitPaddle1=true;
    hitPaddle2=false;
    hitRight=false;
    hitLeft=false;
    if(ballX<=xBar2+8 && ballX>=xBar2 && ballY<=yBar2+bar2H && ballY>=yBar2){
    hitPaddle2=true;
    hitRight=false;
    hitLeft=false;
    //check if the ball hit the Top
    if(ballY<1){
    hitBottom=false;
    hitTop=true;
    if(ballY>250){
    hitTop=false;
    hitBottom=true;
    if(ballX<10){
    hitPaddle2=false;
    hitPaddle1=false;
    hitRight=false;
    hitLeft=true;
    score1++;
    if(ballX>350){
    hitPaddle2=false;
    hitPaddle1=false;
    hitLeft=false;
    hitRight=true;
    score2++;
    if(hitBottom){
    yMovement=ballY-3;
    if(hitTop){
    yMovement=ballY+3;
    if(hitLeft){
    xMovement=ballX+3;
    if(hitRight)
    xMovement=ballX-3;
    if(hitPaddle1){
    xMovement=ballX+3;
    if(hitPaddle2){
    xMovement=ballX-3;
    System.out.println(yMovement+" is yMovement " + "and" + xMovement + " is xmove");
    ballY=yMovement;
    ballX=xMovement;
    frame=0;
    repaint();
    if(code.equals("penis")){
    if(bar1H>=60)
    bar1H=60;
    else bar1H+=5;
    code="";
    //top & bottom
    g.drawLine(0,0,350, 0);
    g.drawLine(0,250,350,250);
    //left & right sides
    g.drawLine(0,0,0,250);
    g.drawLine(350,0,350,250);
    //Midline
    g.drawLine(175,0,175,250);
    //ball
    g.setColor(Color.MAGENTA);
    g.fillOval(ballX,ballY,8,8);
    g.setColor(Color.RED);
    g.fillRect(xBar1,yBar1,5,bar1H);
    g.setColor(Color.BLUE);
    g.fillRect(xBar2,yBar2,5,bar2H);
    frame++;
    for(int b = 0; b<1000;b++){
    int hilbo=b+3;
    repaint();
    public void mouseClicked(MouseEvent e) {
    public void mouseEntered(MouseEvent e) {
    repaint();
    public void mouseExited(MouseEvent e) {
    public void mousePressed(MouseEvent e) {
    public void mouseReleased(MouseEvent e) {
    public void actionPerformed(ActionEvent e) {
    public void mouseDragged(MouseEvent e) {
    public void mouseMoved(MouseEvent e) {
    int y = e.getY();
    if(y<=(250-bar1H))
    yBar1 = y-(int)bar1H/2;
    else yBar1 = 250-bar1H;
    public void keyPressed(KeyEvent e) {
    if(e.getKeyCode()==40){
    yBar2+=10;
    if(e.getKeyCode()==38){
    yBar2-=10;
    if(yBar2>=225)
    yBar2=225;
    if(yBar2<=0)
    yBar2=0;
    repaint();
    for(int i=65;i<=90;i++){
    String s = "";
    switch(e.getKeyCode()){
    case 80: code=code+"p";
    break;
    case 69: code=code+"e";
    break;
    case 78: code=code+"n";
    break;
    case 73: code=code+"i";
    break;
    case 83: code=code+"s";
    break;
    case 82: code=code+"r";
    break;
    case 65: code=code+"a";
    break;
    case 75: code=code+"k";
    default:code="";
    break;
    public void keyReleased(KeyEvent e) {
    public void keyTyped(KeyEvent e) {

    Check out the tutorials on this site. Start here: http://java.sun.com/docs/books/tutorial/
    You can have a thread like this:public class Game extends java.awt.Component {
      public void init() {
        Thread t = new Thread(new GameLoop());
        t.start();
      public void paint(Graphics g) {
        // draw your game here.  Do nothing other than drawing.
      private class GameLoop { // inner class
        public void run() {
           while(true) {
              // update your game state here -- e.g., move the ball's position
              try {
                 Thread.sleep(200); // 200 miliseconds, 1/5 of a second
               catch(InterruptedException e) {
                  // this is the rare exception that it's generally OK to ignore
               repaint();
    }that's not perfect but it will get you started

  • After GLcanvas setVisible(false), normal JPanels sometimes not repainted.

    We are running an application which uses some jogl for animaties.
    Sometimes when the animation is finished and we set the GLCanvas to invisible, the normal
    java JPanels are not repainted.
    Also alt-tab to another windows application and back will not force a redraw.
    The application itself is still working, clicking on a (now) not visible button will trigger
    the jogl animations, and these are show correctly.
    Also from logging it appear that the AWT-thread is not blocking.
    We are looking for tips that can help find us the problem from logging
    as it only appears a few times a week and it is not reproducable.

    The problem is that most of the time it works, but after some hours/days, the java panels are not repainted.
    There is no memory leak.
    Pseudo code:
    public class AnimateTilePopup extends JPanel {
    GLCanvas glCanvas;
    public AnimateTilePopup() {
    super(null);
    setOpaque(true);
    glCanvas = new GLCanvas();
    glCanvas.setLocation(0,0);
    glCanvas.setSize(width, height);
    glCanvas.setVisible(false);
    glCanvas.setFocusable(false);
    add(glCanvas);
    flipRendererZoom = new AnimateRendererZoom();
    glCanvas.addGLEventListener(flipRendererZoom);
    public startAnimation() {
    glCanvas.setVisible(true);
    this.setVisible(true);
    public stopAnimation() {
    glCanvas.setVisible(false);
    this.setVisible(false);
    So sometimes when we call stopAnimation(), the animation is stoppped, but the underlying java JPanels are not repainted.
    When we now call startAnimation, the animation is shown and when stopped, no repaint for the normal JPanels.
    Most times it is working perfectly, only sometimes it failes.
    What can be the cause or what logging can be gather that might gives us a clue how to fix this problem.

  • I am trying to get space on an external hard drive which has some old time machine back up files that I do not need but can not eliminate, even by going into the time machine, clicking on the backup file to be eliminated and using the drop down eliminate

    I am trying to get space on an external hard drive which has some old time machine back up files that I do not need but can not eliminate, even by going into the time machine, clicking on the backup file to be eliminated and using the drop down menu with the gear box symbol to eliminate

    I cannot find this 300GB "Backup" in the Finder, only in the Storage info when I check "About This Mac".
    You are probably using Time Machine to backup your MacBook Pro, right? Then the additional 300 GB could be local Time Machine snapshots.  Time Machine will write the hourly backups to the free space on your hard disk, if the backup drive is temporarily not connected. You do not see these local backups in the Finder, and MacOS will delete them, when you make a regular backup to Time Machine, or when you need the space for other data.
    See Pondini's page for more explanation:   What are Local Snapshots?   http://pondini.org/TM/FAQ.html
    I have restarted my computer, but the information remains the same. How do I reclaim the use of the 300GB? Why is it showing up as "Backups" when it used to indicate "Photos"? Are my photos safe on the external drive?
    You have tested the library on the external drive, and so your photos are save there.  
    The local TimeMachine snapshot probably now contains a backup of the moved library.  Try, if connecting your Time Machine drive will reduce the size of your local Time Machine snapshots.

  • In Pages (5.2), in a table, superscript is not only not printing, but not printing the text around it. Bizarre

    In Pages (5.2), in a table, superscript is not only not printing, but not printing the text around it. Any answers?

    Known issue now for almost 9 months.
    Use Pages '09 if you have it.
    Peter

  • Error in pakcage table or view does not exist but on sql prompt query works

    Hi
    Can any one help me to understand the error of ORA-00942: table or view does not exist while compiling the package on Oracle 9.2.0.4.0
    The below package compiled in system user and trying to query on view v$session_wait however getting below error.
    PACKAGE BODY SYSTEM.PK_DB_ALERT
    On line: 212
    PL/SQL: ORA-00942: table or view does not exist
    I can execute same query mentioned below from sql prompt from system user and it works fine however it doesn't like from package, Please advice, thank you in advance.
    SELECT SID, seq#, event, wait_time
    FROM v$session_wait
    WHERE event NOT LIKE 'SQL*Net%' AND wait_time <> 0
    ORDER BY 2

    SDD wrote:
    Hi
    Can any one help me to understand the error of ORA-00942: table or view does not exist while compiling the package on Oracle 9.2.0.4.0Which means package owner is granted select on view not directly but via roles. However, roles are ignored by definer rights packages/stored procedures/stored functions/triggers... You need to grant package owner select on view directly.
    SY.
    Edited by: Solomon Yakobson on Jan 21, 2010 4:06 AM

  • Error: "Component is not active" but component is active

    Hi Experts,
    I transported a Web Dynpro application from source system to target system. When starting the application via browser I'm getting this error message:
    "Component XXX_BDB is not active"
    But the component is active in source and in target system. When I try to activate the component in target system there comes this error:
    Component XXX_BDB can't be generated because of errors: see long text.
    (Message no. SWDP_WB_TOOL100)
    According to the long text, there is a heavy error and I should check the component in order to eliminate the errors. Checking the syntax is OK and strangely, the component controller is also active. But when I want to start WebDynpro-Component, the error message "Component is not active" is displayed.
    I have checked this thread [Problems when activating ComponentController   |Problems when activating ComponentController; without any effect....
    Does someone have any ideas?
    Thanks in advance,
    Tan

    Hi Thomas,
    where can I check the generation load limit?
    I've done an extended syntax check (in source system, is that okay or should I do the check on both systems?), there are some errors regarding strings, field properties and structure extensions and some warnings regarding field properties.
    But this errors are on an other wd component as well, but this other component is running, so I'm not sure if these errors are crucial. However I'm going to fix the errors now.
    Baskaran, I will check the component controller and component usage as well but how can I identify the errors or should I just check it in general?
    And what about the fact that both applications are running on the source system?
    Additional info: the source system is ECC 6.0, the target system is Solution Manager 7.0
    Thanks
    Edited by: Tan Yildiz on May 4, 2011 9:26 AM

  • Why iPhone 6 Plus sound microphone not working, but in the case of sound recordings in conversation mode work?

    Why iPhone 6 Plus sound microphone not working, but in the case of sound recordings in conversation mode work?

    Sighhhh, wasted so much time yesterday and today going around Sony centre and then carphone warehouse. They told me to come back after Easter.
    I came home, banged the phone against the wall and it worked.
    Turned out that mic was working on loud speaker and when using headphones so I thought that the secondary mic is working and the main one (placed with the speakers) isn't. So I banged that part against the wall slightly (in plastic case to prevent scratches) then put the phone underwater, waited for it to dry and now it's working. Idk how well and if it's of perfect quality again but people can hear me well.

  • Why does mail app show over 2,000 messages (not new) but only 10-12 in my inbox?

    So I recenly got a new rMBP and have begun using the Mail app. I've had a bunch of problems with Gmail and the app but my my biggest gripe is that it seems to have downloaded over 2,000 messages from the server (of which I have no idea where its getting them because I've deleted pretty much everything in trying to solve this to the extent of completely wiping out my email folders). I wouldn't have such a problem with it except that it seems like its actually downloaded the 2,000+ messages onto my computer, no dobt taking up space (and its mildly annoying to me to always see 2,514 to the right of the "Gmail" folder in the left window)
    I have tried emptying my mail completely including emptying the trash+deleting mail account from all apple devices and then restarting them before adding the mail account back.
    Looking in random folders to make sure I haven't lost my marbles and theres a stash of emails lurking somewhere...
    Again, deleting the accounts, powercycling the apple devices and then setting up mail again (just incase the first three times didn't work) and I'll mention that EVERY time I set up the account it imediately starts downloading the 2,000+ emails all over again (little windw shows up in bottom left counting down how many it has and how many there are left to go).
    Any advise would be appreciated- I know its not huge but its driving me crazy!!
    Just FYI I'm using the mail app simultaneously on a rMBP, iPad 4, and iPhone 5...

    Go to Gmail (on the web) look in the folder named All Mail, they will be there, delete what you want.

  • I have like five GB's of ghost data (data the device claims is not there but it is still taking up space) on my I pad 2 from a failed movie download attempt. How do I fix that with out resting my iPad? I have only 1GB left of storage

    I have like five GB's of ghost data (data the device claims is not there but it is still taking up space) on my I pad 2 from a failed movie download attempt. How do I fix that with out resting my iPad? I have only 1GB left of storage
    The movie had gotten to the point that I could watch it all the way through but it still said processing and got stuck at that point and when I turned off the wifi to try to restart it, it deleted its-self but did not free up any of the storage It was taking up
    Restarting my iPad does not help
    Some thing similar also happened to my old laptop when trying to download the iOS 5 update to my laptop when it came out. It kept getting disconnected and every time it would try to start over and act like what had already been downloaded was not there though it was still taking up the storage space.
    And it continued repeating until it took up all of my storage space. The data file would not show up anywhere so I had to restore my laptop to the factory settings and that worked for awhile but I eventually had to get a new laptop.

    Did you try to set it up as new device, as explained in this article?
    How to erase your iOS device and then set it up as a new device or restore it from backups
    If this does not work, use iTunes to set it back to factory settings, which would also install the latest software:
    Use iTunes to restore your iOS device to factory settings - Apple Support

  • I verified my Apple Id but it wont let me download Apps as it says I'm not verified but my email tells me I am verified what do I do?

    I verified my Apple Id but it wont let me download Apps as it says I'm not verified but my email tells me I am verified what do I do?

    Why does this app NOT VERIFIED by Apple?

  • I am using chat-r wireless and facetime is not working but when i put rogers fido or any other carrier sim in it work. Anyone know what the problem?

    I am using chat-r wireless and facetime is not working but when i put rogers fido or any other carrier sim in it work. Anyone know what the problem?

    I'm guessing it's a compatibility problem similar to that that T-Mobile in the US has. I'm not familiar with chat-r wireless, but I'm guessing it's not a supported carrier for the iPhone. If they can't tell you how to fix it, I'm guessing it's just plain not going to work.

Maybe you are looking for