Results 1 to 2 of 2
  1. #1
    Zeptune is offline Member
    Join Date
    May 2014
    Posts
    1
    Rep Power
    0

    Default draw Geometry Shapes 2

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;

    public class Game extends JPanel implements ActionListener, KeyListener{

    Timer t = new Timer(5,this);

    double x = 0, y = 0, velx = 0, vely = 0;

    public Game(){

    t.start();
    addKeyListener(this);
    setFocusable(true);
    setFocusTraversalKeysEnabled(false);
    setBackground(Color.RED);

    }

    public void paint(Graphics g){
    super.paintComponent(g);
    Graphics2D g2 = (Graphics2D) g;
    g2.setColor(Color.BLUE);
    g2.fill(new Ellipse2D.Double(x,y,40,40));
    g2.setColor(Color.GREEN);
    g2.fill(new Ellipse2D.Double(a,b,40,40));

    }

    public void actionPerformed(ActionEvent e){
    repaint();
    x += velx;
    y += vely;
    a += vela;
    b += velb;

    }



    public void up(){

    vely = -5;
    velx = 0;


    }

    public void down(){

    vely = 5;
    velx = 0;
    }
    public void right(){

    vely = 0;
    velx = 5;


    }

    public void left(){

    vely = 0;
    velx = -5;


    }
    public void keyPressed(KeyEvent e){

    int code = e.getKeyCode();
    if(code == KeyEvent.VK_UP){

    up();
    }

    if(code == KeyEvent.VK_DOWN){

    down();

    }
    if(code == KeyEvent.VK_RIGHT){

    right();

    }
    if(code == KeyEvent.VK_LEFT){

    left();




    }
    }



    public void keyTyped(KeyEvent e) {}
    public void keyReleased(KeyEvent e) {}




    /************************************************** ************************************************** ************************************************** **********************/


    double a = 0,b = 0,vela = 0,velb = 0;


    public void w(){

    vela = -5;
    velb = 0;


    }

    public void s(){

    vela = 5;
    velb = 0;
    }
    public void d(){

    vela = 0;
    velb = 5;


    }

    public void a(){

    vela = 0;
    velb = -5;


    }




    public void Calc(KeyEvent e){
    int code = e.getKeyCode();
    if(code == KeyEvent.VK_W){

    w();
    }

    if(code == KeyEvent.VK_S){

    s();

    }
    if(code == KeyEvent.VK_D){

    d();

    }
    if(code == KeyEvent.VK_A){

    a();



    }


    }
    }








    So why doesn't it work and what about the second dot.... ?
    Last edited by Zeptune; 05-09-2014 at 10:36 PM. Reason: I made another mistake!

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,305
    Rep Power
    25

    Default Re: draw Geometry Shapes 2

    Please edit your post and wrap your code with code tags:
    [code]
    YOUR CODE HERE
    [/code]
    to get highlighting and preserve formatting.

    why doesn't it work
    Please explain what "doesn't work" means.
    If you don't understand my response, don't ignore it, ask a question.

Similar Threads

  1. draw Geometry Shapes
    By Madhan in forum New To Java
    Replies: 5
    Last Post: 05-10-2014, 08:22 AM
  2. please help me draw these shapes
    By sozoki in forum New To Java
    Replies: 0
    Last Post: 12-08-2013, 05:00 PM
  3. Button click for drawing geometry shape
    By nnur in forum AWT / Swing
    Replies: 5
    Last Post: 05-15-2010, 07:48 PM
  4. Draw shapes by pressing a button
    By SWEngineer in forum AWT / Swing
    Replies: 16
    Last Post: 05-14-2010, 05:26 AM
  5. Efficient Looping to Draw Shapes
    By clam in forum New To Java
    Replies: 18
    Last Post: 10-31-2009, 04:05 AM

Posting Permissions

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