trouble putting together program for designing catface that will resize in the frame

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.util.Random;
import java.awt.geom.Arc2D;
import java.awt.BasicStroke;

/**
 * A component that draws a Cat Face.
 *@Kevin Bennett
*/

public class CatComponentalt extends JComponent

{
    public void paintComponent(Graphics g)

    {

    // Recover Graphics2D
    Graphics2D g2 = (Graphics2D) g;

    int w = getWidth();
    int h = getHeight();
    int size = Math.min(w, h);

    // translate the origin to the center of the component.
    g2.translate(w / 2, h / 2);

    int radius1 = - (int) (2.0 / 11.0 * size);

    Rectangle r1 = new Rectangle(w, h);
    g2.setColor(Color.YELLOW);
    g2.fill(r1);

    g2.setColor(Color.BLACK);

    // Draw the cat head.
    // Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
    // g2.translate(200, 200);


    g2.draw(radius1);

    // Draw the cat head.
    //Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
    //g2.translate(200, 200);




    // Draw the eye number 1.


    Ellipse2D.Double eye1 = new Ellipse2D.Double(50, 35, 20, 20);
    g2.draw(eye1);
    g2.setColor(Color.BLUE);
    g2.fill(eye1);
    //Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90);
    //g2.draw(eye1);

    // Draw the eye number 2.
    Ellipse2D.Double eye2 = new Ellipse2D.Double(90, 35, 20, 20);
    g2.draw(eye2);
    g2.setColor(Color.BLUE);
    g2.fill(eye2);
    //Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90);
    //g2.draw(eye2);

    // Draw the mouth
    Arc2D.Double mouth = new Arc2D.Double(65, 110, 60, 60, 90, 90, Arc2D.OPEN);
    g2.draw(mouth);

    // Draw the Whiskers 1.
    Line2D.Double whisker1 = new Line2D.Double(0, 95, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker1);

    // Draw the Whiskers 2.
    Line2D.Double whisker2 = new Line2D.Double(0, 110, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker2);

    // Draw the Whiskers 3.
    Line2D.Double whisker3 = new Line2D.Double(0, 125, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker3);

    // Draw the Whiskers 4.
    Line2D.Double whisker4 = new Line2D.Double(95, 110, 160, 95);
    g2.draw(whisker4);

    // Draw the Whiskers 5.
    Line2D.Double whisker5 = new Line2D.Double(95, 110, 160, 110);
    g2.draw(whisker5);

    // Draw the Whiskers 6.
    Line2D.Double whisker6 = new Line2D.Double(95, 110, 160, 125);
    g2.draw(whisker6);

    // Draw the Ear 1a
    Line2D.Double ear1 = new Line2D.Double(5, 10, 30, 25);
    g2.draw(ear1);

    // Draw the Ear 1b
    Line2D.Double ear2 = new Line2D.Double(5, 10, 20, 40);
    g2.draw(ear2);

    // Draw the Ear 2a
    Line2D.Double ear3 = new Line2D.Double(125, 25, 155, 15);
    g2.draw(ear3);

    // Draw the Ear 2b
    Line2D.Double ear4 = new Line2D.Double(140, 40, 155, 15);
    g2.draw(ear4);

    // Draw the greeting
    g2.setColor(Color.BLUE);
    g2.drawString("Hello, Drew", 45, 175);
    g2.drawString("Hello, Drew", 45, 200);

    }


    private void drawRandomCircle(Graphics2D kevin)
    {
        int w = getWidth();
        int h = getHeight();

    //Create the head in a random position
    Random r = new Random();
    final int SIZE = 150;  //width and height of the head

    // coordinates of top-left corner of bounding box

    int x = r.nextInt(w - SIZE);
    int y = r.nextInt(h - SIZE);

    Ellipse2D.Double head = new Ellipse2D.Double(x, y, SIZE, SIZE);

    kevin.setColor(Color.YELLOW);
    kevin.fill(head);
    }


}

suggestions

Edited 3 Years Ago by Dani: Formatting fixed

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.util.Random;
import java.awt.geom.Arc2D;
import java.awt.BasicStroke;

/**
 * A component that draws a Cat Face.
 *@Kevin Bennett
*/

public class CatComponentalt extends JComponent

{
    public void paintComponent(Graphics g)

