Results 1 to 3 of 3
Thread: Bulls Eye drawing
- 04-01-2012, 05:08 AM #1
Member
- Join Date
- Jun 2011
- Posts
- 54
- Rep Power
- 0
Bulls Eye drawing
Hi All! Writing code to draw Bulls Eye picture and did everything correct except don't know how to fix it to the frame size. Tried few variants but no luck so far. We have:
BullsEye class:
Java Code:import java.awt.Color; import java.awt.Graphics2D; import java.awt.geom.Ellipse2D; /** A circle shape that can be placed anywhere on the screen. * * @author FOX * */ public class BullsEye { public BullsEye(int x, int y) { xLeft = x; yTop = y; } public void draw(Graphics2D g2) { Ellipse2D.Double circle = new Ellipse2D.Double(xLeft, yTop, 300,300); g2.draw(circle); g2.setColor(Color.BLACK); g2.fill(circle); Ellipse2D.Double circle2 = new Ellipse2D.Double(xLeft+25, yTop+25, 250, 250); g2.draw(circle2); g2.setColor(Color.WHITE); g2.fill(circle2); Ellipse2D.Double circle3 = new Ellipse2D.Double(xLeft+50, yTop+50, 200, 200); g2.draw(circle3); g2.setColor(Color.BLACK); g2.fill(circle3); Ellipse2D.Double circle4 = new Ellipse2D.Double(xLeft+75, yTop+75, 150, 150); g2.draw(circle4); g2.setColor(Color.WHITE); g2.fill(circle4); Ellipse2D.Double circle5 = new Ellipse2D.Double(xLeft+100, yTop+100, 100, 100); g2.draw(circle5); g2.setColor(Color.BLACK); g2.fill(circle5); } private int xLeft; private int yTop; }
Java Code:import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.JComponent; /** This component draws five circle shapes. * * * @author FOX * */ public class BullsEyeComponent extends JComponent { public void paintComponent (Graphics g) { Graphics2D g2 = (Graphics2D) g; BullsEye eye = new BullsEye(0,0); eye.draw(g2); } } BullsEyeViewer: import javax.swing.JFrame; public class BullsEyeViewer { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub JFrame frame = new JFrame(); frame.setSize(350, 350); frame.setTitle("Bulls Eye"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); BullsEyeComponent component = new BullsEyeComponent(); frame.add(component); frame.setVisible(true); } }
here...I just want picture to change its size in both sides according to the change of frame size when you do it with your mouse. How I can do it? Thank you guys for help!Last edited by Fubarable; 04-01-2012 at 05:17 AM. Reason: code tags added
-
Re: Bulls Eye drawing
Consider:
- Don't create a new BullsEye class in the paintComponent class. There's no need to create more than one of these objects, and in particular, you don't want to create objects unnecessarily in paintComponent, a method that must be as fast as possible.
- Create one BullsEye class in your gui class's constructor.
- Either give Bullseye a constructor that takes a JComponent as a parameter and pass this into it, and then in its draw(...) method have BullsEye check the size of the component, or
- instead you could change draw to accept a Graphics object and a Dimension object, the latter holding the JComponent's current size.
- Please consider using [code] [/code] tags when adding code to the forum.
Good luck.
- 04-01-2012, 06:32 AM #3
Similar Threads
-
help in drawing tex
By Rampletero in forum Advanced JavaReplies: 8Last Post: 03-10-2012, 05:17 PM -
need help for drawing...
By nice7 in forum New To JavaReplies: 4Last Post: 12-06-2011, 03:34 PM -
Drawing an arc
By berkeleybross in forum Java 2DReplies: 10Last Post: 12-09-2010, 01:32 AM -
Drawing a map
By Karp in forum AWT / SwingReplies: 4Last Post: 11-07-2008, 12:26 PM -
Help with 2-D Drawing
By Deathmonger in forum New To JavaReplies: 4Last Post: 06-18-2008, 02:23 AM
Bookmarks