Results 1 to 10 of 10
  1. #1
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Exclamation Complicated Draw

    Hello everyone, I need your help with a small problem that occured to me.

    I have some stuff in my disposal that i have to use and i dont know how to manipulate them to obtain the wanted result:

    Java Code:
    public Polygon(Point[] inShape, Point inPosition, double inRotation)
    The Polygon class gets a series of point coordinates (inShape) that form a shape, the coordinates of this shape's current position (inPosition) relevant to the center of the screen, and inRotation which is the "where the object is looking at" measured in degrees, 0-360.

    I use the class Point as a means of storing coordinates. Here is the code:
    Java Code:
    class Point {
    	double x;
    	double y;
    
    	public Point(double inX, double inY) { 
    		x = inX; 
    		y = inY; 
    	}
    
    	public Point clone() {
    		return new Point(x, y);
    	}
    }
    I also have :
    Java Code:
    public class Ship {
    
    	Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
    	
    }
    which i have to use to store the coordinates that define the shape of a spaceship.

    So what i have to do is that i have to draw the spaceship in the middle of the screen by calling a draw polygon function, in the class shown below, where i have to take the ship's original shape directly from the ship class.

    Java Code:
    import java.awt.*;
    import java.awt.event.*;
    
    class Asteroids extends Game {
      public Asteroids() {
        super("Asteroids!",800,600);
      }
      
    	public void paint(Graphics brush) {
        brush.setColor(Color.black);
        brush.fillRect(0,0,width,height);
      }
      
    	public static void main (String[] args) {
        new Asteroids();
      }
    }
    and being a newb i havent got the slightest idea about how could i do this.
    Any tips and information that could, not necessarily solve this thing, but help me get a clearer image of how my solution should be, are very very welcome. I have a problem grasping the general idea of how should i make this work.
    Thank you.

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,813
    Rep Power
    25

    Default

    Start with a small program that can draw something. Then add the code to draw the desired shape.
    Do you have a program that will draw a line or a circle? When that works, replace the drawing code with what you need for the polygon.

  3. #3
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    Java Code:
    class Point {
    	double x;
    	double y;
    
    	public Point(double inX, double inY) { 
    		x = inX; 
    		y = inY; 
    	}
    
    	public Point clone() {
    		return new Point(x, y);
    	}
    }
    That's is good for start. You can also add some other functions for example
    function move (or function translate), function rotate,...

    Java Code:
    	public void move(int xCoord, int yCoord) {
    		x += xCoord;
    		y += yCoord;
    	}

  4. #4
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    I have written one part of the program. You should continue to work on it.


    class Point.java:
    Java Code:
    public class Point {
    	int x;
    	int y;
    	
    	public Point(int inX, int inY) { 
    		this.x = inX; 
    		this.y = inY; 
    	}
    
    	public Point clone() {
    		return new Point(x, y);
    	}
    	
    	public void move(int xCoord, int yCoord) {
    		x += xCoord;
    		y += yCoord;
    	}
    	
    	public int getX() {
    		return x;
    	}
    	
    	public int getY() {
    		return y;
    	}
    }


    class Polygon.java:
    Java Code:
    import java.util.List;
    import java.util.ArrayList;
    
    public class Polygon {
    	List<Point> polygon = new ArrayList<Point>();
    	
    	public Polygon(List<Point> listOfPoints) {
    		polygon = listOfPoints;
    	}
    	
    	public void addNewPointToPolygon(Point point) {
    		polygon.add(point);
    	}
    	
    	public void move(int xCoord, int yCoord) {
    		for(int i = 0; i < polygon.size(); i++)
    			polygon.get(i).move(xCoord, yCoord);
    	}
    	
    	public Point get(int index) {
    			return polygon.get(index);
    	}
    	
    	public int size() {
    		return polygon.size();
    	}
    	
    	public List<Point> getPolygon() {
    		return polygon;
    	}
    }


    class: Ship.java
    Java Code:
    import java.awt.Graphics;
    
    public class Ship {
    	Polygon polygon;
    	Point inPosition;
    	double inRotation;
    	
    	public Ship(Polygon polygon, Point inPosition, double inRotation) {
    		this.polygon = polygon;
    		this.inPosition = inPosition;
    		this.inRotation = inRotation;
    	}
    	
    	public void drawShip(Graphics g) {
    		for(int i = 0; i < polygon.size() - 1; i++)
    			g.drawLine(polygon.get(i).getX(), polygon.get(i).getY(), 
    					polygon.get(i+1).getX(), polygon.get(i+1).getY());
    			
    			//draw line from last Point to the first Point
    			g.drawLine(polygon.get(polygon.size() - 1).getX(), polygon.get(polygon.size() - 1).getY(),
    					polygon.get(0).getX(), polygon.get(0).getY());
    	}
    }


    class: Asteroids.java
    Java Code:
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    
    import java.util.List;
    import java.util.ArrayList;
    
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.SwingUtilities;
    
    public class Asteroids {
    	public static void main(String[] args) {
    		SwingUtilities.invokeLater(new Runnable() {
    			
    			@Override
    			public void run() {
    				AsteroidsFrame frame = new AsteroidsFrame("Asteroids",800,600);
    				frame.startGame();
    			}
    		});
    	}
    }
    
    class AsteroidsFrame extends JFrame {
    	AsteroidsPanel panel;
    	public AsteroidsFrame(String gameName, int width, int height) {
    		panel = new AsteroidsPanel();
    		getContentPane().add(panel);
    		setTitle(gameName);
    		setSize(width, height);
    	}
    	
    	public void startGame() {
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		setVisible(true);
    	}
    }
    
    class AsteroidsPanel extends JPanel {
    	Point inPosition = new Point(350,250);
    	double inRotation = 0;
    	List<Point> listOfPoints = new ArrayList<Point>();
    	Polygon polygon;
    	Ship ship;
    	
    	
    	public AsteroidsPanel() {
    		listOfPoints.add(new Point(300,300));
    		listOfPoints.add(new Point(400,400));
    		listOfPoints.add(new Point(552,362));
    		listOfPoints.add(new Point(300,200));
    		polygon = new Polygon(listOfPoints);
    		ship = new Ship(polygon, inPosition, inRotation);
    	}
    	
    	public void paintComponent(Graphics g) {
    		super.paintComponent(g);
    		
    		Graphics2D g2 = (Graphics2D) g;
    		g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    		
    		ship.drawShip(g2);
    	}
    }

  5. #5
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Default

    wow thats pretty cool. You actually gave me all the vital code parts. The good thing about it is that im actually able to comprehend it. Thank you very much for your help cselic. I was planning to add those "move, rotate etc" functions but i intended to do it on another class so i can keep Point purely for storing coordinates. It would be possible for me to do the editing inside Polygon or Asteroids, right?

  6. #6
    cselic is offline Senior Member
    Join Date
    Apr 2010
    Location
    Belgrade, Serbia
    Posts
    278
    Rep Power
    5

    Default

    In class Point you write functions that you need for Point.
    For example when you rotate ship that means that you are rotating some Points. So write function rotate in class Point.
    When you rotate ship that means that you are rotating Polygon. Then you should write in class Polygon function rotate. That function rotates all Points of Polygon. It is same as with function "public void move(int xCoord, int yCoord)".

    in class Polygon:
    public void rotate(int degre) {

    // write rotation of Point
    }

    in class Polygon:

    public void rotate() {
    // call all Points of Polygon and rotate them all.
    }

  7. #7
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Default

    I dont really understand though the purpose Polygon serves. Ship is a subclass of polygon (extends Polygon) wether I like it or not and thats where the problem is. Polygon was given to me not for creating new shapes through it but for the shapes to extend Polygon. Here is the whole code of Polygon the way it was given to me. I dont have the right to remove or edit anything from in there and that is what makes it so complicated.

    Java Code:
    /*
    CLASS: Polygon
    DESCRIPTION: A polygon is a sequence of points in space defined by a set of
                 such points, an offset, and a rotation. The offset is the
                 distance between the origin and the center of the shape.
                 The rotation is measured in degrees, 0-360.
    USAGE: You are intended to instantiate this class with a set of points that
           forever defines its shape, and then modify it by repositioning and
           rotating that shape. In defining the shape, the relative positions
           of the points you provide are used, in other words: {(0,1),(1,1),(1,0)}
           is the same shape as {(9,10),(10,10),(10,9)}.
    NOTE: You don't need to worry about the "magic math" details.
    */
    
    class Polygon {
      private Point[] shape;   // An array of points.
      public Point position;   // The offset mentioned above.
      public double rotation; // Zero degrees is due east.
      
      public Polygon(Point[] inShape, Point inPosition, double inRotation) {
        shape = inShape;
        position = inPosition;
        rotation = inRotation;
        
        // First, we find the shape's top-most left-most boundary, its origin.
        Point origin = shape[0].clone();
        for (Point p : shape) {
          if (p.x < origin.x) origin.x = p.x;
          if (p.y < origin.y) origin.y = p.y;
        }
        
        // Then, we orient all of its points relative to the real origin.
        for (Point p : shape) {
          p.x -= origin.x;
          p.y -= origin.y;
        }
      }
      
      // "getPoints" applies the rotation and offset to the shape of the polygon.
      public Point[] getPoints() {
        Point center = findCenter();
        Point[] points = new Point[shape.length];
    	int i=0;
        for (Point p : shape) {
          double x = ((p.x-center.x) * Math.cos(Math.toRadians(rotation)))
                   - ((p.y-center.y) * Math.sin(Math.toRadians(rotation)))
                   + center.x/2 + position.x;
          double y = ((p.x-center.x) * Math.sin(Math.toRadians(rotation)))
                   + ((p.y-center.y) * Math.cos(Math.toRadians(rotation)))
                   + center.y/2 + position.y;
          points[i++] = new Point(x,y);
        }
        return points;
      }
      
      // "contains" implements some magical math (i.e. the ray-casting algorithm).
      public boolean contains(Point point) {
        Point[] points = getPoints();
        double crossingNumber = 0;
        for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) {
          if ((((points[i].x < point.x) && (point.x <= points[j].x)) ||
               ((points[j].x < point.x) && (point.x <= points[i].x))) &&
              (point.y > points[i].y + (points[j].y-points[i].y)/
               (points[j].x - points[i].x) * (point.x - points[i].x))) {
            crossingNumber++;
          }
        }
        return crossingNumber%2 == 1;
      }
      
      public void rotate(int degrees) {rotation = (rotation+degrees)%360;}
      
      /*
      The following methods are private access restricted because, as this access
      level always implies, they are intended for use only as helpers of the
      methods in this class that are not private. They can't be used anywhere else.
      */
      
      // "findArea" implements some more magic math.
      private double findArea() {
        double sum = 0;
        for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) {
          sum += shape[i].x*shape[j].y-shape[j].x*shape[i].y;
        }
        return Math.abs(sum/2);
      }
      
      // "findCenter" implements another bit of math.
      private Point findCenter() {
        Point sum = new Point(0,0);
        for (int i = 0, j = 1; i < shape.length; i++, j=(j+1)%shape.length) {
          sum.x += (shape[i].x + shape[j].x)
                   * (shape[i].x * shape[j].y - shape[j].x * shape[i].y);
          sum.y += (shape[i].y + shape[j].y)
                   * (shape[i].x * shape[j].y - shape[j].x * shape[i].y);
        }
        double area = findArea();
        return new Point(Math.abs(sum.x/(6*area)),Math.abs(sum.y/(6*area)));
      }
    }

  8. #8
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,813
    Rep Power
    25

    Default

    What changes do you want to make to the Polygon class? Can those changes be in the Ship class that you're to write?

    Ship is a subclass of polygon (extends Polygon)
    Not shown in the following:
    public class Ship {

  9. #9
    Desdenova is offline Member
    Join Date
    May 2010
    Location
    Greece
    Posts
    7
    Rep Power
    0

    Default

    uh sorry... big fault of mine. ship is actually extends Polygon. Ship only has to keep the coordinates of the points making up it's shape.
    Like that:
    Java Code:
    public class Ship extends Polygon {
    
    	Point[] Alpha = {new Point(0,0), new Point(2,1), new Point(4,0), new Point(2,4)};
    	
    }
    so when this gets into Polygon
    Java Code:
    public Polygon(Point[] inShape, Point inPosition, double inRotation)
    at the inShape slot Polygon gets that data and in addition to what info about movement it gets from Asteroids makes the calculations needed to change the place and direction of the ship on the screen by editing the inPosition and inRotation. It can already do that stuff though.

    I havent understood how can i call in Asteroids the Polygon class with the points loaded from Ship so that polygon gives to asteroids a set of edited points to draw.

  10. #10
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,813
    Rep Power
    25

    Default

    What method does the actual drawing? Are you going to use the Graphics.drawPolygon() method?

    Do you have a simple program that displays a frame and draws some lines on it?
    With that working you'll know how to draw. Then it'll be a question of drawing the shapes you want.

    polygon gives to asteroids a set of edited points to draw
    Not sure what that means. If you're using the drawPolygon method, the points to draw are in two arrays of ints.

Similar Threads

  1. Complicated Method
    By Desmond in forum New To Java
    Replies: 5
    Last Post: 03-17-2010, 12:31 PM
  2. writng event listners ( seems complicated)
    By Basit56 in forum AWT / Swing
    Replies: 1
    Last Post: 08-25-2009, 10:11 AM
  3. how to draw an arc
    By Baker in forum New To Java
    Replies: 1
    Last Post: 04-16-2009, 10:05 PM
  4. How to draw images in SWT
    By Java Tip in forum SWT Tips
    Replies: 0
    Last Post: 07-02-2008, 09:02 PM
  5. help me draw... please...
    By kureikougaiji in forum New To Java
    Replies: 1
    Last Post: 01-28-2008, 01:22 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
  •