Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 17 of 17

Thread: Painting ovals

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Painting ovals

    Hello all,

    I'm just starting to learn Java for a class that I am taking. I've gotten stuck on one of the assignments

    import java.awt.*;
    import java.util.Random;
    import java.util.Scanner;

    import javax.swing.*;

    public class Assignment1 extends JFrame {
    public int gridSize;
    public int pattern;
    public float maxRadius;
    public float minRadius;

    public Assignment1(){


    Scanner scanner = new Scanner(System.in);
    gridSize = scanner.nextInt();
    do{
    System.out.print("Enter pattern (1, 2, or 3): ");
    pattern= scanner.nextInt();
    System.out.println(pattern);
    }
    while(pattern < 1 || pattern >3);
    maxRadius = scanner.nextFloat();
    minRadius = scanner.nextFloat();
    setSize(640, 480);

    }

    public void paintTriangle(Graphics g){
    for(int i = 0; i < gridSize; i++)
    for(int j = 0; j < gridSize; j++){
    if((i+j)%2 == 1);
    g.fillOval(i*40+100, j*40+100, (int)(maxRadius + (minRadius-maxRadius)), 40); break;

    }
    }

    public void paintTriangle2(Graphics g){
    for(int i = 0; i < gridSize; i++)
    for(int j = 0; j < gridSize; j++){
    if((i+j)%3 == 1);
    g.fillOval(i*40+100, j*40+100, 40, 40); break;

    }
    }

    public void paint(Graphics g){
    super.paint(g);
    switch(pattern){
    case 1: paintTriangle(g); break;
    case 2: paintTriangle2(g); break;
    }
    }

    public static void main(String []args){
    Assignment1 a = new Assignment1();
    a.setVisible(true);
    }
    }
    If you run this code, the ovals are painted horizontally, but not vertically, as they should be. What have I done wrong?


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Painting ovals

    if((i+j)%2 == 1);

    That ; after that if statement might not be what you wanted.

    g.fillOval(i*40+100, j*40+100, (int)(maxRadius + (minRadius-maxRadius)), 40); break;

    What's the break for? I don't really see what it's doing.

    Also, you usually don't paint to a JFrame. I did in the past a few times. It did weird things with the painting. Most likely you'll want to use a JPanel subclass and use the paintComponent() method and set that JPanel subclass as the contentPane.

    Also, any reason why you're using ovals to draw the triangle?

    You can use the Graphics method fillPolygon.

    http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillPolygon%28int[],%20int[],%20int%29

    I should mention that with your break statements in there, it will hit that and exit that iteration of the for loop, which might be causing your problem perhaps.

    Basically for an n by n, it'll only do n times as it'll stop every time after hitting that break statement on the first iteration.
    Last edited by javapenguin; June 7th, 2012 at 11:27 PM.

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Proletariat (June 8th, 2012)

  4. #3
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Quote Originally Posted by javapenguin View Post
    if((i+j)%2 == 1);

    That ; after that if statement might not be what you wanted.

    g.fillOval(i*40+100, j*40+100, (int)(maxRadius + (minRadius-maxRadius)), 40); break;

    What's the break for? I don't really see what it's doing.

    Also, you usually don't paint to a JFrame. I did in the past a few times. It did weird things with the painting. Most likely you'll want to use a JPanel subclass and use the paintComponent() method and set that JPanel subclass as the contentPane.

    Also, any reason why you're using ovals to draw the triangle?

    You can use the Graphics method fillPolygon.

    http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html#fillPolygon%28int[],%20int[],%20int%29

    I should mention that with your break statements in there, it will hit that and exit that iteration of the for loop, which might be causing your problem perhaps.

    Basically for an n by n, it'll only do n times as it'll stop every time after hitting that break statement on the first iteration.
    Thanks. It turns out that I needed a bracket instead of a ;. The template for this was provided by our professor, so that's why I'm using JFrame. The assignment is to draw two triangles and a diamond with ovals. Part of it was also allowing the user to enter a grid size, and have a 60% chance of a border being drawn on the individual ovals. I've done well so far but am having a bit of difficulties on making the triangles (what it is drawing right now is a simple pattern that our professor provided as an example). I think that part of the reason that I'm confused is it feels like I'm forcing this; I know of a few easier ways that I could draw the necessary shapes. I guess I'm not understanding how I am supposed to use the if statement to draw a triangle.

    For clarification, this is the picture of the first shape that we need to draw: http://s22.photobucket.com/albums/b3...rrent=pic1.png

    The colors are not necessary.

    So far I've done this:
    public void paintTriangle(Graphics g){
    int number = 0;
    for(int i = 0; number < gridSize; i++) {
    for(int j = 0; j < i; j++) {
    g.fillOval(i*40+100, j*40+100, 40, 40);
    number = number + 1;
    but this returns a triangle that is flipped 180 degrees from what I need, and is in the middle of the page. I am quite confused.
    Last edited by Proletariat; June 8th, 2012 at 02:05 AM.

  5. #4
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    I just have one more question, and I'll be able to complete my project.

    Right now I have this code, and I need to reverse it:
    I have this for loop: int number = 0;
    for(int i = 0; number < 10; i++) {
    for(int j = 0; j < i; j++) {
    System.out.print(number + “ ”);
    number = number + 1;
    }
    System.out.println(“,”);
    }
    It currently returns

    ,
    0 ,
    1 2 ,
    3 4 5 ,
    6 7 8 9 ,

    and I need it to return

    6 7 8 9 ,
    3 4 5 ,
    1 2 ,
    0 ,
    ,

  6. #5
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    Look at what you want to output. What goes on the first line and then on the second line etc.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #6
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Quote Originally Posted by Norm View Post
    Look at what you want to output. What goes on the first line and then on the second line etc.
    I'm having such a hard time doing this. What I really need to do is reflect this image, or just flip it over the x-axis.: pic1.png picture by Gizzy11 - Photobucket

    I know how to reverse a loop, but the problem is, in the template that my professor is requiring us to use, there are two variables, and I'm stuck. I don't know how to handle number and gridSize.

    public void paintTriangle(Graphics g){
    int number = 0;
    for(int i = 0; number < gridSize; i++) {
    for(int j = 0; j < i; j++) {
    g.fillOval(i*40+100, j*40+100, 40, 40);
    number = number + 1;
    Last edited by Proletariat; June 8th, 2012 at 11:15 AM.

  8. #7
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    What is to be printed on the first line? what is printed first and then what second and so on.
    How many printed on the first line before moving to the next line?
    How do you determine the first digit to be printed on each line? Is there a relationship between the number printed on the previous line?

    You need to work out the algorithm before trying to write the code.

    What problem are you working on? The one in post#4 or #6?
    Last edited by Norm; June 8th, 2012 at 11:27 AM. Reason: which problem?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #8
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Post #6. I'm doing a multi step problem. The first part was to draw a triangle with ovals. http://s22.photobucket.com/albums/b3...rrent=pic1.png I was able to do that. Now I'm on the second part of the problem, which is drawing this: imgur: the simple image sharer

    I thought that to do this I would only need to reflect my drawing in the first problem and just have two different for loops. This has proven to be difficult, however, and I may be approaching this the entirely wrong way. The colors of the ovals do not matter, and neither do the borders. All I need to do at this point is get my ovals to make that shape. gridSize just allows the user to select how large they want the grid to be. I have no textbook to reference when trying to do this.
    Last edited by Proletariat; June 8th, 2012 at 11:36 AM.

  10. #9
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    Is the problem to draw ovals such that there is one on the first line, 2 on the second, three on the third, etc?
    If you don't understand my answer, don't ignore it, ask a question.

  11. #10
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Quote Originally Posted by Norm View Post
    Is the problem to draw ovals such that there is one on the first line, 2 on the second, three on the third, etc?
    You are correct. My code to make a the first shape is this:
    int number = 0;
    for(int i = 0; number < gridSize; i++) {
    for(int j = 0; j < i; j++) {
    g.fillOval(j*40+50, i*40+25, 40, 40);
    number = number + 1;
    I can't get the 2nd shape, which is to draw 1 on the first , 2 on the 2nd, and so on up until the user specified grid size, and then back down to 1 again.

  12. #11
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    When drawing on one row, Would the position of the next oval be to the right of the last oval?

    Can you explain what your code does and what is wrong with it?

    For working out the logic, make a small simple program with the loops and just print out the x and y values of where the fillOval() method would draw.

    For example the output could look like this:
    x=50y=65
    x=50y=105
    x=90y=105
    x=50y=145
    x=90y=145
    x=130y=145
    Last edited by Norm; June 8th, 2012 at 11:50 AM.
    If you don't understand my answer, don't ignore it, ask a question.

  13. #12
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Quote Originally Posted by Norm View Post
    When drawing on one row, Would the position of the next oval be to the right of the last oval?
    Doesn't this line set the position?

    g.fillOval(j*40+50, i*40+25, 40, 40);

    I apologize for being so java illiterate. This is only our 2nd day of class and without a textbook and very few notes, I've found it to be quite difficult. I can easily get the number of ovals to increase on each line, but I'm having trouble getting them to decrease.
    Last edited by Proletariat; June 8th, 2012 at 11:49 AM.

  14. #13
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    I'm having trouble getting them to decrease.
    Please explain what you are trying to do. Is it different from what I asked in post#9?

    Did you see the end of post#11?
    Work out the logic with a small program that prints the x,y values.
    If you don't understand my answer, don't ignore it, ask a question.

  15. #14
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    Quote Originally Posted by Norm View Post
    Please explain what you are trying to do. Is it different from what I asked in post#9?

    Did you see the end of post#11?
    Work out the logic with a small program that prints the x,y values.
    My assignment sheet says, "The idea is to draw a shape grid. However, instead of drawing the complete grid, you will only draw in some areas and make a pattern (two types of triangle, and a diamond – see images on page 2). Moreover, this grid consists only of filled ovals."

    I'm trying to do that and get this shape: http://i.imgur.com/pvzI6.jpg

  16. #15
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    Can you describe what that shape will look like? Will need to work out the logic before trying to code it.

    Have you tried writing a small program with some loops that prints the x and y values of where the fillOval() method would draw its shapes? I find it easier to see the pattern from numeric values. See post#11 for an example. Also a small program can be psoted and everyone can work with it more easily than with a GUI program.
    If you don't understand my answer, don't ignore it, ask a question.

  17. #16
    Junior Member
    Join Date
    Jun 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Painting ovals

    I was able to write code that made the correct shape. It turns out that our professor sent us the wrong template, which is why I found it so difficult. All is well that ends well, I suppose.

  18. #17
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    20,940
    Thanks
    51
    Thanked 2,267 Times in 2,239 Posts

    Default Re: Painting ovals

    Sorry, I can't help you with your professor.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. [SOLVED] Painting Issues
    By snowguy13 in forum AWT / Java Swing
    Replies: 9
    Last Post: May 23rd, 2012, 09:54 PM
  2. Output in applet keeps painting over the top of itself
    By SashaThompson in forum Java Applets
    Replies: 1
    Last Post: October 9th, 2011, 11:21 AM
  3. painting Image on JPanel inside a JScrollPane
    By becca23 in forum AWT / Java Swing
    Replies: 1
    Last Post: September 29th, 2010, 07:28 PM
  4. [SOLVED] Extends JPanel painting problem
    By Philip in forum What's Wrong With My Code?
    Replies: 3
    Last Post: July 28th, 2010, 03:16 PM
  5. Painting swing components to an arbitrary position?
    By ScummyChimp in forum AWT / Java Swing
    Replies: 1
    Last Post: September 1st, 2009, 11:06 PM