shape - How do I draw a star in Java? -
i want draw star off of points. issue is not showing lines little star. missing here? making each point, making lines, setting color, , not show star. show frame thinking issue not frame actual bulk of code. guys suggest trying?
public class starclass implements icon { static jframe frame; public static void main(string[] args) { jframe frame = new jframe(); frame.setsize(400, 400); frame.settitle("my star"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } @override public void painticon(component c, graphics g, int x, int y) { graphics2d g2 = (graphics2d) g; //points point2d.double pt1 = new point2d.double(100, 10); point2d.double pt2 = new point2d.double(125, 75); point2d.double pt3 = new point2d.double(200, 85); point2d.double pt4 = new point2d.double(150, 125); point2d.double pt5 = new point2d.double(160, 190); point2d.double pt6 = new point2d.double(100, 150); point2d.double pt7 = new point2d.double(40, 190); point2d.double pt8 = new point2d.double(50, 125); point2d.double pt9 = new point2d.double(0, 85); //lines line2d.double ln1 = new line2d.double(pt1, pt2); line2d.double ln2 = new line2d.double(pt2, pt3); line2d.double ln3 = new line2d.double(pt3, pt4); line2d.double ln4 = new line2d.double(pt4, pt5); line2d.double ln5 = new line2d.double(pt5, pt6); line2d.double ln6 = new line2d.double(pt6, pt7); line2d.double ln7 = new line2d.double(pt7, pt8); line2d.double ln8 = new line2d.double(pt8, pt9); //color of lines g2.setcolor(color.blue); //draw lines g2.draw(ln1); g2.draw(ln2); g2.draw(ln3); g2.draw(ln4); g2.draw(ln5); g2.draw(ln6); g2.draw(ln7); g2.draw(ln8); } @override public int geticonwidth() { return 200; } @override public int geticonheight() { return 200; } }
you should find better place in painticon.
method not called @ in case.
here code fixed. seems you're missing 1-2 lines
star (this issue should able debug yourself).
import java.awt.*; import java.awt.geom.line2d; import java.awt.geom.point2d; import javax.swing.*; class drawpanel extends jpanel { private static final long serialversionuid = 776058311964590886l; public void paintcomponent(graphics g) { graphics2d g2 = (graphics2d) g; // points point2d.double pt1 = new point2d.double(100, 10); point2d.double pt2 = new point2d.double(125, 75); point2d.double pt3 = new point2d.double(200, 85); point2d.double pt4 = new point2d.double(150, 125); point2d.double pt5 = new point2d.double(160, 190); point2d.double pt6 = new point2d.double(100, 150); point2d.double pt7 = new point2d.double(40, 190); point2d.double pt8 = new point2d.double(50, 125); point2d.double pt9 = new point2d.double(0, 85); // lines line2d.double ln1 = new line2d.double(pt1, pt2); line2d.double ln2 = new line2d.double(pt2, pt3); line2d.double ln3 = new line2d.double(pt3, pt4); line2d.double ln4 = new line2d.double(pt4, pt5); line2d.double ln5 = new line2d.double(pt5, pt6); line2d.double ln6 = new line2d.double(pt6, pt7); line2d.double ln7 = new line2d.double(pt7, pt8); line2d.double ln8 = new line2d.double(pt8, pt9); // color of lines g2.setcolor(color.blue); // draw lines g2.draw(ln1); g2.draw(ln2); g2.draw(ln3); g2.draw(ln4); g2.draw(ln5); g2.draw(ln6); g2.draw(ln7); g2.draw(ln8); } } public class starclass { static jframe frame; public static void main(string[] args) { drawpanel panel = new drawpanel(); jframe frame = new jframe(); frame.getcontentpane().add(panel); frame.setsize(400, 400); frame.settitle("my star"); frame.setdefaultcloseoperation(jframe.exit_on_close); frame.setvisible(true); } }
Comments
Post a Comment