Question

 class ballbouncepanel extends JPanel
{
        public void start()
            {

                Timer timer;
                final int FREQ = 45;
                timer = new Timer(FREQ, new ActionListener()
                {
                    public void actionPerformed(ActionEvent evt)
                    {
                        repaint();
                    }
                });
                timer.start();
    }

    Rect rect = new Rect();
        public Dimension getPreferredSize()
        {
            return new Dimension(250,200);
        }
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            rect.draw(g);
            rect.move(g);
            rect.erase(g);
        }
}

class Rect
{
    public int xLocation = 0;
    public int yLocation = 0;
    public int xVelocity = 10;
    public int yVelocity = 10;

    public void draw(Graphics g)
    {
        g.setColor(Color.cyan);
        g.fillRect(xLocation, yLocation, 20, 20);
    }
    public void move(Graphics g)
    {
        xLocation += xVelocity;
        yLocation += yVelocity;
    }
    public void erase(Graphics g)
    {
        g.setColor(Color.white);
        g.fillRect(xLocation, yLocation, 20, 20);
    }
}

The new error is that now my repaint method isn't working.

Above is my code for the frame that I got that I want to paint on, I understand paint using a applet or JApplet, but I am trying to do what I did in a applet on Swing, and now im running into problems, I have looked up many tutorials on how to implement graphics in, but most of them is just running a main graphics, I need mine to be in this specific frame(BB). If anyone could help me understand or point me to a beginners tutorial for it, it would be appreciated.

Was it helpful?

Solution

I think you are just forgetting to call the start() method of your ballbouncepanel. Also a note: you're move() method does no painting, so take out the Graphics argument and just call it in the timer

Also not sure what the erase method is supposed to do, but I'm thinking you want to change color every tick of the timer. In that case, just keep a color variable, and just change that variable. You can see the example below

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

public class bounceballpanel extends JPanel {
    public void start() {

        Timer timer;
        final int FREQ = 45;
        timer = new Timer(FREQ, new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                rect.move();
                rect.changeColor();
                repaint();
            }
        });
        timer.start();
    }

    Rect rect = new Rect();

    public Dimension getPreferredSize() {
        return new Dimension(250, 200);
    }

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        rect.draw(g);
        //rect.erase(g);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                JFrame frame = new JFrame();
                bounceballpanel panel = new bounceballpanel();
                panel.start();
                frame.add(panel);
                frame.pack();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

class Rect {
    public int xLocation = 0;
    public int yLocation = 0;
    public int xVelocity = 10;
    public int yVelocity = 10;

    Color color = Color.cyan;

    public void draw(Graphics g) {
        g.setColor(color);
        g.fillRect(xLocation, yLocation, 20, 20);
    }

    public void move() {
        xLocation += xVelocity;
        yLocation += yVelocity;
    }

    public void changeColor() {
        if (color == Color.cyan) {
            color = Color.white;
        } else {
            color = Color.cyan;
        }
    }

    /*
    public void erase(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(xLocation, yLocation, 20, 20);
    }*/
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top