0

I have a problem with canvas i wanted to show some moving balls on canvas (animation) but there is nothing I can see except black background.

can somebody tell me the mistake in this code and how it will work please.

public CopyOfCleanBallPanel2() throws IOException, InterruptedException {

        frame = new JFrame("simple gaming loop in java");
        frame.setSize(BOX_WIDTH, BOX_WIDTH);
        frame.setResizable(false);

        displayCanvas = new CustomCanvas();
        displayCanvas.setLocation(0, 0);
        displayCanvas.setSize(CANVAS_WIDTH, CANVAS_HEIGHT);
        displayCanvas.setBackground(Color.BLACK);
        displayCanvas.setFont(new Font("Arial", Font.BOLD, 14));
        displayCanvas.setPreferredSize(new Dimension(CANVAS_WIDTH,CANVAS_HEIGHT));

        frame.add(displayCanvas);
        displayCanvas.requestFocus();

        frame.setLocationRelativeTo(null);

        try {
            this.aBall = (BallServer) Naming
                    .lookup("rmi://localhost/BouncingBalls");

        } catch (Exception e) {
            System.out.println("Exception: " + e);
        }


        frame.pack();
        frame.setVisible(true);

        aBall.start();
        startFrameTimer();  
    }

    /*
     * Initializes the frame (also game update) timer.
     */
    private void startFrameTimer() {
        frameTimer.schedule(new FrameTimerTask(), 1, GAME_TIMER_COOLDOWN);
    }

    public void updateSimulation() throws RemoteException {
        repaintCanvas();
    }
    /*
     * This method gets called by the timer. It updates the game simulation and
     * redraws it.
     */
    private void onFrameTimer() throws RemoteException {
        updateSimulation();
    }

    /*
     * Causes the whole canvas to get repainted.
     */
    private final void repaintCanvas() throws RemoteException  {
        Graphics g =  displayCanvas.getGraphics();
        drawworld(g);
    }

    private class FrameTimerTask extends TimerTask {
        public void run() {
            try {
                onFrameTimer();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }
    /*
     * This custom canvas overrides the paint method thus allowing for a custom
     * painting on the component.
     */
    private class CustomCanvas extends Canvas {
        @Override
        public void paint(Graphics g) {
            // Currently the game message gets drawn over the inner border
            // of the canvas so we have to repaint the whole thing.
            try {
                repaintCanvas();
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }   
    public void drawworld(Graphics g) throws RemoteException {
            g.setColor(Color.BLACK);
             g.fillRect(0, 0, CANVAS_WIDTH, CANVAS_HEIGHT);

            System.out.println("i m in drawworld ");

            serBall = aBall.getState1(); ***// here it is remote call and there is thread going on suspension*** 

            for (int i = 0; i < currentNumBalls; i++) {
                g.setColor(serBall[i].getBallColor(velocity.getLength()));
                g
                        .fillOval((int) (serBall[i].position.getX() - serBall[i]
                                .getRadius()),
                                (int) (serBall[i].position.getY() - serBall[i]
                                        .getRadius()), (int) (2 * serBall[i]
                                        .getRadius()), (int) (2 * serBall[i]
                                        .getRadius()));

                // Draw our framerate and ball count
                g.setColor(Color.WHITE);
                g.drawString("FPS: " + currentFrameRate + " Balls: "
                        + currentNumBalls, 15, 15);
            }   
        }

P.S: I thought there is some thread problem while I m calling the remote method and rendering the drawworld, either of thread is going on suspension or blocked

Please Help.

jibby lala

7
  • i tried everything what i could do. Apr 2, 2011 at 18:20
  • The code does two things: get data through RMI and draw balls in the canvas. Check what is going wrong; modify it to display a "static" ball in a fixed location to see if it appears. If it does it is the remote call that fails, if it does not then it is the drawing part. It can be both that fail, too.
    – SJuan76
    Apr 2, 2011 at 18:29
  • is it not some typical obvious swing mistake u can see? Apr 2, 2011 at 18:38
  • Is the assignment to use the canvas element? Then why are you using Swing? It may help if you could explain what you need to use and why you chose the approach you did. Apr 2, 2011 at 19:06
  • 3
    Folks, please see related thread of the OP's before rehashing that which has already been hashed: stackoverflow.com/questions/5301069/… Apr 2, 2011 at 19:35

2 Answers 2

3

When using Swing custom painting is done by overriding the paintComponent() method of a JPanel (or JComponent), not a Canvas. Canvas is an AWT component and should not be used with Swing. See the Swing tutorial on Custom Painting for more information and examples.

Animation should be done by using a Swing Timer so that code is executed on the EDT. The Swing tutorial also has section on "How to Use Swing Timers" and "Concurrency" which helps explain these concepts.

The repaintCanvas() method is unnecessary. To repaint a component you simply invoke repaint() on the component. You should never use the getGraphics() method. All painting methods already receive the Graphics class as a parameter. That is the Grapphics object you should use for painting.

0
2

It looks as if you are mixing heavy and light components, which requires some care. Alternatively, you might compare your code to this example.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.