Results 1 to 6 of 6
  1. #1
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Lines: How to Make Them Bend?

    Greetings, everyone. Just as the title says, I'm trying to recreate a game called Spirit for iPhone (if any of you heard of it, it'll will make this problem a lot easier). But to keep things simple, I'll be general. The game has a grid, and a player (the player looks like a ghost). The ghost is moved by the mouse. But every time it moves over the grid (which is literally every time) the grid is supposed to bend in the direction the player was going, and when the player leaves the grid, it sways back and forth, less each time, back into place. This gives the grid a sort of flowing-on-water effect.

    Lines: How to Make Them Bend?-998923_2.jpg

    As I said, to keep things simple, let's say I had just one line in the middle of the screen. I also have a circle which the mouse will control. When the circle passes the line, I want the line to bend in the direction of the circle (like if two people were pulling a rope and a third person would run into the middle of it) and once the circle passes the line, I want the line to sway back and forth a few times, with less intensity each sway, until it eventually goes back in place.

    Here's some code that makes a black window with a vertical line in the center and a red circle you can move with the mouse (I make the cursor invisible so it won't interfere with the circle). You can pass the red circle through the line, but obviously nothing will happen.. take a look;

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    import java.awt.geom.*;
    import javax.swing.*;
    
    public class LineSway extends JPanel implements MouseMotionListener
    {
      
      private int x = 150, y = 280;
      private ImageIcon cursor = new ImageIcon (""); // Makes an image for the mouse (nothing)
      private Cursor c = Toolkit.getDefaultToolkit().createCustomCursor (cursor.getImage(), new Point (16, 16), ""); // Makes the mouse invisible
      
      public static void main (String[]args)
      {
        new LineSway();
      }
      
      public LineSway()
      {
        setCursor(c);
        setLayout(null);
        setBackground(Color.BLACK);
        addMouseMotionListener(this);
        
        
        JFrame frame = new JFrame();
        frame.setContentPane(this);
        frame.setTitle ("Line Sway");
        frame.setSize(600, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setLocationRelativeTo(null);
        frame.setResizable(false);
        frame.setVisible(true);
      }
      
      public void mouseDragged (MouseEvent e)
      {
       
      }
      
      public void mouseMoved (MouseEvent e) 
      {
         x = e.getX();
        y = e.getY();
        
        if (x <= 0)
        {
          x = 0;
        }
        else if (x + 40 >= getWidth())
        {
          x = getWidth() - 40;
        }
        
        if (y <= 0)
        {
          y = 0;
        }
        else if (y + 40 >= getHeight())
        {
          y = getHeight() - 40;
        }
        
        repaint();
      }
      
      public void paint (Graphics g)
      {
        super.paint(g);
        Graphics2D g2 = (Graphics2D) g;
        
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,        // Smoothens the graphics
                            RenderingHints.VALUE_ANTIALIAS_ON);
        
        g2.setColor(Color.WHITE);
        g2.setStroke(new BasicStroke(2));
        g2.draw(new Line2D.Double(300, 0, 300, 600));
        g2.setColor(Color.RED);
        g2.fill(new Ellipse2D.Double(x, y, 40, 40));
      }
    }
    It's nothing that complex, really. As I said, what I want to know is how to make the line bend when the circle comes near it. Is there a method? Or does someone have a clever strategy or trick?
    Last edited by CuppaCoffee; 01-22-2012 at 09:34 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Lines: How to Make Them Bend?

    You would first of all draw in the JPanel's paintComponent method, not its paint method . Next you should create a data structure (2 D array of Point2D perhaps?) to represent the grid points of your universe, the ones representing the vertices of your lines, since I'm assuming that these vertices will change as the program runs. You will then use this data structure to draw the lines (again in paintComponent).

  3. #3
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Lines: How to Make Them Bend?

    My apologies, but I still consider myself rather new to Java. Could you give me a brief example of code to better help me understand your explanation?

    I've also fixed my code referring the paintComponent thing you were talking about. Before I made this thread and during the coding, I had switched it from a simple JFrame to a JPanel, so I forgot to edit the final method at the bottom.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Lines: How to Make Them Bend?

    Quote Originally Posted by CuppaCoffee View Post
    My apologies, but I still consider myself rather new to Java. Could you give me a brief example of code to better help me understand your explanation?
    I wish I have the time, but it's Sunday and I'm currently catching up on office work.

  5. #5
    CuppaCoffee's Avatar
    CuppaCoffee is offline Member
    Join Date
    Jan 2012
    Location
    Canada
    Posts
    80
    Rep Power
    0

    Default Re: Lines: How to Make Them Bend?

    No problem. I'm rather busy myself. I just prefer to make these threads ahead of time so I can give you guys more time to answer rather then on demand.

    By all means, you can come back whenever you're free. I'm just glad I have the opportunity to get some help.

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Lines: How to Make Them Bend?

    Quote Originally Posted by CuppaCoffee View Post
    No problem. I'm rather busy myself. I just prefer to make these threads ahead of time so I can give you guys more time to answer rather then on demand.

    By all means, you can come back whenever you're free. I'm just glad I have the opportunity to get some help.
    You may be waiting a few weeks... Better for you to try to implement the suggestion first, and then show us your code if you get stuck. Again, create a 2D array of Points2D to use as the vertices of your grid and iterate through this array to draw your lines. When the lines need to be bent, you change the position of one of the vertices and call repaint.

Similar Threads

  1. How do I make lines and graphs in java?
    By TallOne in forum New To Java
    Replies: 5
    Last Post: 01-17-2012, 02:36 AM
  2. Replies: 3
    Last Post: 10-25-2011, 06:29 PM
  3. Replies: 4
    Last Post: 07-23-2011, 08:08 PM
  4. Cannot Add new Lines
    By capjoel in forum New To Java
    Replies: 3
    Last Post: 04-01-2011, 06:33 PM
  5. What do these lines mean?
    By coding in forum New To Java
    Replies: 5
    Last Post: 02-02-2011, 03:01 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
  •