0

Do you know what it means to override an extended class's method?
You define your own version of the method with the same signature as the one you want to override.
Use the @Override annotation just before the definition to have the compiler tell you if you have correctly overridden the method.

Your draw method needs the Graphics object passed to the paintComponent method. That is what provides the GUI context where the drawing will be done.

0

Okay, see i never knew that. So i would put that in the "ObjectA" code in order to skip the "move" step?
I really don't know

0

Earlier I said:
write a simple testing program that extends JFrame and adds a class of yours that extends JPanel. Override its paintComponent method and call the draw method from inside of the paintComponent method to see if it draws as you expect.

The paintComponent gets the Graphics object that you will pass to the draw method in the ObjectA class.

Leave the move method alone for now. You will be calling it later when you add the Timer to make the shape move.

0

I'm still not getting the whole @"Whatever" does the program then jump to whatever is after the @ symbol?

0

does the program then jump to whatever is after the @ symbol?

Sorry, I don't understand what you are asking.

0

I'm basically asking how you use the Override function, or action, or whatever it is.
I've never seen nor used it before.

0

Ask google how to use it: java @override.

Earlier I said:
Use the @Override annotation just before the definition to have the compiler tell you if you have correctly overridden the method.

0

It is not necessary. If you don't use it the compiler will not give you an error message if you coded the override incorrectly.
You can add a paintCompoennt method but it will never be called because it does not override the paintComponent method of the base class.

0

So let's say i didn't, cause well, i didn't. I don't really know where i need to take my program next. I feel like i have ObjectA completed, i just need to make my Master Class to test that it works, which is where i start stumbling again

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.Color;
import javax.swing.JPanel;


public class ObjectA extends JPanel{
    private int x = 0;
    private int Width = 50;
    private int Height = 50;
    private int xSpeed = 5;
  public void move() {
    if (x + Width >= 425){
	xSpeed = -xSpeed;
    }
    else if (x <= 50){
	xSpeed = xSpeed;
    }
    x = x + xSpeed;
  }
  public void draw(Graphics2D g2d) {
    Rectangle2D.Double BOX1 = new Rectangle2D.Double(x + 75, 75, Width, Height);
    g2d.setPaint(Color.red);
    g2d.fill( BOX1 );
  }
}

Object A above, and now the master below... (thus far)

import javax.swing.JFrame;

public class ThreeObjectAnimation {
    public static void main(String[] args){
	JFrame frame = new JFrame();
	frame.setSize(500, 500);
	frame.setTitle( "Three_Object_Animation.jpg" );
	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	ObjectA TEST = new ObjectA();
	frame.add(TEST);
	frame.setVisible( true );
    }
}
0

Where is the override of the paintComponent method?
That method is the one that is called with the Graphics object that you will pass to the draw method.

For a quick test, redefine the draw class by replacing its definition line with these two lines:
public void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D)g;

0

Okay, so i changed them, and i do get what i wanted to appear, to appear. Where do i go from this..

That's alright. Thanks everyone for the help! I'm just going to see my prof tomorrow about accessing the Objects(A,B,C) from the Master Class.

Thanks again. Big help!

Edited by Hypnos_16: n/a

0

Seeing your prof sounds like a good idea.

The code you created here was for testing to see how some methods worked. Next you'll need to design how you want your classes and their methods to work.

0

I thought what i had created was an object with animation built in, and that all i need to do was sync it up with a timer class to make the animation...
How much work i do still have left to do?

0

You need to
1)rewrite the AnimatedObject class back to having the two methods.
2)create a new class that extends JPanel, overrides the paintComponent class and calls all the AnimatedObject classes' draw() methods. The AnimatedObject objects should be in a list like an ArrayList.
3)create a method that is called by a timer that calls the move() method for each of the objects in the above mentioned list.

0

That doesn't sound too bad, i've already rearranged the classes back to the two methods. I checked in my notes and found the @Override thing you were talking about, we aren't supposed to cover it for a few weeks... So I guess there's an alternative method??? as for the timer i'm hoping i can get help on that tomorrow. This is due Thursday (Today's Tuesday) Next one i get i'm finishing the night i get it. This is getting ridiculous...

0

To Overriding Something (@Override) We haven't learned it yet, i can't imagine that the would make us use it...

0

Its use is to save your code having a bug.

Google it for lots of good descriptions.

Edited by NormR1: n/a

0

I'll just wait until we go over it in class, if i have issues then, i'll ask them.

0

In the code for your JPanel class you have your own paintComponent method, just kike in some of your previous code:

