Draw curve Import java.awt.*;p ublic class Ballgame extends Frame {Image img = Toolkit.getdefaulttoolkit (). GetImage ("Pi Ngpang.jpg "); The problem with this writing is that the static image is not displayed at the beginning and needs to be minimized or maximized before the double x = 200 is displayed; Double y = 200; Double degree = 0; public void Paint (Graphics g)//auto-call, g equivalent to brush {System.out.println ("window was drawn once"); G.drawimage (IMG, (int) x, (int) y, null); Ellipse/*x = + 200*math.cos (degree); y = + + 100*math.sin (degree); *//Sinusoidal x = x + 2; y = + 100*math.sin (degree); degree = degree + 0.1; G.drawimage (IMG, +, +, NULL); /*g.setcolor (Color.green); G.drawline (100, 100, 200, 200); G.drawrect (50, 50, 100, 80); G.drawoval (50, 50, 100, 80); G.setcolor (Color.yellow); g.DrawString ("The game begins! */} void Launchframe () {setSize (500, 300); SetLocation (50, 50); SettiTle ("Hallo ball"); SetBackground (Color.Blue); SetVisible (TRUE); New Paintthread (). Start (); public static void Main (string[] args) {new ballgame (). Launchframe (); } class Paintthread extends Thread {public void run () {while (true) { Repaint (); Repaint the window! try {thread.sleep (+);//40MS, 1S draw 25 times} catch (Exception e) {e.printstacktrace (); }}}}}//------------------------------I'm a split line---------------------------------------- -----------------------Pinball: Import java.awt.*;p ublic class Ballgame extends Frame {Image img = Toolkit.getdefaulttoo Lkit (). GetImage ("Pingpang.png"); The problem with this writing is that the static image is not displayed at the beginning and needs to be minimized or maximized before the double x = 100 is displayed; Double y = 100; Double degree = 3.14/3; public void PAint (Graphics g)//Automatic call, G equivalent to the brush {System.out.println ("window was drawn once"); G.drawimage (IMG, (int) x, (int) y, null); if (Y > 300-30) {degree =-degree; } else if (Y <) {degree =-degree; } else if (x > 500-30) {degree = 3.14-degree; } else if (x < 0) {degree = 3.14-degree; } x = x + 10*math.cos (degree); y = y + 10*math.sin (degree); G.drawimage (IMG, +, +, NULL); /*g.setcolor (Color.green); G.drawline (100, 100, 200, 200); G.drawrect (50, 50, 100, 80); G.drawoval (50, 50, 100, 80); G.setcolor (Color.yellow); g.DrawString ("The game begins! */} void Launchframe () {setSize (500, 300); SetLocation (50, 50); SettItle ("Hallo ball"); SetBackground (Color.Blue); SetVisible (TRUE); New Paintthread (). Start (); public static void Main (string[] args) {new ballgame (). Launchframe (); } class Paintthread extends Thread {public void run () {while (true) { Repaint (); Repaint the window! try {//Thread.Sleep (+);//40MS, 1S draw 25 times Thread.Sleep (40); } catch (Exception e) {e.printstacktrace (); }}}}}//----------------------------I'm a split line------------------------------------------ --------Notes: 1. Java Development Environment installation and configuration a) download JDK (Java development Kit) b) Install JDK, JRE (Java Runtime Environment) c) Configuration environment variable: I.path (the system looks for the relevant executable file through the directory specified by path!) Right-click My Computer properties advanced environment variable modify path! Ii.classpath (System through Classpath looking for class files) JDK1.5, you can not set! 2.Eclipse use (currently the most used ide:integrated developmentEnvironment integrated Development Environment) (a) download B) Unzip it 3. Create a new Java project a) New Java projects only need to enter project name, return! 4. Create a new Java Class A) right-click Srcnew class type name: BALLGAMEB) Enter code: public class Ballgame {public static void main (string[] args) { System.out.println ("I am the Gao of the Academy!") "); }}C) Three ways to run Java programs: I. Right-click Run Asjava applicationii. Alt+shift+x, then press JIII.CTRL+F11IV. Click the Run button to start developing our mini-Game 1. Add window import java.awt.*;p ublic class Ballgame extends Frame {void LA Unchframe () {setSize (500, 300); SetLocation (50, 50); SetVisible (TRUE); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); }}2. Loading pictures under Project Create a new images folder, copy sun.jpg to images below import java.awt.*;p ublic class Ballgame extends Frame {Image Sun = Tool Kit.getdefaulttoolkit (). GetImage ("images/sun.jpg"); This kind of writing is not good, but it is the simplest! public void Paint (Graphics g) {G.drawimage (Sun, +, +, NULL); } void LaunchframE () {setSize (500, 300); SetLocation (50, 50); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); SetVisible (TRUE); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); Note: At run time, the first time you open the window, you cannot see the picture. You need to minimize the window and then open it to see! 3. Learn to draw various curves, shapes, strings: Import java.awt.*;p ublic class Ballgame extends Frame {Image Sun = Toolkit.getdefaulttoolkit (). GE Timage ("images/sun.jpg"); This kind of writing is not good, but it is the simplest! public void Paint (Graphics g) {G.drawimage (Sun, +, +, NULL); G.setcolor (Color.Blue); G.drawline (100, 100, 200, 200); G.drawrect (50, 50, 100, 80); G.drawoval (50, 50, 100, 80); G.setcolor (Color.yellow); g.DrawString ("The game begins!! ", 80, 80); [For the time being, it has nothing to do with our game, but some BASIC programming knowledge!] ]} void Launchframe () {setSize (500, 300); SetLocation (50, 50); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); SetVisible (TRUE); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); }}4. Add animation import java.awt.*;p ublic class Ballgame extends Frame {Image Sun = Toolkit.getdefaulttoolkit (). GetImage ("I Mages/sun.jpg "); This kind of writing is not good, but it is the simplest! int x=100; int y=100; public void Paint (Graphics g) {System.out.println ("window was painted once!") "); G.drawimage (Sun, x, y, null); x = x+2; } void Launchframe () {setSize (500, 300); SetLocation (50, 50); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); SetVisible (TRUE); New Paintthread (). Start (); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); } class Paintthread extends Thread {public void run () {while (true) {repaint ();//Redraw window ! try{Thread.Sleep (+);//40ms 1s=1000ms}catch (Exception e) {e.printstacktrace (); }}}}}5. Control the movement of objects in the game through the mathematical functions (parabola, sinusoidal, ellipse, etc.) we have mastered! Import java.awt.*;p ublic class Ballgame extends Frame {Image Sun = Toolkit.getdefaulttoolkit (). GetImage ("Images/su N.jpg "); This kind of writing is not good, but it is the simplest! Double x=100; Double y=100; Double degree = 0; public void Paint (Graphics g) {System.out.println ("window was painted once!") "); G.drawimage (Sun, (int) x, (int) y, null); x =150+ 100*math.cos (degree); y = 150+100*math.sin (degree); degree = degree + 0.1; } void Launchframe () {setSize (500, 300); SetLocation (50, 50); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); SetVisible (TRUE); New Paintthread (). Start (); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); } class Paintthread extends Thread {public void run () {while (true) { Repaint (); Repaint the window! try{Thread.Sleep (+);//40ms 1s=1000ms}catch (Exception e) {E.prin Tstacktrace (); }}}}}6. Realize the game of billiards! Import java.awt.*;p ublic class Ballgame extends Frame {Image Sun = Toolkit.getdefaulttoolkit (). GetImage ("Images/su N.jpg "); This kind of writing is not good, but it is the simplest! Double x=100; Double y=100; Double degree = 3.14/3; public void Paint (Graphics g) {System.out.println ("window was painted once!") "); G.drawimage (Sun, (int) x, (int) y, null); x = x+ 10*math.cos (degree); y = y+10*math.sin (degree); if (y>300-30) {degree =-degree; } if (x>500-30) {degree = 3.14-degree; } if (x<0) {degree = 3.14-degree; } if (y<30) {degree =-degree; }} void Launchframe () {setSize (500, 300); SetLocation (50, 50); Settitle ("Shang Academy---Zhang San Works"); SetBackground (Color.Black); SetVisible (TRUE); New Paintthread (). Start (); } public static void Main (string[] args) {System.out.println ("I am the Gao of the Academy! "); New Ballgame (). Launchframe (); } class Paintthread extends Thread {public void run () {while (true) {repaint ();//Redraw window ! try{Thread.Sleep (+);//40ms 1s=1000ms}catch (Exception e) {E.prin Tstacktrace (); }}}}}7. Use the keyboard to control the movement of the object!
Reference: Shang Academy Gao game Video
Various curve motion, pinball, notes