Results 1 to 7 of 7

Thread: Basic Graphic

  1. #1
    jkswebsite is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default Basic Graphic

    Does anyone know where I could learn how to make a basic graphic with animations? I don't really understand the coordinates of the animations.
    Last edited by jkswebsite; 11-24-2007 at 10:18 PM.

  2. #2
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    
    public class AnimationVariables extends JPanel implements Runnable {
        int x = 100;
        int y = 100;
        int s = 100;
        int dx = 3;
        int dy = 2;
    
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D)g;
            g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                                RenderingHints.VALUE_ANTIALIAS_ON);
            g2.setPaint(Color.red);
            g2.fillRect(x, y, s, s);
        }
    
        public void run() {
            boolean runMore = true;
            while(runMore) {
                try {
                    Thread.sleep(50);
                } catch(InterruptedException e) {
                    runMore = false;
                }
                checkBoundries();
                x += dx;
                y += dy;
                repaint();
            }
        }
    
        private void checkBoundries() {
            if(x + dx < 0 || x + s + dx > getWidth())
                dx *= -1;
            if(y + dy < 0 || y + s + dy > getHeight())
                dy *= -1;
        }
    
        private void start() {
            while(!isVisible()) {
                try {
                    Thread.sleep(25);
                } catch(InterruptedException e) {
                    break;
                }
            }
            Thread thread = new Thread(this);
            thread.setPriority(Thread.NORM_PRIORITY);
            thread.start();
        }
    
        public static void main(String[] args) {
            AnimationVariables test = new AnimationVariables();
            JFrame f = new JFrame();
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.getContentPane().add(test);
            f.setSize(400,400);
            f.setLocation(200,200);
            f.setVisible(true);
            test.start();
        }
    }

  3. #3
    jkswebsite is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    awesome man thanks! okay now I want to make it a little more complicated with some user interaction. I know the basic code for the user moving the arrow keys left to right. So how can I make that template more "fun," or into a "game?"

    thank you SO much for all your help, you are a life saver!

    this is what I have in another java file for moving text from left to right with the arrow keys:

    // handles KeyEvents
    public void keyPressed(KeyEvent e) {
    if (e.getKeyCode() == KeyEvent.VK_LEFT)// left arrow
    xdir = -5;// move left
    if (e.getKeyCode() == KeyEvent.VK_RIGHT)// right arrow
    xdir = 1;// move right

    if (e.getKeyChar() == ' ') {// space bar
    beep.play();
    xdir = 0;// stop moving
    }
    }

    public void keyReleased(KeyEvent e) {
    }

    public void keyTyped(KeyEvent e) {
    }

    }
    how would I import that to the red animation?
    Last edited by jkswebsite; 11-25-2007 at 11:42 PM.

  4. #4
    hardwired's Avatar
    hardwired is offline Senior Member
    Join Date
    Jul 2007
    Posts
    1,576
    Rep Power
    9

    Default

    If you want to use keyboard input I would recommend keybinding which you can find out about here How to Use Key Bindings. You bind them to the JFrames JRootPane and (you may need to) add a call to setFocusable on your JPanel/graphic component.

  5. #5
    jkswebsite is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    hmmm okay, I don't really understand the site or what you're saying

    so I could just move the key events code into the red one?

    Or I have a better idea.

    On another file, I have a yellow oval, and I want to have text moving (left to right) saying move the eyes (attached under the text) to the center,

    and while the user moves the eyes left or right, eventually to the center,

    the eyes will change color and maybe a "YOU WIN!" could come up, if possible. I just want a simple "game" with some user interaction

  6. #6
    jkswebsite is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    or to keep the red box from hitting the walls, and if it does, maybe it turns black

    is that possible?

    with user interaction though

  7. #7
    jkswebsite is offline Member
    Join Date
    Nov 2007
    Posts
    12
    Rep Power
    0

    Default

    I GOT IT! I made an overlap game, I am going to go in early so I can make them turn white when they overlap, thanks for all your help!

Similar Threads

  1. Replies: 5
    Last Post: 04-25-2009, 08:00 PM
  2. Graphic settings being overridden?
    By sjchase in forum New To Java
    Replies: 0
    Last Post: 01-16-2008, 11:27 PM
  3. How do insert a Graphic
    By carl in forum New To Java
    Replies: 1
    Last Post: 08-01-2007, 05:30 AM
  4. help with basic example
    By fred in forum New To Java
    Replies: 1
    Last Post: 07-20-2007, 05:45 PM
  5. Swing Graphic interface
    By Daniel in forum AWT / Swing
    Replies: 2
    Last Post: 06-28-2007, 04:39 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •