graphics - Random shape generator in Java -
i trying build program @ least 10 shapes created randomly , given random places. far have this:
import javax.swing.jframe; public class randomshapeviewer { public static void main(string[] args) { jframe frame = new jframe(); final int frame_width = 300; final int frame_height = 400; frame.setsize(frame_width, frame_height); frame.settitle("randomshapeviewer"); frame.setdefaultcloseoperation(jframe.exit_on_close); randomshapescomponent component = new randomshapescomponent(); frame.add(component); frame.setvisible(true); } }
and
import javax.swing.jcomponent; import java.awt.graphics; import java.awt.graphics2d; public class randomshapescomponent extends jcomponent { public void paintcomponent(graphics g) { graphics2d g2 = (graphics2d) g; randomshapegenerator r = new randomshapegenerator(getwidth(), getheight()); (int = 1; <= 10; i++) g2.draw(r.graphics()); } }
and
import java.awt.shape; import java.awt.geom.ellipse2d; import java.awt.geom.line2d; import java.awt.geom.rectangle2d; import java.util.random; import java.awt.*; import java.awt.event.*; public class randomshapegenerator { int width, height; random ran = new random(); public randomshapegenerator(int i, int j) { int width = i; int height = j; } public void paintcomponent(graphics g) { switch(ran.nextint(10)) { default: case 0: g.drawoval(10, 20, 10, 20); case 1: g.drawline(100, 100, 150, 150); case 2: g.drawrect(30,40,30,40); } } }
now have couple of questions:
- is possible draw multiple lines in 1 case (and creating triangle) , if how go doing this?
- i error message: 1 error found: file: d:\downloads\wallpaper\randomshapescomponent.java [line: 14] error: cannot find symbol symbol: method graphics() location: variable r of type randomshapegenerator
- furthermore follow-up question on first question: how able fill in oval , rectangle, etc. solid color?
is possible draw multiple lines in 1 case (and creating triangle)
of course. call drawline
3 times, coordinates of triangle. don't forget break
statement in each of case
s.
i error message 1 error found
you don't have of necessary imports, or have typo in source code. sure graphics()
spelled upper-case letter?
how able fill in oval , rectangle etc solid color.
use fillrectangle
and/or filloval
apis.
Comments
Post a Comment