    {

    // Recover Graphics2D
    Graphics2D g2 = (Graphics2D) g;

    int w = getWidth();
    int h = getHeight();
    int size = Math.min(w, h);

    // translate the origin to the center of the component.
    g2.translate(w / 2, h / 2);

    int radius1 = - (int) (2.0 / 11.0 * size);

    Rectangle r1 = new Rectangle(w, h);
    g2.setColor(Color.YELLOW);
    g2.fill(r1);

    g2.setColor(Color.BLACK);

    // Draw the cat head.
    // Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
    // g2.translate(200, 200);


    g2.draw(radius1);

    // Draw the cat head.
    //Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
    //g2.translate(200, 200);




    // Draw the eye number 1.


    Ellipse2D.Double eye1 = new Ellipse2D.Double(50, 35, 20, 20);
    g2.draw(eye1);
    g2.setColor(Color.BLUE);
    g2.fill(eye1);
    //Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90);
    //g2.draw(eye1);

    // Draw the eye number 2.
    Ellipse2D.Double eye2 = new Ellipse2D.Double(90, 35, 20, 20);
    g2.draw(eye2);
    g2.setColor(Color.BLUE);
    g2.fill(eye2);
    //Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90);
    //g2.draw(eye2);

    // Draw the mouth
    Arc2D.Double mouth = new Arc2D.Double(65, 110, 60, 60, 90, 90, Arc2D.OPEN);
    g2.draw(mouth);

    // Draw the Whiskers 1.
    Line2D.Double whisker1 = new Line2D.Double(0, 95, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker1);

    // Draw the Whiskers 2.
    Line2D.Double whisker2 = new Line2D.Double(0, 110, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker2);

    // Draw the Whiskers 3.
    Line2D.Double whisker3 = new Line2D.Double(0, 125, 65, 110);
    g2.setStroke(new BasicStroke(2));
    g2.draw(whisker3);

    // Draw the Whiskers 4.
    Line2D.Double whisker4 = new Line2D.Double(95, 110, 160, 95);
    g2.draw(whisker4);

    // Draw the Whiskers 5.
    Line2D.Double whisker5 = new Line2D.Double(95, 110, 160, 110);
    g2.draw(whisker5);

    // Draw the Whiskers 6.
    Line2D.Double whisker6 = new Line2D.Double(95, 110, 160, 125);
    g2.draw(whisker6);

    // Draw the Ear 1a
    Line2D.Double ear1 = new Line2D.Double(5, 10, 30, 25);
    g2.draw(ear1);

    // Draw the Ear 1b
    Line2D.Double ear2 = new Line2D.Double(5, 10, 20, 40);
    g2.draw(ear2);

    // Draw the Ear 2a
    Line2D.Double ear3 = new Line2D.Double(125, 25, 155, 15);
    g2.draw(ear3);

    // Draw the Ear 2b
    Line2D.Double ear4 = new Line2D.Double(140, 40, 155, 15);
    g2.draw(ear4);

    // Draw the greeting
    g2.setColor(Color.BLUE);
    g2.drawString("Hello, Drew", 45, 175);
    g2.drawString("Hello, Drew", 45, 200);

    }


    private void drawRandomCircle(Graphics2D kevin)
    {
        int w = getWidth();
        int h = getHeight();

    //Create the head in a random position
    Random r = new Random();
    final int SIZE = 150;  //width and height of the head

    // coordinates of top-left corner of bounding box

    int x = r.nextInt(w - SIZE);
    int y = r.nextInt(h - SIZE);

    Ellipse2D.Double head = new Ellipse2D.Double(x, y, SIZE, SIZE);

    kevin.setColor(Color.YELLOW);
    kevin.fill(head);
    }


}

Edited 3 Years Ago by Dani: Formatting fixed

sorry, but using the codes [], <> in places that i am not sure of, will probably result in numerous e-mails which codes are not in proper places becuse I am not sure of where they all go, wasting my time and your.

kevin

Don't know why you are having a problem. 95% of the other posters are able to figure it out. Do you have an older friend you could ask about how to do it?

nope, guess I am the 5%, unless you are standing next to me, I see a tutor which which has helped immensely to allow me to do it with explantion.

tecaher has lost several students, going to fast esp with 50 students

It's easy. Just highlight all your code (click once before the fist line, hold down the shift key while clicking once after the last line) and click where it says CODE in square brackets at the top of the message box. Try it!

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;
import javax.swing.JPanel;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.util.Random;
import java.awt.geom.Arc2D;
import java.awt.BasicStroke;

/**
 * A component that draws a Cat Face.
 *@Kevin Bennett
*/

public class CatComponentalt extends JComponent

{
	public void paintComponent(Graphics g)
	
