0

I am working on a dusk cleaner simulator on Java, in this case the shape of the cleaner is a circular ball.

The program is quite simple, the user puts in the width and length of the "room" and coordinates x and y.

What I want to do and cannot is create a series of commands, each represented by a character. There are three commands that I want to imnplement:

1. char 'A' = Move forward 1 meter

2. char 'L' = Turn left 90 degrees

3. R Turn right 90 degrees

Example of user input AALA, in this case the expected output is that the machine moves 2 meters and then turns left 90 degrees and then moves 1 meter again. Hope I am clear.

As you can see in the code, I have tried to create an array of chars but I dont know what the next step should be...

The code:

public class Cleaner extends JPanel {
    /* int lx = 1, ly = 1;
    int x = 200, y = 250;
    
    */
    int x, y;
    int width = 52, height = 50; // width and height of the "dust sucker"
    int lx , ly;


    // an array of chars
    char[] charArray ={ 'A', 'L', 'R'};     
    
    java.util.Timer move; // making the instance of Timer class from the util package
    static JFrame frame;
     
    Cleaner()
    {
        
        frame = new JFrame ("Cleaner started!"); // passing attributes to our fame
        frame.setSize (400, 400); // setting size of the starting window
        frame.setVisible (true);
        
        setForeground(Color.black); // setting color
        
        move = new java.util.Timer();
        
        move.scheduleAtFixedRate(new TimerTask()
        {
        
            
            public void run() 
            {
                if(x<0)
                    lx = 1;
                if(x>=getWidth()-45)
                    lx = -1; // -1 sets boundry for the dusk sucker
                if(y<0)
                    ly = 1;
                if(y>=getHeight()-45)
                    ly = -1; // -1 sets boundry for the dusk sucker
                
                x+=lx; // to make the machine move
                y+=ly;
                
                repaint();
            }
            
        }, 0, 5// speed of the machine
                );
        
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
    }
    
    public void paint (Graphics g) 
    {
        super.paint(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        g2d.fillOval(x, y, width, height);
    }
    
    public static void main (String[] args) 
    {
        // lx value
        
        String lxValue = 
                 JOptionPane.showInputDialog( "Enter lx" );
        // ly value
        String lyValue =
                  JOptionPane.showInputDialog( "Enter ly" );
        
        String xValue = 
                 JOptionPane.showInputDialog( "Enter x value" );
        // ly value
        String yValue =
                  JOptionPane.showInputDialog( "Enter y value" );


         // convert String inputs to int values 
          int firstInput = Integer.parseInt( lxValue ); 
          int secondInput = Integer.parseInt( lyValue );
          int thirdInput = Integer.parseInt( xValue ); 
          int forthInput = Integer.parseInt( yValue );
    
       
             Cleaner cleaner = new Cleaner();
             
                frame.add(cleaner);
          cleaner.lx = firstInput;
          cleaner.ly = secondInput;
          cleaner.x = thirdInput;
          cleaner.y = forthInput;       
        
    }
    
}

All help is appreciated!

camickr
  • 321,443
  • 19
  • 166
  • 288
Jan Adam
  • 3
  • 1

1 Answers1

0

First some basics:

  1. override paintComponent(), not paint() when doing custom painting.

  2. use a Swing Timer for animation. All updates to Swing components need to be done on the Event Dispatch Thread (EDT).

in this case the shape of the cleaner is a circular ball.

So you need to create a class to represent the ball. It will have basic properties like:

  1. size
  2. location
  3. direction of movement.
  4. speed of movement.

You will then need to create methods to change the properties. Maybe:

  1. move() - move in the current direction at the current speed
  2. turnRight() - adjust direction
  3. turnLeft() - adjust direction

Then you can create an ArrayList to store the moves and a Swing Timer to execute the moves.

Whenever the Timer fires you remove the command from the ArrayList and execute the command. By invoking one of the above 3 methods.

Check out: get width and height of JPanel outside of the class for an example that is similiar (not exact) to what you want. It demonstrates the concept if creating an object with the properties needed to control its motion.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I understand what you mean, but is it possible for you to give me code example? – Jan Adam Nov 21 '20 at 22:03
  • 1
    @JanAdam, I did. No it is not exactly what you need, but it is a start. We are not here to write the application for you, only point you in the right direction. – camickr Nov 21 '20 at 22:58