📄 gra.cpp
字号:
/*** gra.cpp - ** Copyright (c) 1999* Transvirtual Technologies, Inc. All rights reserved.** See the file "license.terms" for information on usage and redistribution * of this file. */#include "math.h"#include "toolkit.hpp"#define WINDOW 0#define IMAGE 1#define GRAPHICS 2void _cdecl getArcPoints( int x, int y, int width, int height, int startAngle, int arcAngle, POINT& ps, POINT& pe) { int maxr, xc, yc; double rad, sa, ea; rad = -3.1415926535 / 180; maxr = ( max( width, height) ) /2; sa = rad * startAngle; ea = rad * (startAngle+arcAngle); xc = x + width/2 - 1; yc = y + height/2 - 1; ps.x = (int)( cos(sa) * maxr) + xc; ps.y = (int)( sin(sa) * maxr) + yc; pe.x = (int)( cos(ea) * maxr) + xc; pe.y = (int)( sin(ea) * maxr) + yc; // printf( "x:%d y:%d w:%d h:%d x1:%d y1:%d x2:%d y2:%d\n", // x, y, width, height, ps.x, ps.y, pe.x, pe.y);}int __cdecl jarray2Points ( POINT** pp, int x0, int y0, jintArray xPoints, jintArray yPoints, int nPoints ){ register int i; int n; jboolean isCopy; jint *jx = JniEnv->GetIntArrayElements( xPoints, &isCopy); jint *jy = JniEnv->GetIntArrayElements( yPoints, &isCopy); if ( (n = JniEnv->GetArrayLength( xPoints)) < nPoints ) nPoints = n; if ( (n = JniEnv->GetArrayLength( yPoints)) < nPoints ) nPoints = n; *pp = (POINT*)getBuffer( X, (nPoints+1)*sizeof(POINT)); for ( i=0; i<nPoints; i++){ (*pp)[i].x = jx[i] + x0; (*pp)[i].y = jy[i] + y0; } JniEnv->ReleaseIntArrayElements( xPoints, jx, JNI_ABORT); JniEnv->ReleaseIntArrayElements( yPoints, jy, JNI_ABORT); return nPoints;}HBRUSH HB_0 = (HBRUSH)GetStockObject( NULL_BRUSH);HPEN HP_0 = (HPEN)GetStockObject( NULL_PEN);struct Graphics { HDC dc; KWnd *wnd; HBITMAP hbm; HBRUSH brush; POINT cliOff; HPEN pen; COLORREF fgClr; COLORREF bgClr; char xor; COLORREF xclr; int x0; int y0; void init ( HDC hdc, KWnd* pWnd, HBITMAP hb, COLORREF cfg, COLORREF cbg, Font* fnt) { dc = hdc; wnd = pWnd; hbm = hb; fgClr = cfg; bgClr = cbg; brush = CreateSolidBrush( fgClr); pen = CreatePen( PS_SOLID, 0, fgClr); xor = 0; DeleteObject( SelectObject( dc, brush)); DeleteObject( SelectObject( dc, pen )); SelectObject( dc, ((Font*)fnt)->fnt); if ( hbm ) { SelectObject( dc, hbm ); }#if !defined(UNDER_CE) SetTextAlign( dc, TA_BASELINE | TA_LEFT);#endif SetBkMode( dc, TRANSPARENT); SetTextColor( dc, fgClr); if ( wnd) { cliOff.x = wnd->insets.left; cliOff.y = wnd->insets.top; } else { cliOff.x = 0; cliOff.y = 0; } } void restoreBrush() { SelectObject( dc, brush); } void selectNullBrush() { SelectObject ( dc, HB_0); } void restorePen() { SelectObject( dc, pen); } void selectNullPen() { SelectObject ( dc, HP_0); } void setFont( HFONT fnt) { SelectObject( dc, fnt); } void setXORMode( int xorClr) { xor = 1; xclr = xorClr; pen = CreatePen( PS_SOLID, 0, fgClr ^ xorClr); DeleteObject( SelectObject( dc, pen)); SetROP2( dc, R2_XORPEN); } void setPaintMode() { xor = 0; pen = CreatePen( PS_SOLID, 0, fgClr); DeleteObject( SelectObject( dc, pen)); SetROP2( dc, R2_COPYPEN); } bool setBackground ( int irgb) { if ( irgb < 0 ) return FALSE; bgClr = (COLORREF)irgb; return true; } bool setForeground ( int irgb) { if ( irgb < 0 ) return FALSE; fgClr = (COLORREF)irgb; SetTextColor( dc, fgClr); brush = CreateSolidBrush( fgClr); DeleteObject( SelectObject( dc, brush)); pen = CreatePen( PS_SOLID, 0, fgClr); DeleteObject( SelectObject( dc, pen)); return true; } boolean fillRect( int x, int y, int w, int h, int clr) { HBRUSH hbr = 0; HPEN hp; if ( clr < 0 ) return false; if ( (COLORREF)clr != fgClr ) { hbr = (HBRUSH)SelectObject( dc, CreateSolidBrush( (COLORREF)clr )); } #if defined( UNDER_95 ) // strange repaint() behaviour under NT ??? PatBlt( dc, x+x0, y+y0, w, h, PATCOPY );#else hp = (HPEN)SelectObject( dc, HP_0); Rectangle( dc, x+x0, y+y0, x+x0+w+1, y+y0+h+1); SelectObject( dc, hp);#endif if ( hbr != 0) { DeleteObject( SelectObject( dc, hbr)); } GFLUSH(); return true; } void drawRect( int x, int y, int w, int h ) { HBRUSH bo = (HBRUSH)SelectObject( dc, HB_0); Rectangle( dc, x+x0, y+y0, x+x0+w+1, y+y0+h+1); SelectObject( dc, bo); GFLUSH(); } void drawLine ( int x1, int y1, int x2, int y2) { POINT pts[2]; pts[0].x = x1+x0; pts[0].y = y1+y0; pts[1].x = x2+x0; pts[1].y = y2+y0; Polyline(dc, pts, 2); GFLUSH(); } void drawText( int x, int y, TCHAR* txt, int len) {#if defined(UNDER_CE) /* The 'y' value is the base of the character but WinCE things is the top - we deduce 10 * to adjust it (we should be doing this based on the font size. */ y -= 10;#endif TextOut( dc, x+x0, y+y0, txt, len); GFLUSH(); } void translate( int x, int y) { /* * we use explicit translation, do not change * viewport here */ x0 = x - cliOff.x; y0 = y - cliOff.y; } void cleanUp() { // kprintf( "releaseDC: %x %x\n", wnd, dc); if ( brush ) { SelectObject( dc, HB_0 ); DeleteObject( brush); brush = 0; } if ( pen ) { SelectObject( dc, HP_0 ); DeleteObject( pen); pen = 0; } if ( wnd ) { ReleaseDC( wnd->hwnd, dc); wnd = 0; } if ( hbm ) { hbm = 0; } } void setClip( int x, int y, int w, int h, boolean set) { HRGN rgn; x += x0; y += y0; rgn = CreateRectRgn( x, y, x+w, y+h); if (set) { SelectClipRgn(dc, rgn); } else {#if !defined(UNDER_CE) ExtSelectClipRgn( dc, rgn, RGN_AND);#else //How do we do this in WinCE ???#endif } DeleteObject( rgn); } };/******************************************************************************** exported functions*/extern "C" { void* __cdecl Java_java_awt_Toolkit_graInitGraphics ( JNIEnv* env, jclass clazz, Graphics* gr, jobject tgt, jint tgtType, jint xOff, jint yOff, jint xClip, jint yClip, jint wClip, jint hClip, jobject fnt, jint fg, jint bg, jboolean blank ) { KWnd *wnd = 0; HDC hdc = 0; Image *img = 0; HBITMAP hbm = 0; Graphics *gr1; // printf( "%d,%d %d,%d,%d,%d %x %x %d\n", xOff, yOff, xClip, yClip, wClip, hClip, gr, tgt, tgtType); // disable batching / flush //GdiSetBatchLimit( 1); if ( tgt ) { if ( gr ) { gr->cleanUp(); } switch ( tgtType) { case WINDOW: wnd = (KWnd*)tgt; hdc = GetDC( wnd->hwnd); break; case IMAGE: img = (Image*)tgt; hdc = img->dc; hbm = img->bmp; break; case GRAPHICS: gr1 = (Graphics*)tgt; wnd = gr1->wnd; if ( wnd ) { hdc = GetDC( wnd->hwnd); } else { hdc = gr1->dc; } hbm = gr1->hbm; break; } } else if ( gr ) { hdc = gr->dc; wnd = gr->wnd; hbm = gr->hbm; } if ( ! gr) { gr = new Graphics(); } gr->init( hdc, wnd, hbm, fg, bg, (Font*)fnt); gr->translate( xOff, yOff); gr->setClip( xClip, yClip, wClip, hClip, TRUE); if ( blank) { gr->fillRect( xClip, yClip, wClip, hClip, (int)bg); } return gr; } void __cdecl Java_java_awt_Toolkit_graFreeGraphics ( JNIEnv* env, jclass clazz, Graphics* gr ) { DBG( awt_gra, ("freeGraphics: %x\n", gr)); gr->cleanUp(); delete gr; } void __cdecl Java_java_awt_Toolkit_graCopyArea ( JNIEnv* env, jclass clazz, Graphics* gr, jint x, jint y, jint width, jint height, jint xDelta, jint yDelta ) { DBG( awt_gra, ("copyArea: %x, %d,%d-%d,%d %d,%d\n", gr, x,y,width,height, xDelta,yDelta)); BitBlt( gr->dc, x+xDelta+gr->x0, y+yDelta+gr->y0, width, height, gr->dc, x+gr->x0, y+gr->y0, SRCCOPY); GFLUSH(); } void __cdecl Java_java_awt_Toolkit_graClearRect ( JNIEnv* env, jclass clazz, Graphics* gr, jint x, jint y, jint width, jint height ) { DBG( awt_gra, ("clearRect: %x, %d,%d-%d,%d\n", gr, x,y,width,height)); gr->fillRect( x, y, width, height, (int)gr->bgClr); } void __cdecl Java_java_awt_Toolkit_graDrawBytes ( JNIEnv* env, jclass clazz, Graphics* gr, jbyteArray jBytes, jint offset, jint len, jint x, jint y ) { jboolean isCopy; int n; jbyte *jb, *jbo; TCHAR *cs; if ( !jBytes ) return; n = env->GetArrayLength( jBytes); jb = env->GetByteArrayElements( jBytes, &isCopy); jbo = jb + offset; if ( offset+len > n ) len = n - offset; cs = jbyte2WinString( X, jbo, len); DBG( awt_gra, ("drawBytes: %x, %x,%d,%d \"%s\" %d,%d\n", gr, jBytes,offset,len, cs, x,y)); gr->drawText( x, y, cs, len); env->ReleaseByteArrayElements( jBytes, jb, JNI_ABORT); } void __cdecl Java_java_awt_Toolkit_graDrawChars ( JNIEnv* env, jclass clazz, Graphics* gr, jcharArray jChars, jint offset, jint len, jint x, jint y ) { jboolean isCopy; int n; jchar *jc, *jco; TCHAR *cs; if ( !jChars ) return; n = env->GetArrayLength( jChars); jc = env->GetCharArrayElements( jChars, &isCopy);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -