Jump to content

Sum java graphics


Guest K1u

Recommended Posts

// ***************************************************************

// Face.java- Creates Face using java programming language 

// 

// Author- Kiumarz Hashemi

// 

// ***************************************************************





import java.applet.Applet;

import java.awt.*;

import java.util.Random;





public class Face extends Applet

{

    public void paint (Graphics page)

    {

    

    Random generator = new Random();

    Random rg = new Random();

    

    final int MAX_SIZE = 300;

    final int PAGE_WIDTH = 800;

    final int PAGE_HEIGHT = 600;

    

    int x, y;   

    int width, height; 

    

    // Background

    setBackground (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));



      // Face

      page.setColor (new Color(255, 255, 0)); 

      

      x = 150;

      y = 100;

    

      width = 250;

      height = 250;

    

      page.fillOval(x, y, width, height);  

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      

      x = 175;    x = generator.nextInt(PAGE_WIDTH); 

                       

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75;

      height = 75;

      

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75;

      height = 75;

      

      page.fillOval(x, y, width, height);

      

      // Smile

      page.setColor (new Color(25, 100, 25));

      

      x = 300;

      y = 250;

      

      width = 200;

      height = 200;

      

      page.drawArc (200, 200, 150, 100, 225, 100);

      

      // Left eye pupil

      page.setColor (Color.black);

      

      x = 200;

      y = 200;

      

      width = 25;

      height = 25;

      

      page.fillOval(x, y, width, height);

      

      // Right eye pupil

      page.setColor (Color.black);

      

      x = 325;

      y = 200;

      

      width = 25;

      height = 25;

      

      page.fillOval(x, y, width, height);

      

      // Nose

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      

      x = 265;

      y = 225;

      

      width = 25;

      height = 25;

      

      page.fillOval(x, y, width, height);

      

      // Text

      page.drawString ("Applet by K1u", 235, 150);

      

      // Left ear

      page.setColor (new Color(255, 25, 50));

      

      x = 105;

      y = 180;

      

      width = 50;

      height = 75;

      

      page.fillOval(x, y, width, height);

    

      // Right ear

      page.setColor (new Color(255, 25, 50));

      

      x = 395;

      y = 180;

      

      width = 50;

      height = 75;

      

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      

      x = 175;    x = generator.nextInt(PAGE_WIDTH);

                       

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75; 

      

      height = 75; 

      

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75; 

      

      height = 75; 

      

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      

      x = 175;    x = generator.nextInt(PAGE_WIDTH);

                       

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75;  

      

      height = 75; 

      

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      

      width = 75; 

      

      height = 75; 

      

      page.fillOval(x, y, width, height);

      

      

    }

}

Something i made when i was bored

Use that html code to launch it.

<html>

<applet code="Face.class" width=800 height=600>

</applet>

</html>

Does anybody have any suggestions on what else to put into it (im gonna be putting some more stuff into it)

Link to comment
Share on other sites

one suggest is not to use so many new lines. Use a new line to seperate blocks of things e.g:

// Face

     page.setColor (new Color(255, 255, 0));

    

     x = 150;

     y = 100;

   

     width = 250;

     height = 250;

   

     page.fillOval(x, y, width, height);

would look better as

// Face

     page.setColor (new Color(255, 255, 0));

     x = 150;

     y = 100;

     width = 250;

     height = 250;

     page.fillOval(x, y, width, height);  



//Next part

......

Link to comment
Share on other sites

one suggest is not to use so many new lines. Use a new line to seperate blocks of things e.g:

// Face

     page.setColor (new Color(255, 255, 0));

    

     x = 150;

     y = 100;

   

     width = 250;

     height = 250;

   

     page.fillOval(x, y, width, height);

// Face

     page.setColor (new Color(255, 255, 0));

     x = 150;

     y = 100;

     width = 250;

     height = 250;

     page.fillOval(x, y, width, height);  



//Next part

......

Hows that look.

// ***************************************************************

// K1u Smiley.java- Creates Face using java programming language 

// 

// Author- Kiumarz Hashemi

// 

// ***************************************************************





import java.applet.Applet;

import java.awt.*;

import java.util.Random;





public class Face extends Applet

{

    public void paint (Graphics page)

    {

    

    Random generator = new Random();

    Random rg = new Random();

    

    final int MAX_SIZE = 300;

    final int PAGE_WIDTH = 800;

    final int PAGE_HEIGHT = 600;

    

    int x, y;   

    int width, height; 

    

    // Background

    setBackground (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));



      // Face

      page.setColor (new Color(255, 255, 0)); 

      x = 150;

      y = 100;

      width = 250;

      height = 250;

      page.fillOval(x, y, width, height);  

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      x = 175;    x = generator.nextInt(PAGE_WIDTH);                

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      width = 75;

      height = 75;

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      width = 75;

      height = 75;

      page.fillOval(x, y, width, height);

      

