



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




g.drawRect(20,20,20,20);
g.drawRect( mx , my ,20,20;
import javax.swing.; import java.awt.;
// extending class from JPanel public class MyPanel extends JPanel {
// variables used to draw rectangles at different //locations int mX = 20; int mY = 20;
// overriding paintComponent method public void paintComponent(Graphics g){
// erasing behaviour โ this will clear all the // previous painting super.paintComponent(g);
// Down casting Graphics object to Graphics2D Graphics2D g2 = (Graphics2D)g;
// changing the color to blue g2.setColor(Color.blue);
// drawing filled oval with color i.e. blue // using instance variables g2.fillRect( mX , mY ,20,20);
} // end paintComponent
} // end MyPanel class
// call to repaint causes rectangle to be drawn on // new location p.repaint() ;
} // end mouseDragged
} // end Handler class
// main method public static void main(String args[ ]){ Test t = new Test();
}
} // end MyPanel class
import javax.swing.; import java.awt.;
// extending class from JPanel public class MyPanel extends JPanel {
// variables used to draw oval at different locations int mX = 200; int mY = 0;
// overriding paintComponent method public void paintComponent(Graphics g){
// erasing behaviour โ this will clear all the // previous painting super.paintComponent(g);
// Down casting Graphics object to Graphics2D Graphics2D g2 = (Graphics2D)g;
// changing the color to blue g2.setColor(Color.blue);
// drawing filled oval with blue color // using instance variables g2.fillOval( mX,mY ,20,20);
}// end paintComponent
} // end MyPanel class
// if ball reached to maximum height of frame // minus 40 then change the Y-direction of ball if (f.getHeight()-40 == p.mY)
y = -3;
// if ball reached to min. of width of frame, // change the X-direction of ball if (p.mX == 0 )
x = 5;
// if ball reached to min. of height of frame, // change the Y-direction of ball
if (p.mY == 0 )
y = 3;
// Assign x,y direction to MyPanelโs mX & mY p.mX += x; p.mY += y;
// call to repaint() method so that ball is drawn on // new locations p.repaint();
} // end actionPerformed() method
// main method public static void main(String args[ ]){ AnimTest at = new AnimTest(); }
} // end AnimTest class