public void paintComponent( Graphics g ){
   Graphics2D g2 = (Graphics2D) g;
   // now call each object's [I]draw [/I]method
}

ps: You stupidly-named "ObjectA" class doesn't need to extend anything - see the example code I posted earlier.

0

Hey. Back again, got my problem straightened away. Not as complicated as i thought

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.geom.Rectangle2D;
import java.awt.Color;
import javax.swing.JPanel;

public class ObjectA implements Animate, Drawable {
    public int x = 0;
    private int Width = 50;
    private int Height = 50;
    private int xSpeed = 5;
    private Color CIRCLE_COLOR = Color.red;
  public void move(double a, double b) {
    if (x + Width > a){
	xSpeed = (xSpeed * (-1) - 1);
    }
    else if (x + Width < b){
	xSpeed = (xSpeed * (1) + 1);
    }
    x += xSpeed;
  }
  public void changeConfiguration() {
    if ( CIRCLE_COLOR == Color.red){
	CIRCLE_COLOR = Color.blue;
    }
    else {
	CIRCLE_COLOR = Color.red;
    }
  }
  public void draw(Graphics2D g2d) {
    Rectangle2D.Double BOX1 = new Rectangle2D.Double(x + 75, 75, Width, Height);
    Rectangle2D.Double BOX2 = new Rectangle2D.Double(x + 75, 175, Width, Height + 100);
    Rectangle2D.Double BOX3 = new Rectangle2D.Double(x + 75, 375, Width, Height);
    g2d.setPaint(CIRCLE_COLOR);
    g2d.fill( BOX1 );
    g2d.fill( BOX2 );
    g2d.fill( BOX3 );
  }
}
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Dimension;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.EventQueue;
import javax.swing.JPanel;
import javax.swing.JFrame;

public class ThreeObjectAnimation extends JPanel implements ActionListener {

    private static final int INTERVAL = 250;
    private static int COUNTER = 0;
    private static final int WINDOW_WIDTH = 600;
    private static final int WINDOW_HEIGHT = 600;
    private static final Color BG_COLOR = Color.WHITE;

    private ObjectA A = new ObjectA();
    private ObjectB B = new ObjectB();
    private ObjectC C = new ObjectC();

    private javax.swing.Timer timer;

    private int width = -1;
    private int height = -1;

    public ThreeObjectAnimation() {
        setPreferredSize( new Dimension( WINDOW_WIDTH, WINDOW_HEIGHT ) );
        setBackground( BG_COLOR );

        timer = new javax.swing.Timer(INTERVAL, this);
        timer.start();
    }
    public void start(){
	timer.start();
    }
    public void stop(){
	timer.stop();
    }
    protected void paintComponent( Graphics g ) {
        super.paintComponent( g );
        Graphics2D g2d = (Graphics2D)g;
        int w = getWidth();
        int h = getHeight();

        if ( w != width || h != height ) {
            width = w;
            height = h;
        }
	C.draw(g2d);
	A.draw(g2d);
	B.draw(g2d);
    }
    public void actionPerformed( ActionEvent evt ) {
	if (COUNTER % 2 == 0){
	     A.move(450,50);
	    // Give the Borders that the box bounces back and forth between 
	    // First value is the Right Most Border
	    // Second value is the Left Most Border
	    B.move(450,50);
	    // Give the Borders that the box bounces back and forth between 
	    // First value is the Bottom Most Border
	    // Second value is the Top Most Border
        }
	A.changeConfiguration();
	B.changeConfiguration();
	C.changeConfiguration();
	COUNTER++;
        repaint();
    }

    public static void runApplication( final JPanel app ) {
        EventQueue.invokeLater(
            new Runnable() {
                public void run() {
                    JFrame frame = new JFrame();
                    frame.setSize( app.getPreferredSize() );
                    frame.setTitle( app.getClass().getName() );
                    frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
                    frame.add( app );
                    frame.setVisible( true );
                }
            } );
    }
    public static void main(String[] args ) {
        ThreeObjectAnimation application = new ThreeObjectAnimation();
        runApplication( application );
    }
}

Thanks to everyone that helped! I now have to try and use user input now. That should be fun to do.... Wish me luck

0

Glad you got it working.
Good luck on the part of the project.

A problem I see is the three separate classes A, B and C.
And the three boxes all in one class.
Its not clear why you have three classes that are exactly the same.

If I comment out all statements using the varibles: B and C,
the output looks the same as when there are the 3 classes being executed instead of just A.

Edited by NormR1: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.