      // Smile

      page.setColor (new Color(25, 100, 25));

      x = 300;

      y = 250;

      width = 200;

      height = 200;

      page.drawArc (200, 200, 150, 100, 225, 100);

      

      // Left eye pupil

      page.setColor (Color.black);

      x = 200;

      y = 200;

      width = 25;

      height = 25;

      page.fillOval(x, y, width, height);

      

      // Right eye pupil

      page.setColor (Color.black);

      x = 325;

      y = 200;

      width = 25;

      height = 25;

      page.fillOval(x, y, width, height);

      

      // Nose

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      x = 265;

      y = 225;

      width = 25;

      height = 25;

      page.fillOval(x, y, width, height);

      

      // Text

      page.drawString ("Applet by K1u", 235, 150);

      

      // Left ear

      page.setColor (new Color(255, 25, 50));

      x = 105;

      y = 180;

      width = 50;

      height = 75;

      page.fillOval(x, y, width, height);

    

      // Right ear

      page.setColor (new Color(255, 25, 50));

      x = 395;

      y = 180;

      width = 50;

      height = 75;

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      

      x = 175;    x = generator.nextInt(PAGE_WIDTH);            

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      width = 75; 

      height = 75; 

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      width = 75; 

      height = 75; 

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));    

      x = 175;    x = generator.nextInt(PAGE_WIDTH);            

      y = 150;  y = generator.nextInt(PAGE_HEIGHT);

      width = 75;  

      height = 75; 

      page.fillOval(x, y, width, height);

      

      // Random ball

      page.setColor (new Color(rg.nextInt(255), rg.nextInt(255), rg.nextInt(255), rg.nextInt(255)));

      x = 300; x = generator.nextInt(PAGE_WIDTH);

      y = 150; y = generator.nextInt(PAGE_HEIGHT);

      width = 75; 

      height = 75; 

      page.fillOval(x, y, width, height);

      

      

    }

}

Link to comment
Share on other sites

Code like that makes me want to stab someone in the neck with a no.2 pencil.

// ***************************************************************

// K1u Smiley.java- Creates Face using java programming language

//

// Author- Kiumarz Hashemi

//

// ***************************************************************



import java.applet.Applet;

import java.awt.*;

import java.util.Random;



public class Face extends Applet {



    private Random randomGenerator = new Random();



    private static final int MAX_COLORPART = 255;

    private static final int PAGE_WIDTH = 800;

    private static final int PAGE_HEIGHT = 600;



    private Color getRandomColor() {

        int red = randomGenerator.nextInt(MAX_COLORPART);

        int green = randomGenerator.nextInt(MAX_COLORPART);

        int blue = randomGenerator.nextInt(MAX_COLORPART);

        int alpha = randomGenerator.nextInt(MAX_COLORPART);

        return new Color(red, green, blue, alpha);

    }



    private void drawRandomBall(int width, int height) {

        page.setColor( getRandomColor() );

        int x = randomGenerator.nextInt(PAGE_WIDTH);

        int y = randomGenerator.nextInt(PAGE_HEIGHT);

        page.fillOval(x, y, width, height);

    }



    public void paint (Graphics page) {



        // Background

        setBackground( getRandomColor() );

        // Face ... I don't get this comment.

        page.setColor (new Color(255, 255, 0));

        page.fillOval(150,100,250,250);

        // Random balls

        drawRandomBall(75,75);

        drawRandomBall(75,75);



        // Smile

        page.setColor(new Color(25, 100, 25));

        page.drawArc(200, 200, 150, 100, 225, 100);



        // Left eye pupil

        page.setColor(Color.black);

        page.fillOval(200,200,25,25);

        // Right eye pupil

        page.fillOval(325,200,25,25);



        // Nose

        page.setColor( getRandomColor() );

        page.fillOval(265, 225, 25, 25);



        // Left ear

        page.setColor (new Color(255, 25, 50));

        page.fillOval( 105, 180, 50, 75);

        // Right ear

        page.fillOval( 395, 180, 50, 75);



        // Random ball

        drawRandomBall(75,75);

        drawRandomBall(75,75);

        drawRandomBall(75,75);

        drawRandomBall(75,75);



        // Text

        page.drawString ("Applet by K1u", 235, 150);

    }

}

Your method was *WAAAAAAY* too long. And unneccessarily so even.

You don't set a variable to a value, and then set it again without using it in the mean time.

When you set a variable to something, the idea is that you play with it. Use it a couple more times. Instead, you then pass the variable to a function, and then overwrite the value of the variable to start over. What's the point of having the variable then? Just fill in a fixed value.

When you notice repetition in your code, take that repeating stuff and dunk it into its own method.

Don't use 2 Random objects. It's not like the other one will give you 'more' random numbers...

You should now take out the smiley-drawing part of the method, and put that in its own method. Pass it the x, y and diameter that constrains it.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...