














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!















Your Painting Strategy
Last Example Code: MyPanel.java import javax.swing.; import java.awt.; public class MyPanel extends JPanel { public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.drawRect(20,20,20,20); g2.setColor(Color.blue); g2.fillOval(50,50,20,20); g2.drawString("Hello World", 120, 50); } } Docsity.com
Last Example Code: Test.java public static void main(String args[ ]){ Test1 = new Test(); } } // end class
How to Animate
Coordinate System
= height - Y
Example Code: MyPanel.java import javax.swing.; import java.awt.; public class MyPanel extends JPanel { int mX = 20; int mY = 20; public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.fillRect(mX, mY,10, 20); } }
Example Code: Test.java
Example Code: Test.java public static void main(String args[ ]){ Test t = new Test( ); } } // end class
Example Code: MyPanel.java import javax.swing.; import java.awt.; public class MyPanel extends JPanel { int mX = 200; int mY = 0; public void paintComponent(Graphics g){ super.paintComponent(g); // erasing behaviour Graphics2D g2 = (Graphics2D)g; g2.setColor(Color.blue); g2.fillOval(mX, mY, 20, 20); } } // end class
Example Code: AnimTest.java p = new MyPanel(); c.add(p); f.setSize(400,400); f.setVisible(true); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); Timer t = new Timer (5, this); t.start(); } // end constructor …..
Example Code: AnimTest.java public void actionPerformed(ActionEvent ae){ if (f.getWidth()-40 == p.mX) x= -5; if (f.getHeight()-40 == p.mY) y= -3; if (p.mX == 0 ) x = 5; if (p.mY == 0 ) y=3; p.mX+=x; p.mY+=y; p.repaint() ; } Docsity.com