Tải bản đầy đủ - 0 (trang)
Hack 55. Anti-Aliased Text with a Custom Look and Feel

Hack 55. Anti-Aliased Text with a Custom Look and Feel

Tải bản đầy đủ - 0trang

NomatterwhichSwingLookandFeelyouset,thewrap()

methodwillhandleitproperly.Onelineofcodeisgood,butnot

goodenough.We'dbebetteroffwithnolineofcodeatall.

FrédéricfeelsthesamewayandprovidestheexcellentWrapit

classyoucanusetoinstalltheWrapLookandFeelatruntime:





java-classpathwraplf.jar;.WrapitWebHunter



TheWrapitclasscontainsamain()entrypointthatwillinstallthe

WrapLookandFeelandthencallthemain()entrypointofthe

classpassedasthefirstargumentonthecommandline.This

LookandFeelisapowerfultoolyoucanusetoenhancethe

appearanceofanyJavaapplication,whetheryouhavethe

sourcecodeornot.

RomainGuy



Chapter8.Rendering

Section8.1.Hacks5664:Introduction

Hack56.CreateaMagnifyingGlassComponent

Hack57.CreateaGlobalRight-Click

Hack58.BlockaWindowWithoutaModalDialog

Hack59.CreateaColorEyedropper

Hack60.ChangingFontsThroughoutYourApplication

Hack61.LoadNewFontsatRuntime

Hack62.BuildaColorfulVector-BasedButton

Hack63.AddaThirdDimensiontoSwing

Hack64.TurntheSpotlightonSwing



8.1.Hacks5664:Introduction

Sometimesit'snotwhatyouputintoyourGUI,buthowyou

drawit.Thehacksinthischapterarebasedinsomewayon

using(orabusing)howAWTandSwingrenderthegraphic

contentsofaGUI.Inseveralcases,weuseJava2Dtobring

graphictransformationsandcolor-handlingtoSwing

components.Inothers,weuseAWT'sfonthandlingtochange

components;notjustJTextComponents,butanycomponentsthat

needtodrawtexttorenderthemselves.Andinstillothercases,

wemesswiththeprocessbywhichSwingrendersitscontents.



Hack56.CreateaMagnifyingGlassComponent



Zoominonthosepixelswithalittlecreativeabuseofthe

AWT'sdebuggingorientedRobotclass.

Somegraphicsprogramsuseacomponentthatshowsa

magnifiedviewofwhatthecursoriscurrentlyhoveringover.

Thiscanbeveryhelpfulfordoingpixel-accurateeditingofa

picture.

ItshouldbesimpleenoughtodoinSwinggetpixelsfromone

componentandputtheminanotherbuttherearesomemissing

pieces.Specifically,howdoyougetthepixelsoutofthesource

componentasanImagesoyoucandrawImage()themintothe

magnifiedcomponent?Youcoulddothisifyouownedthe

sourcecomponentandsetitupwithadouble-bufferbecause

creatingtheoffscreenbufferwouldrequirecreatinganImage,

whichisexactlywhatyouneededanyway.Butforanarbitrary

JComponent,youcan'tassumethatlevelofaccesstothesource's

pixels.

Butthere'sanotheroptionbackinAWT:theRobotclass,

introducedinJ2SE1.3.IthasacreateScreenCapture()methodthat

cangrabthescreen,orjustpartofit,andreturnitasaJava2D

BufferedImage.Thisiswhatweneedtogetthingsgoing.



8.2.1.BuildtheMagnifyingGlass

TheDetachedMagnifyingGlasswillneedtokeeptrackoftheComponent

it'sviewing,thecurrentmouselocationinthatcomponent,a

zoomfactor,anditsownsize.Itwillalsoneedaninstanceof

theAWTRobotfortakingscreengrabs.Theotherthingitneeds



todoistohaveaMouseMotionListener,sothatitwillgetupdates

onthecursor'spositionand,whenitchanges,doanewgrab

andrepaint().

TheDetachedMagnifyingGlasscodeisshowninExample8-1.



Example8-1.JComponenttoprovideamagnified

viewofanotherJComponent

publicclassDetachedMagnifyingGlassextendsJComponent



implementsMouseMotionListener{





doublezoom;



JComponentcomp;



Pointpoint;



DimensionmySize;



Robotrobot;









publicDetachedMagnifyingGlass(JComponentcomp,











Dimensionsize,











doublezoom){











































this.comp=comp;

//flagtosaydon'tdrawuntilwegetaMouseM

point=newPoint(-1,-1);

comp.addMouseMotionListener(this);

this.mySize=size;

this.zoom=zoom;

//ifwecan'tgetarobot,thenwejustnever

//paintanything

try{



robot=newRobot();





}catch(AWTExceptionawte){







System.err.println("Can'tgetaRobot"







awte.printStackTrace();



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Hack 55. Anti-Aliased Text with a Custom Look and Feel

Tải bản đầy đủ ngay(0 tr)

×