— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet358/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   354   355   356   357   358   359   360   361   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

}
}
each time paintComponent() is 
called, the oval gets painted at a 
different location 
But where do we get the new x and y 
coordinates?
And who calls repaint()?
Sharpen your pencil
See if you can design a simple solution to get the ball to animate from the top left of the 
drawing panel down to the bottom right. Our answer is on the next page, so don’t turn 
this page until you’re done!
Big Huge Hint: make the drawing panel an inner class. 
Another Hint: don’t put any kind of repeat loop in the paintComponent() method.
Write your ideas (or the code) here:
there are no
Dumb Questions


384
 
chapter 12
animation using an 
inner class
import javax.swing.*;
import java.awt.*;
public class SimpleAnimation {
int x = 70;
int y = 70;
public static void main (String[] args) {
SimpleAnimation gui = new SimpleAnimation ();
gui.go();
}
public void go() {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
MyDrawPanel drawPanel = new MyDrawPanel();
frame.getContentPane().add(drawPanel);
frame.setSize(300,300);
frame.setVisible(true);
for (int i = 0; i < 130; i++) {
x++;
y++;
drawPanel.repaint();

try {
Thread.sleep(50);
} catch(Exception ex) { }
}

}
// close go() method
class MyDrawPanel extends JPanel {

public void paintComponent(Graphics g) {
g.setColor(Color.green);

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   354   355   356   357   358   359   360   361   ...   555




Ma'lumotlar bazasi mualliflik huquqi bilan himoyalangan ©hozir.org 2024
ma'muriyatiga murojaat qiling

kiriting | ro'yxatdan o'tish
    Bosh sahifa
юртда тантана
Боғда битган
Бугун юртда
Эшитганлар жилманглар
Эшитмадим деманглар
битган бодомлар
Yangiariq tumani
qitish marakazi
Raqamli texnologiyalar
ilishida muhokamadan
tasdiqqa tavsiya
tavsiya etilgan
iqtisodiyot kafedrasi
steiermarkischen landesregierung
asarlaringizni yuboring
o'zingizning asarlaringizni
Iltimos faqat
faqat o'zingizning
steierm rkischen
landesregierung fachabteilung
rkischen landesregierung
hamshira loyihasi
loyihasi mavsum
faolyatining oqibatlari
asosiy adabiyotlar
fakulteti ahborot
ahborot havfsizligi
havfsizligi kafedrasi
fanidan bo’yicha
fakulteti iqtisodiyot
boshqaruv fakulteti
chiqarishda boshqaruv
ishlab chiqarishda
iqtisodiyot fakultet
multiservis tarmoqlari
fanidan asosiy
Uzbek fanidan
mavzulari potok
asosidagi multiservis
'aliyyil a'ziym
billahil 'aliyyil
illaa billahil
quvvata illaa
falah' deganida
Kompyuter savodxonligi
bo’yicha mustaqil
'alal falah'
Hayya 'alal
'alas soloh
Hayya 'alas
mavsum boyicha


yuklab olish