0

I'm a real beginner here, so forgive my stupidity on this. I am trying to create a program that will move a graphic in a continuous direction until it is out of frame based off a single key press from an input validator. This is what I have so far:

Frame Building Class with main method:

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

public class EmptyFrameViewer 
{
public static void main(String[] args)
{
    JFrame frame = new JFrame();
    frame.setSize(300,  400);
    frame.setTitle("Alien Faces");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

    FaceComponent face1 = new FaceComponent();
    frame.add(face1);
    frame.setVisible(true);

    FaceComponentTwo face2 = new FaceComponentTwo();
    frame.add(face2);
    frame.setVisible(true);

}

This is the painting class:

}

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JComponent;

/*
 * This component will draw an "Alien" face
 */ 

public class FaceComponent extends JComponent 
{

public void paintComponent(Graphics g)
{
    //Recover Graphics2D
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;

    //Construct the alien face

    //Draw the  head
    Ellipse2D.Double head = new Ellipse2D.Double (5, 80, 100, 150);
    g2.draw(head);

    //Draw the set of eyes
    g2.setColor(Color.GREEN);
    Rectangle eye = new Rectangle(25, 140, 15, 15);
    g2.fill(eye);
    eye.translate(50, 0);
    g2.fill(eye);

    //Draw the mouth
    Line2D.Double mouth = new Line2D.Double(30, 180, 80, 180);
    g2.setColor(Color.RED);
    g2.draw(mouth);

    //Draw the greeting
    g2.setColor(Color.BLUE);
    g2.drawString("Hello, World!", 20, 245);
}


}

This is the framework for the Movement class but I feel this needs to be heavily modified:

import javax.swing.*;


/*
 * This class will invoke the methods that move the 
 * objects in different directions.
 */

public class Movement extends FaceComponent
{
//List of fields that are used within this class.
int x = 0, y = 0, velX = 7, velY = 7;


//Constructor, that creates two brand new instances of the alien faces.
public Movement()
{
    FaceComponent face1 = new FaceComponent();
    FaceComponentTwo face2 = new FaceComponentTwo();
    repaint();
}

//List of methods that will be invoking different movements which are based on the user's inputs.


//Method that will move the first face(left) to the right until it is out of frame.
public void moveRight(int x, int y)
{

}


//Method that will move the second face(right) to the left until it is out of frame.
public void moveLeft(int x, int y)
{
    while(x > 0 || x <= 550)
    {
        x += velX;
        repaint();
    }
}


//Method that will move the first face(left) up until it is out of frame.
public void moveUp(int x, int y)
{

}


//Method that will move the second face(right) until it is out of frame.
public void moveDown(int x, int y)
{

}


//Method that will move the first face(left) in a downwards-right angle until it is out of frame.
public void moveDownRight(int x, int y)
{

}


//Method that will move the second face(right) in a downwards-left angle until it is out of frame.
public void moveDownLeft(int x, int y)
{

}


}

Probably going about this in the wrong way, and I've asked this question different times before but I haven't been asking the questions the right way. Forgive my ignorance, any help is appreciated.

kpinz
  • 3
  • 3

1 Answers1

1

First you might want to start by learning how to move a component when a key is pressed. Check out the MotionWithKeyBindings example code found in Motion Using the Keyboard for an example of how to do this with key Bindings.

Once you understand this you will need to modify the Action since you will now need to use a Swing Timer to schedule continuous events. So instead of having the Action directly change the location of the component you would start the Timer. Then when the Timer fires you would change the location of the component.

Check our the section from the Swing tutorial on How to Use Swing Timers for more information and examples.

You can also check out: Update a Label with a Swing Timer for a simple example of a Swing Timer.

Community
  • 1
  • 1
camickr
  • 321,443
  • 19
  • 166
  • 288