— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet365/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   361   362   363   364   365   366   367   368   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

controller 
events


getting 
gui
you are here
4
391
This final version builds on version two by adding the GUI parts. We build a 
frame, add a drawing panel to it, and each time we get an event, we draw a 
new rectangle and repaint the screen. The only other change from version 
two is that the notes play randomly as opposed to simply moving up the 
scale. 
The most important change to the code (besides building a simple GUI) 
is that we make the drawing panel implement the ControllerEventListener 
rather than the program itself. So when the drawing panel (an inner class) 
gets the event, it knows how to take care of itself by drawing the rectangle.
Complete code for this version is on the next page.
Version Three: drawing graphics in time with the music
 class MyDrawPanel extends JPanel implements ControllerEventListener {

boolean msg = false;
public void controlChange(ShortMessage event) {
msg = true;
repaint();
}
public void paintComponent(Graphics g) {
if (msg) {

Graphics2D g2 = (Graphics2D) g;
int r = (int) (Math.random() * 250);
int gr = (int) (Math.random() * 250);
int b = (int) (Math.random() * 250);
g.setColor(new Color(r,gr,b));
int ht = (int) ((Math.random() * 120) + 10);
int width = (int) ((Math.random() * 120) + 10);
int x = (int) ((Math.random() * 40) + 10);
int y = (int) ((Math.random() * 40) + 10);
g.fillRect(x,y,ht, width);
msg = false;
} // close if
} // close method
} // close inner class
The drawing panel inner class:
The drawing panel is a listener
We set a flag to false, and we’ll set it 
to true only when we get an event. 
We got an event, so we set the flag to 
true and call repaint()
We have to use a flag because OTHER things might trigger a repaint(), 
and we want to paint ONLY when there’s a ControllerEvent
The rest is code to generate 
a random color and paint a 
semi-random rectangle.



Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   361   362   363   364   365   366   367   368   ...   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