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 3 of 3

Thread: OOP graphics program

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default OOP graphics program

     import java.awt.*;
     
    class Snowperson
    {
    	int xCoor;
    	int yCoor;
    	int width;
    	int height;
     
    	public Snowperson()
    	{
    		xCoor = 0;
    		yCoor = 0;
    		width = 0;
    		height = 0;
    	}//end default
    	public Snowperson(int x, int y, int wid, int hght)
    	{
    		xCoor = x;
    		yCoor = y;
    		width = wid;
    		height = hght;
     
    	}//end default	
    	public int getX()
    	{
    	   return xCoor;
    	}
    	public int getY()
    	{
    	   return yCoor;
    	}
    	public int getH()
    	{
    	   return height;
    	}
    	public int getW()
    	{
    	   return width;
    	}
    	public void drawSnowBottom(Graphics g)
    	{
    		g.fillOval(xCoor,yCoor,width, height);
    	}//end draw snow bottom
    	public void drawSnowMiddle(Graphics g)
    	{
    	 g.fillOval(xCoor+19,yCoor-128,width-49,height-40);
    	}//end draw snow middle
    	public void drawSnowTop(Graphics g)
    	{
    	  g.fillOval(xCoor+50, yCoor-211,width/2, height/2);
    	}//end draw snow top
     
    		public void addNose(Graphics g)
    	{	
    		g.setColor(Color.orange);
    		Polygon nose = new Polygon();
    		nose.addPoint(xCoor+100,yCoor-160);
    		nose.addPoint(xCoor+100,yCoor-150);
    		nose.addPoint(xCoor+125,yCoor-155);
    		g.fillPolygon(nose);
    	}	
    		public void addArms(Graphics g)
    		{
    			g.setColor(Color.black);
    			g.drawLine(xCoor+25,yCoor-75,xCoor-10,yCoor+10);
    			g.drawLine(xCoor+165,yCoor-75,xCoor+190,yCoor+10);
    		}	
     
    		public void addEyes(Graphics g)
    		{
    			g.setColor(Color.black);
    			g.fillOval(xCoor+80,yCoor-180,10,10);
    			g.fillOval(xCoor+110,yCoor-180,10,10);
    		}	
     
    		public void addButtons(Graphics g)
    		{
    			g.setColor(Color.black);	
    			g.fillRect(xCoor+90,yCoor-50,10,10);
    			g.fillRect(xCoor+90,yCoor,10,10);
    			g.fillRect(xCoor+90,yCoor+50,10,10);
    		}	
     
    	private void addHat(Graphics g)
    		{
    			g.setColor(Color.red);
    			Polygon hat = new Polygon();
    			hat.addPoint(xCoor+65,yCoor-198);
    			hat.addPoint(xCoor+135,yCoor-198);
    			hat.addPoint(xCoor+115,yCoor-238);
    			hat.addPoint(xCoor+165,yCoor-258);
    			hat.addPoint(xCoor+85,yCoor-248);
    			g.fillPolygon(hat);
    			g.setColor(Color.white);
    			g.fillOval(xCoor+165,yCoor-263,10,10);
     
    		}
     
    	public void drawSnowperson(Graphics g)
    	{
    		g.setColor(Color.white);
    		drawSnowBottom(g);
    		drawSnowMiddle(g);
    		addHat(g);
    		drawSnowTop(g);
    		g.setColor(Color.orange);
    		addNose(g);
    		addArms(g);
    		addEyes(g);
    		addButtons(g);
     
    		Graphics2D g2 = (Graphics2D)g;
    		Font font = new Font("Serif", Font.PLAIN,50);
    		g2.setFont(font);
     
    		g2.drawString("Happy Holidays!", 100, 150);
    	}//end draw snow person	
    }//end class SnowpersonAG
     
    class skiir extends Snowperson
    {
    	private Color myPink;
    	private Color myGBlue;
    	private Color myOrange;
    	private Color mahogany;
    	private Color myGrey;
     
    	public skiir() 
    	{
    		xCoor = 0;
    		yCoor = 0;
    		width = 0;
    		height = 0;
    	}//end default
     
    	public skiir(int x, int y, int wid, int hght)
     
    	{
    		super (x,y,wid,hght);
     
    		Color myGrey= new Color(162,162,162);
    		Color myPink= new Color(255,130,182);
    		Color myGBlue = new Color(69,78,97);
    		Color myOrange = new Color(255,128,3);
    		Color mahogany = new Color(145,20,0); 
    	{
    		xCoor = x;
    		yCoor = y;
    		width = wid;
    		height = hght;
     
    	}//end default
    	}
    	public int getX()
    	{
    	   return xCoor;
    	}
    	public int getY()
    	{
    	   return yCoor;
    	}
    	public int getH()
    	{
    	   return height;
    	}
    	public int getW()
    	{
    	   return width;
    	}
     
     
    	public void drawskis(Graphics g)
    		{
    		Polygon ski1= new Polygon();
    		g.setColor(Color.red);
    		ski1.addPoint(xCoor,yCoor);
    		ski1.addPoint(xCoor+20,yCoor-10);
    		ski1.addPoint(xCoor+120,yCoor-120);
    		ski1.addPoint(xCoor+120,yCoor-135);
    		ski1.addPoint(xCoor+30,yCoor-130);
    		ski1.addPoint(xCoor,yCoor-20);
    		g.fillPolygon(ski1);
    		}	 
     
    	public void drawSnowperson(Graphics g)
    	{
    		g.setColor(Color.white);
    		drawSnowBottom(g);
    		drawSnowMiddle(g);
    		drawSnowTop(g);
    		g.setColor(Color.orange);
    		addNose(g);
    		addArms(g);
    		addEyes(g);
    		addButtons(g);
    		g.setColor(Color.red);
    		drawskis(g);
     
    	}		
     
     
    }



    i want the hat to only show up ont the first snowperson and the skis to show up on the second
    Last edited by conmanva; January 2nd, 2011 at 03:03 PM.


  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: OOP graphics program

    What seems to be the problem?

    Where are you drawing this at?

    You don't have a Window and you don't extend one either.

  3. #3
    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: OOP graphics program

    I can't get it to draw anything.

    import javax.swing.JFrame;
     
    import java.awt.*;
     
    class SnowPerson extends JFrame
    {
        int xCoor;
        int yCoor;
        int width;
        int height;
     
        public SnowPerson() 
        {
     
            xCoor = 0;
            yCoor = 0;
            width = 0;
            height = 0;
        }//end default
        public SnowPerson(int x, int y, int wid, int hght)
        {
     
            xCoor = x;
            yCoor = y;
            width = wid;
            height = hght;
     
        }//end default 
     
        public void setX(int x)
        {
        	this.xCoor = x;
        }
        public void setY(int y)
        {
        	this.yCoor = y;
        }
     
     
     
        public int getX()
        {
           return xCoor;
        }
        public int getY()
        {
           return yCoor;
        }
        public int getH()
        {
           return height;
        }
        public int getW()
        {
           return width;
        }
        public void drawSnowBottom(Graphics g)
        {
            g.fillOval(xCoor,yCoor,width, height);
        }//end draw snow bottom
        public void drawSnowMiddle(Graphics g)
        {
         g.fillOval(xCoor+19,yCoor-128,width-49,height-40);
        }//end draw snow middle
        public void drawSnowTop(Graphics g)
        {
          g.fillOval(xCoor+50, yCoor-211,width/2, height/2);
        }//end draw snow top
     
            public void addNose(Graphics g)
        {  
            g.setColor(Color.orange);
            Polygon nose = new Polygon();
            nose.addPoint(xCoor+100,yCoor-160);
            nose.addPoint(xCoor+100,yCoor-150);
            nose.addPoint(xCoor+125,yCoor-155);
            g.fillPolygon(nose);
        }  
            public void addArms(Graphics g)
            {
                g.setColor(Color.black);
                g.drawLine(xCoor+25,yCoor-75,xCoor-10,yCoor+10);
                g.drawLine(xCoor+165,yCoor-75,xCoor+190,yCoor+10);
            }  
     
            public void addEyes(Graphics g)
            {
                g.setColor(Color.black);
                g.fillOval(xCoor+80,yCoor-180,10,10);
                g.fillOval(xCoor+110,yCoor-180,10,10);
            }  
     
            public void addButtons(Graphics g)
            {
                g.setColor(Color.black);   
                g.fillRect(xCoor+90,yCoor-50,10,10);
                g.fillRect(xCoor+90,yCoor,10,10);
                g.fillRect(xCoor+90,yCoor+50,10,10);
            }  
     
        private void addHat(Graphics g)
            {
                g.setColor(Color.red);
                Polygon hat = new Polygon();
                hat.addPoint(xCoor+65,yCoor-198);
                hat.addPoint(xCoor+135,yCoor-198);
                hat.addPoint(xCoor+115,yCoor-238);
                hat.addPoint(xCoor+165,yCoor-258);
                hat.addPoint(xCoor+85,yCoor-248);
                g.fillPolygon(hat);
                g.setColor(Color.white);
                g.fillOval(xCoor+165,yCoor-263,10,10);
     
            }
     
        public void drawSnowperson(Graphics g)
        {
            g.setColor(Color.white);
            drawSnowBottom(g);
            drawSnowMiddle(g);
            addHat(g);
            drawSnowTop(g);
            g.setColor(Color.orange);
            addNose(g);
            addArms(g);
            addEyes(g);
            addButtons(g);
     
            Graphics2D g2 = (Graphics2D)g;
            Font font = new Font("Serif", Font.PLAIN,50);
            g2.setFont(font);
     
            g2.drawString("Happy Holidays!", 100, 150);
        }//end draw snow person
    }//end class SnowpersonAG

    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Polygon;
     
    import javax.swing.JPanel;
     
    class skiir extends SnowPerson
    {
        private Color myPink;
        private Color myGBlue;
        private Color myOrange;
        private Color mahogany;
        private Color myGrey;
     
     
        public skiir()
        {
            xCoor = 0;
            yCoor = 0;
            width = 0;
            height = 0;
     
        }//end default
     
        public skiir(int x, int y, int wid, int hght)
     
        {
            super (x,y,wid,hght);
     
            Color myGrey= new Color(162,162,162);
            Color myPink= new Color(255,130,182);
            Color myGBlue = new Color(69,78,97);
            Color myOrange = new Color(255,128,3);
            Color mahogany = new Color(145,20,0);
     
            xCoor = x;
            yCoor = y;
            width = wid;
            height = hght;
     
     
        }
        public int getX()
        {
           return xCoor;
        }
        public int getY()
        {
           return yCoor;
        }
        public int getH()
        {
           return height;
        }
        public int getW()
        {
           return width;
        }
     
     
        public void drawskis(Graphics g)
            {
            Polygon ski1= new Polygon();
            g.setColor(Color.red);
            ski1.addPoint(xCoor,yCoor);
            ski1.addPoint(xCoor+20,yCoor-10);
            ski1.addPoint(xCoor+120,yCoor-120);
            ski1.addPoint(xCoor+120,yCoor-135);
            ski1.addPoint(xCoor+30,yCoor-130);
            ski1.addPoint(xCoor,yCoor-20);
            g.fillPolygon(ski1);
            }    
     
        public void drawSnowperson(Graphics g)
        {
            g.setColor(Color.white);
            drawSnowBottom(g);
            drawSnowMiddle(g);
            drawSnowTop(g);
            g.setColor(Color.orange);
            addNose(g);
            addArms(g);
            addEyes(g);
            addButtons(g);
            g.setColor(Color.red);
            drawskis(g);
     
        }      
     
       public static void main(String[] args)
       {
    	   SnowPerson p = new SnowPerson(500, 500, 100, 100);
    	   p.setVisible(true);
     
       }
     
    }

Similar Threads

  1. Java Graphics help
    By Stockholm Syndrome in forum AWT / Java Swing
    Replies: 9
    Last Post: November 4th, 2010, 05:23 PM
  2. wrong graphics
    By mingming8888 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 25th, 2010, 09:33 AM
  3. Help about Graphics
    By mamech in forum What's Wrong With My Code?
    Replies: 13
    Last Post: September 9th, 2010, 03:20 PM
  4. Graphics mayhem
    By javapenguin in forum AWT / Java Swing
    Replies: 2
    Last Post: August 9th, 2010, 05:21 PM
  5. graphics in job?
    By SweetyStacey in forum The Cafe
    Replies: 10
    Last Post: May 3rd, 2010, 03:29 PM