Welcome to Dream.In.Code
Become a Java Expert!

Join 149,523 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,421 people online right now. Registration is fast and FREE... Join Now!




graphical objects

 
Reply to this topicStart new topic

graphical objects

dogz
26 May, 2007 - 07:19 AM
Post #1

New D.I.C Head
*

Joined: 15 Apr, 2007
Posts: 13


My Contributions
I am trying to create an applet that displays a Dialog box giving the user a choice of shapes to draw and in the applets paint() method i need to have a for loop that loops at least 100 times. Inside the loop i need a switch statement that draws the line rectangle oval or polygon depending on the users choice. With each repeat of the loop, the shape drawn should be positioned in a slightly different place to create an interesting pattern. If an invalid number is entered, the default case should use the drawString() method to write an error message.

i do not understand how to repeat the object 100x to create the pattern, below is how far i have made it so far.


CODE
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;

/**
* create a shape then have it repeat 100x.
*
* @author (Simon Orazi)
* @version (26/05/07)
*/
public class Shapes extends JApplet
{
    String input;
    int choice;

    public void init()
    {
        do
        {
            input = JOptionPane.showInputDialog("Enter 1 to draw lines\n" + "Enter 2 to draw rectangles\n" +
                                  "Enter 3 to draw ovals\n" + "Enter 4 to draw polygons");  
            choice = Integer.parseInt(input);
        
            
            public void paint(Graphics g)
            {
                switch (choice)
                {
                    case 1:
                        if (choice == 1);
                        drawLine(g);
                        break;
                    case 2:
                        if (choice == 2);
                        drawRectangle(g);
                        break;
                    case 3:    
                        if (choice == 3);
                        drawOval(g);
                        break;
                    case 4:
                        if (choice == 4);
                        drawPolygon(g);
                        break;
                    default:
                }       g.drawString("Please enter 1, 2, 3 or 4", 20, 20);
           }while (choice > 4);
    }        
    public void drawLine(Graphics g)
    {
        g.drawLine(20, 20, 20, 80);
        for(choice=100; choice<100; choice++);
    }    
    public void drawRectangle(Graphics g)
    {
        g.drawRect(20, 20, 80, 80);
        for(choice=100; choice<100; choice++);
    }    
    
    public void drawOval(Graphics g)
    {
        g.drawOval(20, 20, 12, 75);
        for(choice=100; choice<100; choice++);
    }    
    
    public void drawPolygon(Graphics g)
    {
        int[] xCoords = {60, 100, 140, 140,
                             100, 60, 20, 20};
        int[] yCoords = {20, 20, 60, 100,
                            140, 140, 100, 60};
        g.drawPolygon(xCoords, yCoords, 8);
        for(choice=100; choice<100; choice++);
    }    
}

User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Graphical Objects
26 May, 2007 - 08:42 AM
Post #2

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
Do something similar like I did with the for loops.
Also you've made some errors like

QUOTE
if (choice == 1);


QUOTE
for(choice=100; choice<100; choice++);



p
QUOTE
ublic void paint(Graphics g)

should be outside of init().
Here is what I did:


CODE
import javax.swing.JOptionPane;
import java.awt.*;
import javax.swing.*;
import java.applet.*;

/**
* create a shape then have it repeat 100x.
*
* @author (Simon Orazi)
* @version (26/05/07)
*/
public class Shapes extends JApplet
{
    String input;
    int choice;

    public void init()
    {
        do
        {
            input = JOptionPane.showInputDialog("Enter 1 to draw lines\n" + "Enter 2 to draw rectangles\n" +
                                  "Enter 3 to draw ovals\n" + "Enter 4 to draw polygons");  
            choice = Integer.parseInt(input);
        
            //paint(Graphics g);
            repaint();
               
        }while (choice > 4);  
        
  
}


public void paint(Graphics g)
            {
                switch (choice)
                {
                    case 1:
                        if (choice == 1)
                                {g.drawLine(20,20,20,20);
                                break;}
                    case 2:
                        if (choice == 2)
                                {drawRectangle(g);
                                break;}
                    case 3:    
                        if (choice == 3)
                                {drawOval(g);
                                break;}
                    case 4:
                        if (choice == 4)
                                {drawPolygon(g);
                                break;}
                        default: break;
                    
                }       g.drawString("Please enter 1, 2, 3 or 4", 20, 20);
           }    
          
          
    public void drawLine(Graphics g)
    {
        g.drawLine(20, 20, 20, 80);
        for(choice=100; choice<100; choice++){}
    }    
    public void drawRectangle(Graphics g)
    {
        
        for(choice=0; choice<100; choice++){g.drawRect(20+choice*2, 20+choice, 80+choice, 80);repaint();}
    }    
    
    public void drawOval(Graphics g)
    {
        g.drawOval(20, 20, 12, 75);
        for(choice=100; choice<100; choice++){}
    }    
    
    public void drawPolygon(Graphics g)
    {
        
        
        for(choice=0; choice<100; choice++)
        {
            int[] xCoords = {60+2*choice, 100+2*choice, 140+2*choice, 140+2*choice,
                             100+2*choice, 60+2*choice, 20+2*choice, 20+2*choice};
            int[] yCoords = {20+2*choice, 20+2*choice, 60+2*choice, 100+2*choice,
                            140+2*choice, 140+2*choice, 100+2*choice, 60+2*choice};
            g.drawPolygon(xCoords, yCoords, 8);
            repaint();
        }
    }
}

User is offlineProfile CardPM
+Quote Post

spullen
RE: Graphical Objects
26 May, 2007 - 12:58 PM
Post #3

D.I.C Regular
Group Icon

Joined: 22 Mar, 2007
Posts: 330



Thanked: 1 times
Dream Kudos: 50
My Contributions
in your switch statement, why do you also have if statements, seems kind of repetitive.
User is offlineProfile CardPM
+Quote Post

PennyBoki
RE: Graphical Objects
26 May, 2007 - 03:12 PM
Post #4

system("revolution");
Group Icon

Joined: 11 Dec, 2006
Posts: 2,010



Thanked: 7 times
Dream Kudos: 500
Expert In: Java,C++,C

My Contributions
yes it does I've overlooked it.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:38PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month