Results 1 to 10 of 10
 12272008, 09:39 PM #1Member
 Join Date
 Dec 2008
 Posts
 9
 Rep Power
 0
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/]

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!
 12272008, 10:00 PM #3Member
 Join Date
 Dec 2008
 Posts
 9
 Rep Power
 0
Nope, it didnt help. :(

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.


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); } }

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; 12272008 at 10:40 PM.
 12282008, 02:11 AM #8Java 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++; } } }

I'm not a big fan of spoonfeeding, 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; 12282008 at 02:48 AM.
 12282008, 01:55 PM #10Member
 Join Date
 Dec 2008
 Posts
 9
 Rep Power
 0
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

[SOLVED] Image drawing, saving
By Goseph in forum Java 2DReplies: 2Last Post: 12212008, 07:36 PM 
Java Developer  Need Job  This is Perfect match
By ravisrivastava2003 in forum Java SoftwareReplies: 0Last Post: 12102008, 07:35 PM 
drawing an image to an offscreen image
By hunterbdb in forum Java 2DReplies: 9Last Post: 10302008, 06:17 PM 
How to limit selection to items that match a pattern in SWT Tree
By Java Tip in forum SWTReplies: 0Last Post: 07072008, 04:51 PM 
Get all groups from a regular expression match?
By johann_p in forum New To JavaReplies: 0Last Post: 05162008, 07:50 PM
Bookmarks