Java exercises, a daily Java applet -1...__ Small program

Source: Internet
Author: User
Tags mul
Java exercises, how much you can do. http://bbs.csdn.net/topics/110067294
This is seen from CSDN. One every day.
"Program 10"
Title: A ball from the height of 100 meters free fall, each landing after the back of the original height of half, and then fell, to the 10th time when landing, the total number of meters. How high the 10th rebound.
This topic, I will post two kinds of code. In fact, this topic, I am tired of simple calculation, want to make a little fun, the result of a few days delay, finally found that the bad.
First of all, to understand the problem and solve problems.
= = = = = = Main.java = = public class Main {private Double totalheight = $; private double curheight = 50;
public void Drop (int times) {if ((times-1) = = 0) {return;}
Settotalheight (Gettotalheight () + 2 * getcurheight ()); Setcurheight (Getcurheight ()/2);
Drop (times-1); }
Public double Gettotalheight () {return totalheight;}
public void Settotalheight (double totalheight) {totalheight = Totalheight;}
Public double Getcurheight () {return curheight;}
public void Setcurheight (double curheight) {curheight = Curheight;}
public static void Main (string[] args) {main main = new main (); Main.drop (2); System.out.println ("total height is" + main.gettotalheight ()); System.out.println ("current" + main.getcurheight ());}
And then the second kind of =====.
= = = = = = = Main.java = Package Main;
Import Javax.swing.JFrame;
Import panel. Ballpanel; Import time. Delay;
public class MainFrame extends JFrame {public MainFrame (String title) {Super (title);}
public static void Main (string[] args) {Delay Delay = new Delay ();
MainFrame frame = new MainFrame ("Hello JFrame"); Ballpanel Ballpanel = new Ballpanel (); Frame.add (Ballpanel); Frame.setsize (500, 500); Frame.setvisible (TRUE); Frame.setdefaultcloseoperation (Jframe.exit_on_close);
Delay.initdelay ();
Ballpanel.setdelay (Delay.getdelay ()); Ballpanel.loopdrop (0); } }
= = = = = Delay.java = Package time;
public class Delay {public void Initdelay () {int g = + int i = 0; double s, t, t0 = 0.0;
Delay = new INT[100];
for (s = s < 10100 s + +) {t = math.sqrt (2 * s/g); delay[i++] = (int) ((t-t0) *; t0 = t;}}
Public int[] Getdelay () {return delay;}
private int delay[]; }
= = = = Ballpanel.java = = Package panel;
Import Java.awt.Color; Import Java.awt.Graphics;
Import Javax.swing.JPanel;
public class Ballpanel extends JPanel {public Ballpanel () {super ();}
public void Paint (Graphics g) {g.clearrect (0, 0, this.getwidth (), This.getheight ()); G.setcolor (Color.Blue); g.filloval (Ballcenter, 30, 30); }
public void Loopdrop (int height) {int i;
if (this.height = = height) {//At bottom for (i = 0; i < targetheight i + = MUL) {ballcenter = This.height-i; Repaint (); try {thread.sleep (delay[(targetheight-i-1)/MUL]); \ catch (Interruptedexception e) {e.printstacktrace ();}} Loopdrop (this.height-i); else {//at top for (i = height; i < this.height i + = MUL) {ballcenter = i; this.repaint (); try {thread.sleep (del ay[(i-height)/MUL]); catch (Interruptedexception e) {e.printstacktrace ();}}
Targetheightv = TARGETHEIGHTV/2; Targetheight = Targetheightv; if (targetheight!= 0) {loopdrop (i);}} }
public void Setdelay (int delay[]) {this.delay = delay;}
private int extracted () {return * MUL;}
private int targetheight = extracted ();
private int Targetheightv = extracted (); private int ballcenter = 0; private int height = extracted (); private int delay[];
Private final int MUL = 4; }
In fact, the so-called second, is the use of JFrame on the JPanel on the drawing, intuitive display of the effect of each bounce. Because the 100 pixel is too small, so I made a variable mul, equal to the effect of proportional expansion. The problem is that we're running out of space less than 10 times, so ... The right to be entertained. Of course, can be equal to magnification, such as 100M as 10000 pixels, this can be more than a few times. This program, the last ball is there, the program will not exit itself.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.