Graphics come from paint in
Canvas repaint call, the system will call paint (Graphics g), which give the space to play with the programmers in the paint on the inside, then, paint the Graphics from where it come from? In other words, we paint the things in the end operation is it? He is also how to react to the phone hardware, the screen out?
1. Let's take a look at how the implementation repaint.
public final void repaint (int x, int y, int width, int height) (
synchronized (Display.LCDUILock) (
callRepaint (x + viewport [X], y + viewport [Y], width, height, null);
)
)
2. So callReapint is how to achieve it?
In Displable there is a way (to final logo), against any sub-class overrides this method is to ensure that only his repaint events to control the transfer:
final void callRepaint (int x, int y, int width, int height, Object target) (
if (currentDisplay! = null) (
/ / Note: Display will not let anyone but the current
/ / Displayable schedule repaints
currentDisplay.repaintImpl (paintDelegate, x, y, width, height, target); / / target is NULL
)
)
3, in Diaplay, the implementation of this method are as follows:
void repaintImpl (Displayable d, int x, int y, int w, int h,
Object target) (
synchronized (LCDUILock) (
if (paintSuspended | |! hasForeground | | d! = current) (
return;
)
)
eventHandler.scheduleRepaint (x, y, w, h, target); / / Repaint the request to continue the deployment of
)
4.
/ **
* Called to schedule a repaint of the current Displayable
* As soon as possible
*
* @ Param x The x coordinate of the origin of the repaint rectangle
* @ Param y The y coordinate of the origin of the repaint rectangle
* @ Param w The width of the repaint rectangle
* @ Param h The height of the repaint rectangle
* @ Param target An optional target Object, which may have been the
* Original requestor for the repaint
* /
public void scheduleRepaint (int x, int y, int w, int h, Object target) (
eventQueue.push (x, y, w, h, target); / / arrange the event after the deal is to push a message queue
)
5.
/ **
* Push a repaint
*
* @ Param x The x origin coordinate
* @ Param y The y origin coordinate
* @ Param w The width
* @ Param h The height
* @ Param target The optional paint target
* /
public void push (int x, int y, int w, int h, Object target) (
try (
w + = x; / / convert from width, height to absolute
h + = y; / / x2, y2
if (x <0) x = 0;
if (y <0) y = 0;
synchronized (qLock) (
if (paintX1 == -1) (
/ / If we have no pending repaint
/ / Just store the region
paintX1 = x;
paintY1 = y;
paintX2 = w;
paintY2 = h;
paintTarget = target;
) Else (
/ / If there is a pending repaint
/ / Union the dirty regions
if (paintX1> x) (
paintX1 = x;
)
if (paintY1> y) (
paintY1 = y;
)
if (paintX2 paintX2 = w; ) if (paintY2 paintY2 = h; ) paintTarget = null; ) ) / / Synchronized ) Catch (Throwable t) ( t.printStackTrace (); ) queuedEventHandler.process (); / / push the news, we must immediately deal ) 6. In fact, an event queue is an infinite loop of thread, constantly check the queue to see if there is need to address the message, there is treatment, without the wait. Now that push into a message, he would wake up immediately to deal with the news. So naturally notify the. / ** * Signal this handler there is a need to process * A pending event. * / public synchronized void process () ( try ( notify (); ) Catch (Throwable t) ( / / TO DO: Do something more useful with this t.printStackTrace (); ) ) 7. We look at the event queue EventQuene thread is defined, which repaintScreenEvent is to be processed on the repaint event. / ** * Process events from the EventQueue * / public void run () ( int x1, y1, x2, y2, type = 0; Display parentOfNextScreen = null; Displayable nextScreen = null; boolean call = false; …….. if (x1! = -1) ( repaintScreenEvent (x1, y1, x2, y2, target); x1 = y1 = x2 = y2 = -1; target = null; ) 8. So let us look at how to achieve repaintScreenEvent's, / ** * Process a repaint event * * @ Param x1 The x origin coordinate * @ Param y1 The y origin coordinate * @ Param x2 The lower right x coordinate * @ Param y2 The lower right y coordinate * @ Param target The optional paint target * / void repaintScreenEvent (int x1, int y1, int x2, int y2, Object target) ( try ( synchronized (eventLock) ( displayManager.repaint (x1, y1, x2, y2, target); / / call the screen manager reapint, please note that this is the 5 parameters of the repaint ) ) Catch (Throwable t) ( handleThrowable (t); ) ) 9. The five parameters of the implementation is as follows: / * * SYNC NOTE: this method performs its own locking of * LCDUILock and calloutLock. Therefore, callers * Must not hold any locks when they call this method. * / void repaint (int x1, int y1, int x2, int y2, Object target) ( Displayable currentCopy = null; synchronized (LCDUILock) ( if (paintSuspended | |! hasForeground) ( return; ) currentCopy = current; ) if (currentCopy == null) ( return; ) screenGraphics.reset (x1, y1, x2, y2); current.callPaint (screenGraphics, target); refresh (x1, y1, x2, y2); ) 10. Can be seen, screenGraphcis as a parameter passed to the callPaint, screenGraphics need to know how come, we look callPaint method. / ** * Paint this Canvas * * @ Param g the Graphics to paint to * @ Param target the target Object of this repaint * / void callPaint (Graphics g, Object target) ( super.callPaint (g, target); if (g.getClipY () + g.getClipHeight () < = viewport [Y]) ( return; ) / / We prevent the Canvas from drawing outside of the / / Allowable viewport - such as over the command labels g.clipRect (viewport [X], viewport [Y], viewport [WIDTH], viewport [HEIGHT]); synchronized (Display.calloutLock) ( g.translate (viewport [X], viewport [Y]); try ( paint (g); / / This is called after the repaint method, the system will call to paint (g), the g is the method of delivery call callPaint over the screenGraphcis, see the following analysis ) Catch (Throwable t) ( Display.handleThrowable (t); ) g.translate (-viewport [X],-viewport [Y]); ) ) 11, we now look screenGraphics come from there, in the Display category, there is a static data initialization code . / * * 1 10 11 12 13 14 15 16 17 18 19 2 20 21 22 23 24 25 26 27 28 29 3 30 31 32 4 5 6 7 8 9 Static initializer, constructor * / static ( / * Done this way because native access to static fields is hard 1 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17 / 18 / 19 / 2 / 20 / 21 / 22 / 23 / 24 / 25 / 26 / 27 / 28 / 29 / 3 / 30 / 31 / 32 / 4 / 5 / 6 / 7 / 8 / 9 / DeviceCaps c = new DeviceCaps (); WIDTH = c.width; HEIGHT = c.height; ADORNEDHEIGHT = c.adornedHeight; … … … c = null; / / let the DeviceCaps instance be garbage collected / * Let com.sun.midp classes call in to this class. 1 / 10 / 11 / 12 / 13 / 14 / 15 / 16 / 17 / 18 / 19 / 2 / 20 / 21 / 22 / 23 / 24 / 25 / 26 / 27 / 28 / 29 / 3 / 30 / 31 / 32 / 4 / 5 / 6 / 7 / 8 / 9 / displayManagerImpl = new DisplayManagerImpl (); / / initialize the screen manager DisplayManagerFactory.SetDisplayManagerImpl (displayManagerImpl); / / get the screen manager, that a factory method to use to prepare for the future, not dwell on here deviceAccess = new DisplayDeviceAccess (); / / guess might be the bridge KVM and DEVICE eventHandler = getEventHandler (); / / event handler screenGraphics = Graphics.getGraphics (null); / / Ha ha, screenGraphics found ) 12. In the Graphics class where there is a solution, getGraphics, however, this method is the default package-level access. So we will not see. static Graphics getGraphics (Image img) ( if (img == null) ( return new Graphics (Display.WIDTH, Display.HEIGHT); ) Else ( return new ImageGraphics (img); / / guess it is for Whom? ) ) 13. Graphics constructor as follows: Graphics (int w, int h) ( destination = null; maxWidth = (short) (w & 0x7fff); maxHeight = (short) (h & 0x7fff); init (); reset (); ) 14. God, finally found, native of an interface, init, is it to initialize the Graphics / ** * Intialize the native peer of this Graphics context * / private native void init (); 15. In fact, to find where excavation can not be said to achieve the most fundamental, but we defer to excavate here, so, to sum up, paint the Graphics, is to provide Display, only, this variable will not let us to. So the underlying implementation of KVM, which is native of the init (), will this Graphics to the right received a specific platform, such as win32 the GDI, Symbian's Graphics GDI, there is a common QT LINUX platform GDI so. Recommended links: Fans PARADISE - watch the game with three arms PPMate Jobs: find a good quality of the work of eight major 3G the first number up to the “special” user Easy To Use Mathematics Education Data Domain's Position Once Again Tend To NetApp Six college students recruited reef workplace IDS weaknesses and limitations (2) “Home” and the new audio-visual! Want to sing sing with ABPlayer DLL gray dove release type articles of the Back door of the signature changes

Admonish your friends privately, but praise them openly.
Comment by Cheap Jordan 2 — November 3, 2010 @ 8:20 am