	{
	
	// Recover Graphics2D
	Graphics2D g2 = (Graphics2D) g;

	int w = getWidth();
	int h = getHeight();
	int size = Math.min(w, h);

	// translate the origin to the center of the component.
	g2.translate(w / 2, h / 2);

	int radius1 = - (int) (2.0 / 11.0 * size);

	Rectangle r1 = new Rectangle(w, h);
	g2.setColor(Color.YELLOW);
	g2.fill(r1);

	g2.setColor(Color.BLACK);

	// Draw the cat head.
	//Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
	//g2.translate(200, 200);

	
	g2.draw(radius1);

	// Draw the cat head.
	//Ellipse2D.Double head = new Ellipse2D.Double(5, 10, 150, 150);
	//g2.translate(200, 200);

	
		 		

	// Draw the eye number 1.
	
	
	Ellipse2D.Double eye1 = new Ellipse2D.Double(50, 35, 20, 20);
	g2.draw(eye1);
	g2.setColor(Color.BLUE);
	g2.fill(eye1);
	//Line2D.Double eye1 = new Line2D.Double(25, 70, 45, 90);
	//g2.draw(eye1);

	// Draw the eye number 2.
	Ellipse2D.Double eye2 = new Ellipse2D.Double(90, 35, 20, 20);
	g2.draw(eye2);
	g2.setColor(Color.BLUE);
	g2.fill(eye2);
	//Line2D.Double eye2 = new Line2D.Double(85, 70, 65, 90);
	//g2.draw(eye2);
  
	// Draw the mouth
	Arc2D.Double mouth = new Arc2D.Double(65, 110, 60, 60, 90, 90, Arc2D.OPEN);
	g2.draw(mouth);
	
	// Draw the Whiskers 1.
	Line2D.Double whisker1 = new Line2D.Double(0, 95, 65, 110);
	g2.setStroke(new BasicStroke(2));
	g2.draw(whisker1);
	
	// Draw the Whiskers 2.
	Line2D.Double whisker2 = new Line2D.Double(0, 110, 65, 110);
	g2.setStroke(new BasicStroke(2));
	g2.draw(whisker2);

	// Draw the Whiskers 3.
	Line2D.Double whisker3 = new Line2D.Double(0, 125, 65, 110);
	g2.setStroke(new BasicStroke(2));
	g2.draw(whisker3);

	// Draw the Whiskers 4.
	Line2D.Double whisker4 = new Line2D.Double(95, 110, 160, 95);
	g2.draw(whisker4);

	// Draw the Whiskers 5.
	Line2D.Double whisker5 = new Line2D.Double(95, 110, 160, 110);
	g2.draw(whisker5);

	// Draw the Whiskers 6.
	Line2D.Double whisker6 = new Line2D.Double(95, 110, 160, 125);
	g2.draw(whisker6);

	// Draw the Ear 1a
	Line2D.Double ear1 = new Line2D.Double(5, 10, 30, 25);
	g2.draw(ear1);

	// Draw the Ear 1b
	Line2D.Double ear2 = new Line2D.Double(5, 10, 20, 40);
	g2.draw(ear2);

	// Draw the Ear 2a
	Line2D.Double ear3 = new Line2D.Double(125, 25, 155, 15);
	g2.draw(ear3);

	// Draw the Ear 2b
	Line2D.Double ear4 = new Line2D.Double(140, 40, 155, 15);
	g2.draw(ear4);

	// Draw the greeting
	g2.setColor(Color.BLUE);
	g2.drawString("Hello, Drew", 45, 175);
	g2.drawString("Hello, Drew", 45, 200);
	
	}
	
	
	private void drawRandomCircle(Graphics2D kevin)
	{
		int w = getWidth();
		int h = getHeight();

	//Create the head in a random position
	Random r = new Random();
	final int SIZE = 150;  //width and height of the head

	// coordinates of top-left corner of bounding box

	int x = r.nextInt(w - SIZE);
	int y = r.nextInt(h - SIZE);
	
	Ellipse2D.Double head = new Ellipse2D.Double(x, y, SIZE, SIZE);

	kevin.setColor(Color.YELLOW);
	kevin.fill(head);
	}
	
	
}

thanks for being persistent, I was frustrated unemployed and trying to better myself.

anyway, two files which CatComponentalt is tha main body,(CatFrame is Run program)

Got program to display cat face, was wanting to add criteria to able the cat to resize with the frame resizing, (original program was named CatComponent) change name to add additional fun

kevin

wanting to add criteria to able the cat to resize

That sounds like artistic, algorithmic stuff: redraw a complicate figure at a different scale.
If you have an algorithm, then we can help you write the java code for it.

wanting to add criteria to able the cat to resize

That sounds like artistic, algorithmic stuff: redraw a complicate figure at a different scale.
If you have an algorithm, then we can help you write the java code for it.

nope, we did in class with another program with just a colored circle, (using private) but not with all the extras, ears, mouth and such

If you have some suggestions, without getting to complicated, first semester java,

Walk me thru it, via phone 570-765-1055

kevin

The only suggestion I have is to replace all the hardcoded x,y values in the paint method and replace them with variables whose values can be computed.

tutor suggested using Double.math.min(w,h);
using each whicker/ear
like line2D(0 * min)/600
line2D(95 * min)600

600 being the size of the window and as window resizes w and h are recalculated.

setting up is my challenge

This article has been dead for over six months. Start a new discussion instead.