Results 1 to 6 of 6
- 10-19-2012, 11:31 PM #1
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Trying to build new shape program
I have already written some code to produce the picture below. I just need some help duplicating the shapes on the second line of the picture. If you could help me develop some simple code to produce the output would be extremely appreciative!!
OUTPUT I NEED:

OUTPUT I HAVE:

THIS IS MY CODE:
Thank you so much for your help.Java Code:import java.awt.*; public class Illusion { public static void main(String[] args) { DrawingPanel panel = new DrawingPanel (500, 400); panel.setBackground(Color.GRAY); Graphics g = panel.getGraphics(); drawBasic(g, 0, 0, 3, 90); drawBasic(g, 120, 10, 3, 90); drawBasic(g, 250, 50, 5, 90); drawGrid(g, 10, 120, 10, 100, 2); drawGrid(g, 350, 20, 5, 40, 3); drawGrid(g, 230, 160, 5, 50, 4); } public static void drawBasic(Graphics g, int x, int y, int circles, int size) { int gap=size/(2*circles); g.setColor(Color.RED); g.fillOval(x, y, size, size); g.setColor(Color.BLACK); for (int i=0; i<circles; i++) { g.drawOval(x+(i*gap), y+(i*gap), size-(2*i*gap), size-(2*i*gap)); } Polygon diamond = new Polygon(); diamond.addPoint(x+(size/2), y); diamond.addPoint(x, y+(size/2)); diamond.addPoint(x+(size/2), y+size); diamond.addPoint(x+size, y+(size/2)); g.drawPolygon(diamond); } public static void drawGrid(Graphics g, int x, int y, int circles, int size, int rows){ g.setColor(Color.LIGHT_GRAY); g.fillRect(x, y, size*rows, size*rows); g.setColor(Color.RED); g.drawRect(x, y, size*rows, size*rows); for(int i=0; i<rows; i++) { drawBasic(g, x+(i*size), y, circles, size); } } }Last edited by Fubarable; 10-19-2012 at 11:37 PM. Reason: Post toned down a bit. Code tags added.
-
Re: Trying to build new shape program
Moderator edit: code tags added to posted code, extra exclamation marks removed, all references to ASAP removed.
To the original poster, please calm down. Too many exclamation marks and urgent requests will usually have the opposite effect here. Yes your post is important, but please realize that it is no more important than any other question on this site.
-
Re: Trying to build new shape program
It looks to me as if you're missing some for loops that would allow you to create several rows of your images. We're also not seeing a bit of your code including your DrawingPanel class. Also, in general you should avoid obtaining a Graphics object by calling getGraphics() on a GUI component since the Graphics object obtained won't persist. To see what I mean, try minimizing and then restoring your GUI. Do any of your graphics disappear after you've done this?
- 10-19-2012, 11:50 PM #4
Member
- Join Date
- Oct 2012
- Posts
- 2
- Rep Power
- 0
Re: Trying to build new shape program
I am using jgrasp for the programming and I believe you have to call for graphics, but I could be wrong. Also, I am extreme intermediate at java and it took me awhile to write this code, but now I am stuck with trying the same but duplicating it.
-
Re: Trying to build new shape program
And so could I as I'm not very familiar with JGrasp. Unfortunately neither are many of us here since it really isn't used much outside of intro to Java courses.
No one will give you the code of course, and since you're using JGrasp, I doubt that they even could. But just glancing at your images, it looks like you're using a for loop to create a row of each image (I'm guessing here), but that you may need to use nested for loops to create multiple rows of images, one on top of the other.Also, I am extreme intermediate at java and it took me awhile to write this code, but now I am stuck with trying the same but duplicating it.
- 10-21-2012, 06:11 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 5
- Rep Power
- 0
Re: Trying to build new shape program
You should be doing your painting by overriding DrawingPanel.paintComponent(Graphics)
(or just implementing it in DrawingPanel, if DrawingPanel is your own class).
Also, technically you shouldn't create a DrawingPanel outside the AWT event
dispatch thread, which means you should create it within a SwingUtilites.invokeLater().
This rarely causes a problem for simple examples though, and your instructor would
probably question you about why you did it. Certainly you shouldn't do other things,
like obtaining a Graphics and using it, outside the EDT.
Similar Threads
-
Need help ASAP on a math program
By Swiper in forum New To JavaReplies: 11Last Post: 08-13-2011, 06:16 AM -
Help with a PolyGrapher Program ASAP
By acole5 in forum New To JavaReplies: 6Last Post: 06-15-2011, 03:35 AM -
HELP with a plotter program ASAP!
By acole5 in forum New To JavaReplies: 3Last Post: 06-03-2011, 12:16 AM -
Assistance needed ASAP: Postpix program
By Debonairj in forum New To JavaReplies: 18Last Post: 07-27-2010, 01:37 PM -
Need help ASAP with Payroll Program Part 2
By arrech326 in forum New To JavaReplies: 10Last Post: 11-17-2009, 10:17 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks