0

I'd like to set the velocity of the player object to augment its x position by two, and I'l also like to set the velocity of the entity object to augment its x position by three.

But, at the end of every tick both x positions for both objects player and entity are augmented by five and both move at the same time.

I would really appriciate any help that you would be able to share.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

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

public class Frame extends JPanel implements ActionListener {

    // Initialize ObjectOne objects
    ObjectOne obj = new ObjectOne();
    ObjectOne objRM = new ObjectOne();
    ObjectOne entity = new ObjectOne();
    ObjectOne player = new ObjectOne();

    // Create timer object
    Timer tm = new Timer(10, this);

    public void paintComponent(Graphics g) {
        super.paintComponent(g);

        // Create menus
        obj.drawGRect("Your current score: ", 0, 0, 25, g);
        objRM.drawGRect("N/A.", 0, 23, 25, g);

        // Create players
        player.setY(2);
        player.drawGRect(20, 20, g);
        repaint();

        entity.setY(40);
        entity.drawGRect(20, 20, g);
        repaint();
    }

    public Frame() {
        setBackground(Color.BLACK);
        tm.start();
    }

    public static void main(String[] args) {
        Frame frClass = new Frame();
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
        frame.setSize(800, 100);
        frame.add(frClass);
    }

    public void actionPerformed(ActionEvent arg0) {
        // TODO Auto-generated method stub

        player.setPosition(2);
        repaint();

        entity.setPosition(3);
        repaint();

        //The positions for both player and entity will be incremented by 5 every tick.

        System.out.println(player.getX());
    }
}

ObjectOne class:

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;

import javax.swing.JComponent;

public class ObjectOne {

    private static int x;
    private static int y;

    public void setY(int y) {
        this.y = y;
    }

    public int getY() {
        return y;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getX() {
        return x;
    }

    // Creates rectangle without text
    public void drawGRect(int w, int h, Graphics g) {

        Graphics2D g2d = (Graphics2D) g;

        //Creates white rectangle
        Rectangle rect = new Rectangle(x, y, w, h);
        g.setColor(Color.WHITE);
        g2d.fill(rect);

        //creates black rectangle
        Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
        g2d.setColor(Color.BLACK);
        g2d.fill(rectG);

    }

    //Creates rectangle with text
    public void drawGRect(String s, int x, int y, int h, Graphics g) {
        int w = s.length() * 6 + 15;

        Graphics2D g2d = (Graphics2D) g;

        //Creates white rectangle
        Rectangle rect = new Rectangle(x, y, w, h);
        g2d.setColor(Color.WHITE);
        g2d.fill(rect);

        //Creates black rectangle
        Rectangle rectG = new Rectangle(x + 2, y + 2, w - 4, h - 4);
        g2d.setColor(Color.BLACK);
        g2d.fill(rectG);

        //Creates text
        g2d.setColor(Color.WHITE);
        g2d.drawString(s, x + 14, y + (h / 2) + 5);

    }

    //Redefines x integer
    public void setPosition(int speed) {
        x += speed;

    }

}
John Kugelman
  • 349,597
  • 67
  • 533
  • 578

1 Answers1

1
private static int x;
private static int y;

The static modifier makes it so there's only a single copy of x and y throughout your entire program, rather than a copy for each instance of the class. Remove static.

private int x;
private int y;
Community
  • 1
  • 1
John Kugelman
  • 349,597
  • 67
  • 533
  • 578