Results 1 to 10 of 10
  1. #1
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default Need help getting java drawing match to image.

    i have one picture here: h**p://enos.itcollege.ee/~aandreim/java/praktikumid/12/tiivik.png

    i have to draw in java so it would match with this picture, what i linked.

    So far i have written this kind of code but it aint workin:
    [CODE]
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.util.ArrayList;
    import java.util.Collections;

    public class Harjutus2 extends Applet {
    private static final long serialVersionUID = 1L;
    public void paint(Graphics g) {
    int x0 = 150; // Center
    int y0 = 150;
    int r = 80; // Radius
    int x, y, l;
    ArrayList <Integer> xv = new ArrayList<Integer>();
    ArrayList <Integer> yv = new ArrayList<Integer>();
    double t;

    // How big is window
    int w = getWidth();
    int h = getHeight();

    // Fill background
    g.setColor(Color.white);
    g.fillRect(0, 0, w, h);

    // Draw
    g.setColor(Color.black);

    for(t = -Math.PI; t < Math.PI; t = t + Math.PI / 10) {

    x = (int)(r * Math.cos(t) + x0);
    y = (int)(r * Math.sin(t) + y0);
    xv.add(x);
    yv.add(y);
    g.drawLine(x0, y0, x, y);
    }

    for (l = 0; l < xv.size(); l++){
    g.drawLine(xv.get(l), yv.get(l), yv.get(l), xv.get(l));
    System.out.print(xv.get(l) + ", ");
    System.out.println(yv.get(l));
    }
    }
    }
    [CODE/]

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

    Default

    Thank of what you want to do when drawing the "caps" on the star. Is there one cap per spike? Nope, a cap is shared by 2 spikes. So you'll want to loop through every other spike (increment the for loop by i += 2, not i++). Then which spike connects with the other to make a cap? It's the next one over from the current one. This is easy to code for if you think about it, so why not try to take a stab at it now?

    good luck!

  3. #3
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Nope, it didnt help. :(

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

    Default

    Also, your program would benefit by taking the arraylists and calculations out of the paint method and into the applet proper (for the array lists) and the init method (for the calculations). else your applet will be creating arraylists and recalculating the points each time it has to repaint.

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

    Default

    Quote Originally Posted by kiduut View Post
    Nope, it didnt help. :(
    C'mon, you can do it. Don't give up.

    let's see your code repeat attempt! :D

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

    Default

    In addition to my recommendations above, I'd use a single ArrayList of Point rather than two parallel arrays of ints. I find it easier to work with. Something like this:
    Java Code:
    public class Harjutus2 extends Applet
    {
      private static final long serialVersionUID = 1L;
      private static final Point CENTER = new Point(150, 150);
      private static final int RADIUS = 80;
      private static final double SLICE_COUNT = 10;
      private ArrayList<Point> points = new ArrayList<Point>();
      
      @Override
      public void init()
      {
        for (double theta = -Math.PI; theta < Math.PI; theta += Math.PI / SLICE_COUNT)
        {
          Point p = new Point();
          p.x = (int) (RADIUS * Math.cos(theta) + CENTER.x);
          p.y = (int) (RADIUS * Math.sin(theta) + CENTER.y);
          points.add(p);
        }
      }

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

    Default

    Did I loose you or are you still trying?

    edit: OK, I guess you lost interest. Well, good by and good luck.
    Last edited by Fubarable; 12-27-2008 at 10:40 PM.

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

    Default

    Java Code:
    // <applet code="H2" width="300" height="300"></applet>
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    
    public class H2 extends Applet {
        private static final long serialVersionUID = 1L;
    
        public void paint(Graphics g) {
            int x0 = 150; // Center
            int y0 = 150;
            int r  =  80; // Radius
            int x, y, l;
            double t;
    
            // How big is window
            int w = getWidth();
            int h = getHeight();
    
            // Fill background
            g.setColor(Color.white);
            g.fillRect(0, 0, w, h);
    
            // Draw
            g.setColor(Color.black);
            int lastX = -1, lastY = -1;
            int count = 0;
            for(t = -Math.PI; t < Math.PI; t = t + Math.PI / 10) {
                x = (int)(r * Math.cos(t) + x0);
                y = (int)(r * Math.sin(t) + y0);
                if(lastX > -1 && (count % 2) == 0) {
                    g.drawLine(lastX, lastY, x, y);
                }
                g.drawLine(x0, y0, x, y);
                lastX = x;
                lastY = y;
                count++;
            }
        }
    }

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

    Default

    I'm not a big fan of spoon-feeding, but even less so when the it advocates bad habits such as keeping the logic in the paint method. So much as I hate to do this, I have to, because this code is better:

    Java Code:
    import java.applet.Applet;
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Point;
    import java.awt.RenderingHints;
    import java.util.ArrayList;
    
    public class Harjutus2 extends Applet
    {
      private static final long serialVersionUID = 1L;
      private static final Point CENTER = new Point(150, 150);
      private static final int RADIUS = 80;
      private static final int SLICE_COUNT_DIV_2 = 5; // must be even, so I'll mult any int by 2!
      private static final int SLICE_COUNT = 2*SLICE_COUNT_DIV_2;
      private ArrayList<Point> points = new ArrayList<Point>();
      
      @Override
      public void init()
      {
        for (int i = 0; i < (2 * SLICE_COUNT); i++)
        {
          double theta = i * Math.PI/(SLICE_COUNT) - Math.PI;
          Point p = new Point();
          p.x = (int) (RADIUS * Math.cos(theta) + CENTER.x);
          p.y = (int) (RADIUS * Math.sin(theta) + CENTER.y);
          points.add(p);
        }
      }
      
      public void paint(Graphics g)
      {
        // How big is window
        int w = getWidth();
        int h = getHeight();
    
        // Fill background
        g.setColor(Color.white);
        g.fillRect(0, 0, w, h);
    
        // Draw
        g.setColor(Color.black);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(
            RenderingHints.KEY_ANTIALIASING, 
            RenderingHints.VALUE_ANTIALIAS_ON);
    
        for (int i = 0; i < points.size(); i++)
        {
          Point p = points.get(i);
          g.drawLine(CENTER.x, CENTER.y, p.x, p.y);
          
          if (i % 2 == 0) // if an "even" spoke, connect it with the next odd one
          {
            Point p2 = points.get(i + 1);
            g.drawLine(p.x, p.y, p2.x, p2.y);
          }
        }
    
      }
    }
    Last edited by Fubarable; 12-28-2008 at 02:48 AM.

  10. #10
    kiduut is offline Member
    Join Date
    Dec 2008
    Posts
    9
    Rep Power
    0

    Default

    Thank you guys! I was sleeping, didnt loose my hope. And with fresh head in the morning, got it to work. Solution was similar to hardwired ones,

    Working draw part:
    Java Code:
    // Draw
            g.setColor(Color.red);
            for(t = -Math.PI; t < Math.PI; t = t + Math.PI / 10) {
                x = (int)(r * Math.cos(t) + x0);
                y = (int)(r * Math.sin(t) + y0);
                if((l % 2) == 1) {
                    g.drawLine(xv, yv, x, y);
                }
                g.drawLine(x0, y0, x, y);
                xv = x;
                yv = y;
                l++;
            }

Similar Threads

  1. [SOLVED] Image drawing, saving
    By Goseph in forum Java 2D
    Replies: 2
    Last Post: 12-21-2008, 07:36 PM
  2. Java Developer - Need Job - This is Perfect match
    By ravisrivastava2003 in forum Java Software
    Replies: 0
    Last Post: 12-10-2008, 07:35 PM
  3. drawing an image to an offscreen image
    By hunterbdb in forum Java 2D
    Replies: 9
    Last Post: 10-30-2008, 06:17 PM
  4. Replies: 0
    Last Post: 07-07-2008, 04:51 PM
  5. Get all groups from a regular expression match?
    By johann_p in forum New To Java
    Replies: 0
    Last Post: 05-16-2008, 07:50 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
  •