— Scott McNealy, Sun Microsystems Chairman, President and ceo



Download 40,57 Mb.
Pdf ko'rish
bet337/555
Sana26.05.2022
Hajmi40,57 Mb.
#609235
1   ...   333   334   335   336   337   338   339   340   ...   555
Bog'liq
Head First Java (Kathy Sierra, Bert Bates) (z-lib.org)

How do you KNOW if 
an object is an event 
source? 
Look in the API.
OK. Look for what?
A method that starts with 
‘add’, ends with ‘Listener’, 
and takes a listener inter-
face argument. If you see:
addKeyListener(KeyListener k)
you know that a class 
with this method is a 
source of KeyEvents. 
There’s a naming pattern.
event 
handling


getting 
gui
you are here
4
363
Now that we know a little about how events work (we’ll learn 
more later), let’s get back to putting stuff on the screen. 
We’ll spend a few minutes playing with some fun ways to get 
graphic, before returning to event handling. 
Getting back to graphics...
Three ways to put things on your GUI:
Put widgets on a frame
Add buttons, menus, radio buttons, etc.
frame.getContentPane().add(myButton);
The javax.swing package has more than a dozen 
widget types.
1
Put a JPEG on a widget
You can put your own images on a widget.
graphics.drawImage(myPic,10,10,this);
3
Draw 2D graphics on a widget
Use a graphics object to paint shapes.
graphics.fillOval(70,70,100,100);
You can paint a lot more than boxes and circles; 
the Java2D API is full of fun, sophisticated 
graphics methods.
2
Number of Head 
First Java books 
mistakenly 
bought by coffee 
house baristas.
art, games, 
simulations, etc.
charts, 
business 
graphics, 
etc.


364
 
chapter 12
If you want to put your own graphics on the screen, your best 
bet is to make your own paintable widget. You plop that widget 
on the frame, just like a button or any other widget, but when it 
shows up it will have your images on it. You can even make those 
images move, in an animation, or make the colors on the screen 
change every time you click a button. 
It’s a piece of cake.
Make a subclass of JPanel and override one 
method, paintComponent(). 
All of your graphics code goes inside the paintComponent() 
method. Think of the paintComponent() method as the method 
called by the system to say, “Hey widget, time to paint yourself.” 
If you want to draw a circle, the paintComponent() method will 
have code for drawing a circle. When the frame holding your 
drawing panel is displayed, paintComponent() is called and your 
circle appears. If the user iconifies/minimizes the window, the 
JVM knows the frame needs “repair” when it gets de-iconified, 
so it calls paintComponent() again. Anytime the JVM thinks the 
display needs refreshing, your paintComponent() method will be 
called. 
One more thing, you never call this method yourself! The argument 
to this method (a Graphics object) is the actual drawing canvas 
that gets slapped onto the 
real display. You can’t get this by 
yourself; it must be handed to you by the system. You’ll see 
later, however, that you 
can ask the system to refresh the display 
(repaint()), which ultimately leads to paintComponent() being 
called. 
Make your own drawing widget

Download 40,57 Mb.

Do'stlaringiz bilan baham:
1   ...   333   334   335   336   337   338   339   340   ...   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