Results 1 to 2 of 2
  1. #1
    dharav is offline Member
    Join Date
    Dec 2011
    Posts
    28
    Rep Power
    0

    Question Objects and Classes

    I have a task here, where i created a small movement of pokemon, now i want the full action to repeat it self once it completes. Eg.it stops, when the pikachu shows up, i want it to go back to the starting position and re do the same thing and keep looping it. I tried at it for a while now but noT quite getting it yet. Plz can anyone help me. Sorry about the flickering, i dont know about buffering yet.
    THIS IS THE PIC USED IN THE PROGRAM!
    Objects and Classes-landscape.png
    Code
    Java Code:
    package movingTask;
    import java.awt.*;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.geom.*;
    
    import javax.swing.*;
    
    public class movingTask extends JFrame implements ActionListener {
    	Image one;
    	int xmove=697,xmove2=700,xmove3=700,xmove4=789,xmove5=794;
    	
    	int x=106,y=793,x2=135,y2=700;
    	Timer watch;
    	int showing=0; 
    	float lx=106,ly=793;
    	public void paint (Graphics g){
    
    		Graphics2D ga = (Graphics2D)g;
    
    
    		ImageIcon gp = new ImageIcon(this.getClass().getResource("landscape.png"));
    		one= gp.getImage();
    		ga.drawImage(one,0,0,null);
    		Color yellow = new Color (254,242,000);
    		Color gold = new Color (195,171,47);
    		Color orange = new Color (255,140,00);
    		Color red=new Color(255,0,0);
    
    
    		if(showing==1)
    		{
    			// laser
    			
    			
    			
    			
    			Line2D.Float laserbeam = new Line2D.Float (x,y,x2,y2);
    			ga.setStroke(new BasicStroke(20));
    			ga.setColor(red);
    			ga.draw(laserbeam);
    			ga.setStroke(new BasicStroke(1));
    			
    			
    			
    			
    			if(lx>=300)
    			{
    				
    				
    				//Ear PATH COLOURED
    
    				GeneralPath ear = new GeneralPath();
    				ear.moveTo(154, 177);
    				ear.quadTo(128, 153, 85, 88);
    				ear.quadTo(140, 112, 183, 145);
    				ear.quadTo(226, 136, 275, 138);
    				ear.quadTo(323, 123, 396, 125);
    				ear.quadTo(349, 157, 306, 170);
    				ear.closePath();
    				ga.setPaint(yellow);
    				ga.fill(ear);
    
    				//Tail
    				GeneralPath tailpath = new GeneralPath();
    				tailpath.moveTo(320,374);
    				tailpath.quadTo(320,374,347,363);
    				tailpath.quadTo(347,363,343,351);
    				tailpath.quadTo(343,351,369,337);
    				tailpath.quadTo(369,337,357,313);
    				tailpath.quadTo(357,313,397,286);
    				tailpath.quadTo(397,286,384,264);
    				tailpath.quadTo(384,264,436,218);
    				tailpath.quadTo(436,218,426,140);
    				tailpath.quadTo(426,140,327,253);
    				tailpath.quadTo(327,253,355,284);
    				tailpath.quadTo(355,284,326,309);
    				tailpath.quadTo(326,309,343,324);
    				tailpath.quadTo(343,324,315,348);
    				tailpath.quadTo(315,348,320,374);
    				tailpath.closePath();
    				ga.setPaint(yellow);
    				ga.fill(tailpath); 
    
    
    				GeneralPath redpath = new GeneralPath();
    				redpath.moveTo(320,374);
    				redpath.quadTo(320,374,347,363);
    				redpath.quadTo(347,363,343,351);
    				redpath.quadTo(343,351,369,337);
    				redpath.quadTo(369,337,357,313);
    				redpath.quadTo(353,308, 350,312 );
    				redpath.quadTo(326,309,343,324);
    				redpath.quadTo(343,324,315,348);
    				redpath.closePath();
    				ga.setPaint(Color.red);
    				ga.fill(redpath); 
    
    				Line2D.Float tail1 = new Line2D.Float (320,374,347,363);
    				ga.setColor(Color.black);
    				ga.setStroke(new BasicStroke(2));
    				ga.draw(tail1);
    
    				Line2D.Float tail2 = new Line2D.Float (347,363,343,351);
    				ga.setColor(Color.black);
    				ga.draw(tail2);
    
    				Line2D.Float tail3 = new Line2D.Float (343,351,369,337);
    				ga.setColor(Color.black);
    				ga.draw(tail3);
    
    				Line2D.Float tail4 = new Line2D.Float (369,337,357,313);
    				ga.setColor(Color.black);
    				ga.draw(tail4);
    
    				Line2D.Float tail5 = new Line2D.Float (357,313,397,286);
    				ga.setColor(Color.black);
    				ga.draw(tail5);
    
    				Line2D.Float tail6 = new Line2D.Float (397,286,384,264);
    				ga.setColor(Color.black);
    				ga.draw(tail6);
    
    				Line2D.Float tail7 = new Line2D.Float (384,264,436,218);
    				ga.setColor(Color.black);
    				ga.draw(tail7);
    
    				Line2D.Float tail8 = new Line2D.Float (436,218,426,140);
    				ga.setColor(Color.black);
    				ga.draw(tail8);
    
    				Line2D.Float tail9 = new Line2D.Float (426,140,327,253);
    				ga.setColor(Color.black);
    				ga.draw(tail9);
    
    				Line2D.Float tail10 = new Line2D.Float (327,253,355,284);
    				ga.setColor(Color.black);
    				ga.draw(tail10);
    
    				Line2D.Float tail11 = new Line2D.Float (355,284,326,309);
    				ga.setColor(Color.black);
    				ga.draw(tail11);
    
    				Line2D.Float tail12 = new Line2D.Float (326,309,343,324);
    				ga.setColor(Color.black);
    				ga.draw(tail12);
    
    				Line2D.Float tail13 = new Line2D.Float (343,324,315,348);
    				ga.setColor(Color.black);
    				ga.draw(tail13);
    
    				Line2D.Float tail14 = new Line2D.Float (315,348,320,374);
    				ga.setColor(Color.black);
    				ga.draw(tail14);
    				ga.setPaint(yellow);
    				ga.setStroke(new BasicStroke(1));
    
    
    
    				//Head PATH COLOURED
    				GeneralPath head = new GeneralPath();
    				head.moveTo(159,278);
    				head.quadTo(139,247, 147,220);
    				head.quadTo(150,170, 168,160);
    				head.quadTo(183,145, 226,138);
    				head.quadTo(275,138,295,159);
    				head.quadTo(310,170, 313,210);
    				head.quadTo(325,250, 305,274);
    				ga.setPaint(yellow);
    				ga.fill(head);
    
    				//Body PATH COLOURED
    				GeneralPath body = new GeneralPath();
    				body.moveTo(312,264);
    				body.quadTo(333,349, 315,400);
    				body.quadTo(301,417,291,414);
    				body.quadTo(205,423, 245,417);
    				body.quadTo(284,417, 195,422);
    				body.quadTo(178,421, 170,410);
    				body.quadTo(140,358, 152,266);
    				body.closePath();
    				ga.setPaint(yellow);
    				ga.fill(body);
    
    				//RIGHT Toes/Feet PATH COLOURED
    				GeneralPath rfeet = new GeneralPath();
    				rfeet.moveTo(275, 415);
    				rfeet.quadTo(278, 419,302, 442);
    				rfeet.quadTo(302, 442,300, 431);
    				rfeet.quadTo(300, 431,307, 437);
    				rfeet.quadTo(307, 437,303, 429);
    				rfeet.quadTo(303, 429,311, 432);
    				rfeet.quadTo(311, 432,298, 413);
    				rfeet.closePath();
    				ga.setPaint(yellow);
    				ga.fill(rfeet);
    
    				//LEFT Toes/Feet PATH COLOURED
    				GeneralPath lfeet = new GeneralPath();
    				lfeet.moveTo(188,422);
    				lfeet.quadTo(188,422,174, 441);
    				lfeet.quadTo(174,441,188, 435);
    				lfeet.quadTo(188,435,184, 443);
    				lfeet.quadTo(184, 443,194, 436);
    				lfeet.quadTo(194, 436,191, 447);
    				lfeet.quadTo(191, 447,212, 422);
    				lfeet.closePath();
    				ga.setPaint(yellow);
    				ga.fill(lfeet);
    
    				//Ear1
    				QuadCurve2D earline1 = new QuadCurve2D.Float();
    				earline1.setCurve(85,88,140,112,183,145);
    				ga.setColor(Color.black);
    				ga.draw(earline1);
    
    				QuadCurve2D earline2 = new QuadCurve2D.Float();
    				earline2.setCurve(85,88,128,153,154,177);
    				ga.setColor(Color.black);
    				ga.draw(earline2);
    
    
    
    
    
    				//Ear2
    				QuadCurve2D ear2line1 = new QuadCurve2D.Float();
    				ear2line1.setCurve(275,138,323,123,396,125);
    				ga.setColor(Color.black);
    				ga.draw(ear2line1);
    
    				QuadCurve2D ear2line2 = new QuadCurve2D.Float();
    				ear2line2.setCurve(306,170,349,157,396,125);
    				ga.setColor(Color.black);
    				ga.draw(ear2line2);
    
    
    				//Head
    				QuadCurve2D topline = new QuadCurve2D.Float();
    				topline.setCurve(183,145,226,136,275,138);
    				ga.setColor(Color.black);
    				ga.draw(topline);
    
    				//my pov left cheek
    				QuadCurve2D leftcheekline1 = new QuadCurve2D.Float();
    				leftcheekline1.setCurve(168,160,150,170,147,220);
    				ga.setColor(Color.black);
    				ga.draw(leftcheekline1);
    
    				QuadCurve2D leftcheekline2 = new QuadCurve2D.Float();
    				leftcheekline2.setCurve(147,220,139,247,159,278);
    				ga.setColor(Color.black);
    				ga.draw(leftcheekline2);
    
    				//my pov right cheek
    				QuadCurve2D rightcheekline1 = new QuadCurve2D.Float();
    				rightcheekline1.setCurve(295,159,310,170,313,210);
    				ga.setColor(Color.black);
    				ga.draw(rightcheekline1);
    
    				QuadCurve2D rightcheekline2 = new QuadCurve2D.Float();
    				rightcheekline2.setCurve(313,210,325,250,305,274);
    				ga.setColor(Color.black);
    				ga.draw(rightcheekline2);
    
    				//Right body
    				QuadCurve2D rightbody = new QuadCurve2D.Float();
    				rightbody.setCurve(312,264,333,349,315,400);
    				ga.setColor(Color.black);
    				ga.draw(rightbody);
    
    				QuadCurve2D rightbody2 = new QuadCurve2D.Float();
    				rightbody2.setCurve(315,400,301,417,291,414);
    				ga.setColor(Color.black);
    				ga.draw(rightbody2);
    
    				//Left body
    				QuadCurve2D leftbody = new QuadCurve2D.Float();
    				leftbody.setCurve(152,266,140,358,170,410);
    				ga.setColor(Color.black);
    				ga.draw(leftbody);
    
    				QuadCurve2D leftbody2 = new QuadCurve2D.Float();
    				leftbody2.setCurve(170,410,178,421,195,422);
    				ga.setColor(Color.black);
    				ga.draw(leftbody2);
    
    				//pelvis
    				QuadCurve2D pelvis = new QuadCurve2D.Float();
    				pelvis.setCurve(205,423,245,417,284,417);
    				ga.setColor(Color.black);
    				ga.draw(pelvis);
    
    				//characters pov right leg leg
    				Line2D.Float rightleg1 = new Line2D.Float (188,422,174, 441);
    				ga.setColor(Color.black);
    				ga.draw(rightleg1);
    
    				Line2D.Float rightleg2 = new Line2D.Float (174,441,188, 435);
    				ga.setColor(Color.black);
    				ga.draw(rightleg2);
    
    				Line2D.Float rightleg3 = new Line2D.Float (188,435,184, 443);
    				ga.setColor(Color.black);
    				ga.draw(rightleg3);
    
    				Line2D.Float rightleg4 = new Line2D.Float (184, 443,194, 436);
    				ga.setColor(Color.black);
    				ga.draw(rightleg4);
    
    				Line2D.Float rightleg6 = new Line2D.Float (194, 436,191, 447);
    				ga.setColor(Color.black);
    				ga.draw(rightleg6);
    
    				Line2D.Float rightleg7 = new Line2D.Float (191, 447,212, 422);
    				ga.setColor(Color.black);
    				ga.draw(rightleg7);
    
    				//characters pov lefts leg
    				Line2D.Float leftleg1 = new Line2D.Float (278, 419,302, 442);
    				ga.setColor(Color.black);
    				ga.draw(leftleg1);
    
    				Line2D.Float leftleg2 = new Line2D.Float (302, 442,300, 431);
    				ga.setColor(Color.black);
    				ga.draw(leftleg2);
    
    				Line2D.Float leftleg3 = new Line2D.Float (300, 431,307, 437);
    				ga.setColor(Color.black);
    				ga.draw(leftleg3);
    
    				Line2D.Float leftleg4 = new Line2D.Float (307, 437,303, 429);
    				ga.setColor(Color.black);
    				ga.draw(leftleg4);
    
    				Line2D.Float leftleg5 = new Line2D.Float (303, 429,311, 432);
    				ga.setColor(Color.black);
    				ga.draw(leftleg5);
    
    				Line2D.Float leftleg6 = new Line2D.Float (311, 432,298,413);
    				ga.setColor(Color.black);
    				ga.draw(leftleg6);
    
    				//left hand
    				ga.setStroke(new BasicStroke(2));
    				QuadCurve2D lefthand1 = new QuadCurve2D.Float();
    				lefthand1.setCurve(180,300,193,327,206,358);
    				ga.setColor(Color.black);
    				ga.draw(lefthand1);
    
    				Line2D.Float lefthand2 = new Line2D.Float (206, 358,200,359);
    				ga.setColor(Color.black);
    				ga.draw(lefthand2);
    
    				Line2D.Float lefthand3 = new Line2D.Float (200, 359,204,365);
    				ga.setColor(Color.black);
    				ga.draw(lefthand3);
    
    				Line2D.Float lefthand4 = new Line2D.Float (204, 365,197,364);
    				ga.setColor(Color.black);
    				ga.draw(lefthand4);
    
    				Line2D.Float lefthand5 = new Line2D.Float (197, 364,198,369);
    				ga.setColor(Color.black);
    				ga.draw(lefthand5);
    
    				Line2D.Float lefthand6 = new Line2D.Float (198, 369,192,367);
    				ga.setColor(Color.black);
    				ga.draw(lefthand6);
    
    				Line2D.Float lefthand7 = new Line2D.Float (192, 367,193,375);
    				ga.setColor(Color.black);
    				ga.draw(lefthand7);
    
    				QuadCurve2D lefthand8 = new QuadCurve2D.Float();
    				lefthand8.setCurve(157,343,171,360,193,375);
    				ga.setColor(Color.black);
    				ga.draw(lefthand8);
    
    				//right Hand
    				ga.setStroke(new BasicStroke(2));
    				QuadCurve2D righthand1 = new QuadCurve2D.Float();
    				righthand1.setCurve(285,296,277,324,271,359);
    				ga.setColor(Color.black);
    				ga.draw(righthand1);
    
    				Line2D.Float righthand2 = new Line2D.Float (271,359,277,356);
    				ga.setColor(Color.black);
    				ga.draw(righthand2);
    
    				Line2D.Float righthand3 = new Line2D.Float (277,356,278,362);
    				ga.setColor(Color.black);
    				ga.draw(righthand3);
    
    				Line2D.Float righthand4 = new Line2D.Float (278,362,284,359);
    				ga.setColor(Color.black);
    				ga.draw(righthand4);
    
    				Line2D.Float righthand5 = new Line2D.Float (284,359,285,366);
    				ga.setColor(Color.black);
    				ga.draw(righthand5);
    
    				Line2D.Float righthand6 = new Line2D.Float (285,366,291,361);
    				ga.setColor(Color.black);
    				ga.draw(righthand6);
    
    				Line2D.Float righthand7 = new Line2D.Float (291,361,291,367);
    				ga.setColor(Color.black);
    				ga.draw(righthand7);
    
    				QuadCurve2D righthand8 = new QuadCurve2D.Float();
    				righthand8.setCurve(291,367,311,340,315,320);
    				ga.setColor(Color.black);
    				ga.draw(righthand8);
    				ga.setStroke(new BasicStroke(1));
    				// Left Eye
    				Ellipse2D eye1 = new Ellipse2D.Double(165,191,33,33);
    				ga.draw(eye1);
    				ga.setPaint(Color.black);
    				ga.fill(eye1);
    
    				Ellipse2D puple1 = new Ellipse2D.Double(171,195,14,14);
    				ga.draw(puple1);
    				ga.setPaint(Color.white);
    				ga.fill(puple1);
    
    				//Right Eye
    				Ellipse2D eye2 = new Ellipse2D.Double(266,185,33,33);
    				ga.draw(eye2);
    				ga.setPaint(Color.black);
    				ga.fill(eye2);
    
    				Ellipse2D puple2 = new Ellipse2D.Double(272,191,14,14);
    				ga.draw(puple2);
    				ga.setPaint(Color.white);
    				ga.fill(puple2);
    
    				//Blush left
    				Ellipse2D blush1 = new Ellipse2D.Double (152,238,27,27);
    				ga.draw(blush1);
    				ga.setPaint(Color.red);
    				ga.fill(blush1);
    				ga.setStroke(new BasicStroke(2));
    				ga.setPaint(Color.black);
    				ga.draw(blush1);
    
    				//Blush right
    				Ellipse2D blush2 = new Ellipse2D.Double (285,229,27,27);
    				ga.draw(blush2);
    				ga.setPaint(Color.red);
    				ga.fill(blush2);
    				ga.setStroke(new BasicStroke(2));
    				ga.setPaint(Color.black);
    				ga.draw(blush2);
    				ga.setStroke(new BasicStroke(1));
    
    				//Nose
    				Line2D.Float nose1 = new Line2D.Float (226,228,245,226);
    				ga.setColor(Color.black);
    				ga.draw(nose1);
    
    				Line2D.Float nose2 = new Line2D.Float (245,226,236,233);
    				ga.setColor(Color.black);
    				ga.draw(nose2);
    
    				Line2D.Float nose3 = new Line2D.Float (236,233,226,228);
    				ga.setColor(Color.black);
    				ga.draw(nose3);
    
    				GeneralPath nosepath = new GeneralPath();
    				nosepath.moveTo(226,228);
    				nosepath.quadTo(226,228,245,226);
    				nosepath.quadTo(245,226,236,233);
    				nosepath.quadTo(236,233,226,228);
    				nosepath.closePath();
    				ga.setPaint(Color.black);
    				ga.fill(nosepath);
    
    				//Mouth
    				QuadCurve2D mouth1 = new QuadCurve2D.Float();
    				mouth1.setCurve(210,252,217,258,238,248);
    				ga.setColor(Color.black);
    				ga.draw(mouth1);
    
    				QuadCurve2D mouth2 = new QuadCurve2D.Float();
    				mouth2.setCurve(238,248,259,256,266,250);
    				ga.setColor(Color.black);
    				ga.draw(mouth2);
    
    				//Neck
    				Line2D.Float neck1 = new Line2D.Float (216,296,253,296);
    				ga.setColor(gold);
    				ga.draw(neck1);
    
    				QuadCurve2D neck2 = new QuadCurve2D.Float();
    				neck2.setCurve(253,296,253,300,235,309);
    				ga.setColor(gold);
    				ga.draw(neck2);
    
    				QuadCurve2D neck3 = new QuadCurve2D.Float();
    				neck3.setCurve(235,309,216,300,216,296);
    				ga.setColor(gold);
    				ga.draw(neck3);
    
    				GeneralPath neck = new GeneralPath();
    				neck.moveTo(216,296);
    				neck.quadTo(216,296,253,296);
    				neck.quadTo(253,300,235,309);
    				neck.quadTo(216,300,216,296);
    				neck.closePath();
    				ga.setPaint(gold);
    				ga.fill(neck);
    			}
    		}
    
    		//Pokeball
    		Ellipse2D blackback = new Ellipse2D.Double (xmove,697,205,205);
    		ga.draw(blackback);
    		ga.setPaint(Color.black);
    		ga.fill(blackback);
    		ga.draw(blackback);
    
    
    		g.setColor(Color.white);
    		g.fillOval(xmove2,700,200,200);
    		g.setColor(Color.red); 
    		g.fillArc(xmove2,700,200,200, 0, 180);
    		g.setColor(Color.black);
    
    		Rectangle2D.Double black = new Rectangle2D.Double (xmove3,795,200,5); 
    		ga.setPaint(Color.black);
    		ga.fill(black);
    		ga.draw (black);
    
    		Ellipse2D small = new Ellipse2D.Double (xmove4,789,20,20);
    		ga.setPaint(Color.black);
    		ga.fill(small);
    		ga.draw(small);
    
    		Ellipse2D smaller = new Ellipse2D.Double (xmove5,793,10,10);
    		ga.setPaint(Color.white);
    		ga.fill(smaller);
    		ga.draw(smaller);
    
    
    	}
    
    	public movingTask(){
    		watch=new Timer(2,this);
    		watch.start();
    	}
    
    	public static void main(String args []){
    	
    			JFrame frame = new movingTask();
    			frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			frame.setSize(1080,979);
    			frame.setVisible(true);
    	
    	}
    
    	@Override
    	public void actionPerformed(ActionEvent e) {
    	
    			xmove=xmove-10;
    			xmove2=xmove2-10;
    			xmove3=xmove3-10;
    			xmove4=xmove4-10;
    			xmove5=xmove5-10;
    
    
    
    			if (xmove<=9&&xmove2<=12&&xmove3<=12&&xmove4<=101&&xmove5<=106)
    			{
    				xmove=9;
    				xmove2=12;
    				xmove3=12;
    				xmove4=101;
    				xmove5=106;
    
    				showing=1;
    			}
    
    
    			if(x<=206&&y<=993)
    			{
    
    				x=x+10;
    				y=y-30;
    				x2=x2+10;
    				y2=y2-30;
    			}
    
    
    			else if(lx<300)
    			{
    				lx++;
    				ly=ly-2.0f;
    
    				x=106;
    				y=793;
    				x2=135;
    				y2=700;
    			}
    
    			repaint();
    		}
    	}
    Last edited by dharav; 10-17-2012 at 05:10 AM.

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default Re: Objects and Classes

    So if you model the series of movements as some form of action object - i.e. an instance of said object holds the specific set of movements you want to make, then I don't see why you couldn't add methods to reset position and just repeatedly call the movement routine as needed.

Similar Threads

  1. OOP Objects and Classes help
    By richlan in forum New To Java
    Replies: 5
    Last Post: 03-05-2011, 06:50 PM
  2. Classes and Objects Help
    By collin389 in forum New To Java
    Replies: 1
    Last Post: 12-14-2009, 01:44 AM
  3. classes as objects
    By kroiz in forum New To Java
    Replies: 4
    Last Post: 07-25-2009, 06:22 AM
  4. help with passing objects between classes
    By aruna1 in forum New To Java
    Replies: 7
    Last Post: 03-22-2009, 03:41 PM
  5. Objects and Classes
    By Aleve in forum New To Java
    Replies: 8
    Last Post: 12-31-2007, 09:05 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •