drawing a diamond inside a square usnig java swing -
i having trouble while using swing draw diamond inside square. code this,please 1 have @ , let me know if can provide fullfunctional code working in creating diamond inside square. code is:-
import javax.swing.*; import java.awt.*; public class mydrawing extends jpanel { static int width=250; static int height=250; static int x=0; static int y=0; private void dodrawing(graphics g) { graphics2d g2d = (graphics2d) g; g2d.setcolor(color.blue); //for (int = 0; <= 1000; i++) g2d.drawrect(x, y, width,height); g2d.rotate(math.toradians(-45)); system.out.println(math.toradians(-45)); x=0; y=height/2; system.out.println(y); width=(int)math.pow(math.pow((width/2),2)*2,0.5); height=width; system.out.println("width:"+width+"height:"+height); g2d.drawrect(y, x, width,height); } @override public void paintcomponent(graphics g) { super.paintcomponent(g); dodrawing(g); } }
to keep the diamond inside sqaure, 1. draw rectangle 2. rotate rectangle center.
rectangle2d rectangle = new rectangle2d.double(20, 20, 50, 50); g2.draw(rectangle); affinetransform transform = new affinetransform(); transform.rotate(math.pi/4, rectangle.getx() + rectangle.width/2, rectangle.gety() + rectangle.height/2); g2.draw(transform);
Comments
Post a Comment