仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 1213|回复: 19
打印 上一主题 下一主题

[学习教程] JAVA网页设计[援用] Taking a look at SWT Images

[复制链接]
不帅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-18 11:48:32 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
你精通任何一门语言就最强大。现在来看,java的市场比C#大,C#容易入手,比较简单,java比较难
From:http://www.eclipse.org/articles/Article-SWT-images/graphics-resources.html

Thisfirstsectionofthisarticlegivesanintroductiontocolorsandshowshowanimagerecordsthecolorvalueofeachpixel.
IntroductionImagelifecycleImageDataColorPaletteDataIndexedpaletteDirectpaletteThenextsectiondescribesimagetransparency,alphablending,animation,andhowtoscaleimages.TransparencyManipulatingImageDataSavingImagesBlendingSinglealphavalueDifferentalphavalueperpixelImageeffectsGIFanimationScalingFinally,thearticleshowshowtocreatecursorsfromimages,byusingasourceimagetogetherwithamask.CursorsPlatformcursorsCustomcursorsIntroductionThesimplestwaytocreateanSWTImageistoloaditfromarecognizedgraphicfileformat.ThisincludesGIF,BMP(Windowsformatbitmap),JPG,andPNG.TheTIFFformatisalsosupportedinmorerecentEclipsereleases.ImagescanbeloadedfromaknownlocationinthefilesystemusingtheconstructorImage(Displaydisplay,StringfileLocation):
Imageimage=newImage(display,"C:/eclipse/eclipse/plugins/org.eclipse.platform_2.0.2/eclipse_lg.gif");

Insteadofhard-codingthelocationoftheimage,itsmorecommontoloadtheImagefromafolderlocationrelativetoagivenclass.ThisisdonebycreatinganInputStreampointingtothefilewiththemethodClass.getResourceAsStream(Stringname),andusingtheresultastheargumenttotheconstructorImage(Displaydisplay,InputStreaminputStream).

TheEclipsepackageexplorerbelowshowstheclasscom.foo.ShellWithButtonShowingEclipseLogoandtheeclipse_lg.gifinthesamefolder.Tofollowingcodewouldloadthegraphicfromitslocationrelativetotheclass.

Imageimage=newImage(display,ShellWithButtonShowingEclipseLogo.class.getResourceAsStream("eclipse_lg.gif"));



OncetheimagehasbeencreateditcanbeusedaspartofacontrolsuchasaButtonorLabelthatisabletorenderthegraphicaspartoftheirsetImage(Imageimage)methods.

Buttonbutton=newButton(shell,SWT.PUSH);button.setImage(image);



ImagescanbedrawnontousingagraphicscontextthatiscreatedwiththeconstructorGC(Drawabledrawable)withtheImageastheargument.

GCgc=newGC(image);gc.setForeground(display.getSystemColor(SWT.COLOR_WHITE));gc.drawText("Ivebeendrawnon",0,0,true);gc.dispose();



UsingaGCtodrawontoanImagepermanentlyaltersthegraphic.MoreinformationonhowtouseaGCiscoveredinthearticleGraphicsContext-Quickonthedraw.
ImagelifecycleWhenanimageisloaded,thefirststepistocreatedeviceindependentImageDatarepresentedbytheclassorg.eclipse.swt.graphics.ImageData.Followingthis,thedataispreparedforaspecificdevicebycreatinganactualImageinstance.
AswellasloadinganImagedirectlyfromafile,youcanseparatelycreatetheImageDataobjectandthenconstructtheImageusingImage(Devicedevice,ImageDataimageData).ThedataforanexistingImagecanberetrievedusinggetImageData(),althoughthiswillnotbethesameobjectthatwasusedtocreatetheimage.Thisisbecausewhenpreparinganimagetobedrawnontoascreen,propertiessuchasitscolordepthmightbedifferentfromtheinitialimagedata.

InstancesofImagerepresentanunderlyingresourcethathasbeenpreparedforaspecificdeviceandtheymustbedisposedwhentheyarenolongerrequiredtofreeuptheallocatedresource.ThereisnofinalizationofresourcesinSWTwhenanobjectisgarbagecollected.FormoreinformationseeSWT:TheStandardWidgetToolkit:ManagingOperatingSystemResources.
ImageDataImageDatacanbethoughtofasthemodelforanimage,whereastheImageistheviewthatsbeenpreparedforoutputontoaspecificdevice.TheImageDatahasawidthandheight,andapixelvalueforeachcoordinate.Therawdataoftheimageisabyte[],andthedepthoftheimagespecifiesthenumberofbitsthatareusedforeachcoordinate.Animagedepthof1canstoretwopossiblevaluesforeachpixel(0and1),adepthof4canstore2^4=16,adepthof8canstore2^8=256valuesandadepthof24canrepresent2^24=16milliondifferentvaluesperpixel.Thelargerthedepthofanimagethemorebytesarerequiredforitspixels,andsomeformatssuchasGIFthatwereinitiallydesignedfordownloadacrossinternetconnectionsonlysupportamaximumdepthof8.Howthevalueofeachpixelvaluetranslatesintoanactualcolordependsonitspaletterepresentedbytheclassorg.eclipse.swt.graphics.PaletteData.
ThenextsectiondescribeshowcolorsarerepresentedbytheirRGBvalues,andhowPaletteDatamapsamappixelvaluetoaparticularcolor.
ColorTheclassorg.eclipse.swt.graphics.ColorisusedtomanageresourcesthatimplementtheRGBcolormodel.Eachcolorisdescribedintermsofitsred,greenandbluecomponent(eachexpressedasanintegervaluefrom0fornocolorto255forfullcolor).
ColorcyanColor=newColor(display,0,255,255);//...CodetousetheColorcyanColor.dispose();

Theconvenienceclassorg.eclipse.swt.graphics.RGBexistsinSWTthatcombinesacolorsred,greenandbluevaluesintoasingleobject.

RGBcyanRGB=newRGB(0,255,255);ColorcyanColor=newColor(display,cyanRGB);//...CodetousetheColorcyanColor.dispose();

TheColorinstanceshouldbedisposedwhenitisnolongerrequired,whereastheRGBhasnoneedtobedisposed.ThisissimilartotherelationshipbetweenanImageanditsImageData,whereColorandImagearedevicespecificobjectsusingunderlyingnativeresources,whileRGBandImageDataaretheunderlyingmodeldata.

Toavoidhavingtocreateandmanageinstancesofthecommonlyusedcolors,theDisplayclassallowthesetoberetrievedusingthemethodDisplay.getSystemColor(intid).

ColorcyanColor=display.getSystemColor(SWT.COLOR_CYAN)

WhenaColorisobtainedbyanSWTprogramusingthemethodDisplay.getSystemColor(intid)method,itmustnotbedisposed.TheruleofthumbthatworksforanySWTresourceis"Ifyoucreatedit,youareresponsiblefordisposingit".Becausethestatementaboveretrievedthecyancolorinstance,anddidntexplicitlyconstructit,itshouldnotbedisposed.

HowaColorisactuallyrepresentedonthedisplaydependsonfactorssuchastheresolutionanddepthofthedisplay.FormoreinformationonthisandtheSWTcolormodelseeSWTColorModel.
PaletteDataTherearetwokindsofPaletteData,anindexedpaletteandadirectpalette.PaletteDataisamodelofhowpixelvaluesmaptoRGBvalues,andbecausetheydonotrepresentanunderlyingresource,theydonotrequiredisposing.IndexedpaletteWithanindexedpaletteeachpixelvaluerepresentsanumberthatisthencrossindexedwiththepalettetodeterminetheactualcolor.Therangeofallowablepixelvaluesisthedepthoftheimage.
Theexamplebelowisa48by48squareimagecreatedwithadepthof1,andanindexedcolorpalette.Theindexedpaletteassigns0toberedand1tobegreen(byvirtueoftheirorderintheRGB[]intheconstructor).TheImageDatasun-initializedpixelvalueswillinitiallybe0(red),andtwoforloopsseta34by34squareinthecenteroftheImageDatatobe1(green).

PaletteDatapaletteData=newPaletteData(newRGB[]{newRGB(255,0,0),newRGB(0,255,0)});ImageDataimageData=newImageData(48,48,1,paletteData);for(intx=11;x<35;x++){for(inty=11;y<35;y++){imageData.setPixel(x,y,1);}}Imageimage=newImage(display,imageData);



Theaboveexamplehasadepthof1soitcanstore2colors,butasthecolordepthoftheImageDataincreasesthensocanthenumberofcolorsinthepalette.Anindexedpalettecanhavea1,2,4,or8bitdepths,andan8bitdepthprovides2^8=256possiblecolors.Tohaveahighercolordepth(suchas16,24,or32)adirectpalettemustbeused.
DirectpaletteInsteadofhavingeachpixelvaluerepresentanindexinthepalettecorrespondingtoitscolor,adirectpaletteallowseachpixelvaluetodirectlyrecorditsred,greenandbluecomponent.AdirectPaletteDatadefinesred,greenandbluemasks.Thesemasksarenumberofbitsrequiredtoshiftapixelvaluetotheleftsothehighbitofthemaskalignswiththehighbitofthefirstbyteofcolor.Forexample,a24bitdirectpalettecandivideitselfinto3portions,storingredinthelowest8bits,greeninthecentral8bitsandblueinthehighest8bits.Theredshiftmaskwouldbe0xFF,green0xFF00andblue0xFF0000.


Thevalueforeachpixelrepresentsacombinationofthered,greenandbluecomponentsintoasingle24bitinteger.Toconstructanindexedpalettetheconstructorusedallowsthered,greenandbluecolormaskstobespecified.

PaletteDatapalette=newPaletteData(0xFF,0xFF00,0xFF0000);ImageDataimageData=newImageData(48,48,24,palette);

Usingthesametechniqueasearlier,thecodeiteratesovereverypixelcoordinatesettingittoeither0xFF(forred)or0xFF00(forgreen).

for(intx=0;x<48;x++){for(inty=0;y<48;y++){if(y>11&&y<35&&x>11&&x<35){imageData.setPixel(x,y,0xFF00);//Setthecentertogreen}else{imageData.setPixel(x,y,0xFF);//andeverythingelsetored}}};Imageimage=newImage(display,imageData);

Thiscreatestheresultbelowwheretheimageisredwithagreencenter.



Becauseyoucanusecolordepthsof16,24and32bitswithdirectpalettes,youcanrepresentmorecolorsthanareavailablewithanindexedpalettewhosemaximumdepthis8.Acolordepthof24allowsyoutorepresent16millioncolors(2^24).Thetradeoffhoweverissize,becauseanindexedpalettewithadepthof8requiresonebyteperimagecoordinatewhereasadirectpalettewithadepthof24requiresthreebytesperimagecoordinate.

WithbothdirectandindexedpalettesyoucangofromanRGBtoapixelvalueandvice-versausingthepublicmethodsintgetPixel(RGBrgb)andRGBgetRGB(intpixelValue).
TransparencyThepurposeoftransparencyistomakeaportionoftheimagenon-opaque,sowhenitisdrawnonaGUIsurfacetheoriginalbackgroundshowsthrough.Thisisdonebyspecifyingthatoneoftheimagecolorsistransparent.Wheneverapixelwiththetransparentcolorvalueisdrawn,insteadofusingtheRGBvaluedefinedinthepalettetheoriginaldestinationbackgroundpixelcolorisusedinstead.Theeffecttotheuseristhattheareasoftheimagethatequalthetransparentpixelshowthebackgroundofwhatevertheimageisbeingdrawnover,thusachievingtransparency.PersistedimagefileformatssuchasGIForBMPallowyoutospecifyatransparentpixelvalue,althoughonlyifthepaletteisindexedandthecolordepthis8orless.
WhenImagesareuseddirectlyoncontrolssuchasButtonorLabelthenativebehaviormaybethattransparentpixelsareignoredanddrawninthepixelcolorspecifiedbythesource.NativeimagetransparencyhoweverissupportedinSWTforoperationsinvolvingaGC.ToillustratethisthefollowingfileIdea.gifhasacolordepthof8,andthewhitepixel(index255inthepalette)settobethetransparentpixel.



TheshellbelowhasaLabelontheleftwithaCanvasnexttoit.TheIdea.gifisusedasthelabelsimage,andalsointhepainteventoftheCanvas.BecausetheLabeldoesnotsupportnativetransparencytheoriginalwhitecolorofthetransparentpixelisusedasthebackground,howevertheGCinthepainteventrespectsthetransparentpixelandthegreybackgroundshowsthrough.

ImageideaImage=newImageData(getClass().getResourceAsStream("Idea.gif"));Labellabel=newLabel(shell,SWT.NONE);label.setImage(ideaImage);Canvascanvas=newCanvas(shell,SWT.NO_REDRAW_RESIZE);canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEvente){e.gc.drawImage(ideaImage,0,0);}});



FortheaboveexampleIstackedthedeckinmyfavor,becauseIdidntactuallyusetheideagraphicthatisincludedwitheclipsearticlesintheirbanner.ThereasonisthattheoriginalgraphicisaJPGfilewhichdoesntsupporttransparency,soIusedagraphicstooltoconvertittoaGIFandsetthevalueofthewhitepixelinthepalettetobethetransparencypixel.TheoriginalIdea.jpgisshownbelow,andalthoughitlooksthesameastheIdea.gif,thisisbecauseitisonthewhitebackgroundoftheHTMLbrowser.



ByusingtheoriginalJPGfilethisoffersagoodexampleofhowwetoachieveatransparencyeffectprogrmaticallybymanpulatingitsImageData.TheImageDataclasshasapublicfieldtransparentPixeltospecifywhichpixelistransparentthatcanbesetonceapersistedimagefileisloadedintoanImageDatainstance,irrespectiveofwhetherthepersistedfileformatsupportstransparency.

ThecodebelowloadstheIdea.jpgfileinanImageDataobjectandsetsthetransparentpixelfortheImageDatatobethepixelvalueofthecolorwhiteinthepalette.ThepixelvalueintheindexedpalettethatrepresentswhiteisretrievedbyusinggetPixel(RGB).ThemanipulatedImageDataisusedtocreateanImagetransparentIdeaImagethatnowhasthewhitepixelvaluespecifiedtobetransparent.

ImageDataideaData=newImageData(getClass().getResourceAsStream("Idea.jpg"));intwhitePixel=ideaData.palette.getPixel(newRGB(255,255,255));ideaData.transparentPixel=whitePixel;ImagetransparentIdeaImage=newImage(display,ideaData);

NextaShellusesthenewlycreatedimageinanativeLabelandalsointhepainteventofaCanvas.AWindowsLabeldoesnotsupportnativetransparencysoitstillappearswithawhitebackground,howevertheGCfortheCanvasusestheexistingbackgroundcolorwheneverawhitepixelisencounteredinthesourceimage,sotheimageappearsastransparent.

LabeltransparentIdeaLabel=newLabel(shell,SWT.NONE);transparentIdeaLabel.setImage(transparentIdeaImage);Canvascanvas=newCanvas(shell,SWT.NONE);canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEvente){e.gc.drawImage(transparentIdeaImage,0,0);}});



Ascanbeseenfromthesecondofthetwoimages(drawnontheCanvaswiththewhitepixelsettotransparent),therearestillsomepatchesofwhite.Closeranalysisrevealsthatthisisnotabug,butthattheseregionsarenotpurewhite(255,255,255),butareslighlyoff-white(suchas255,254,254).ThetransparentpixelofanImageDatacanonlybeusedforasinglevalue.Thisnowpresentsthenextproblemtobesolved-locatealloftheoff-whitepixelsintheImageDataandconvertthemtopure-white.Todothiswewilliterateovereachpixelintheimagedataandmodifythosethatareclosetowhitetobepurewhite.
ManipulatingImageDataBecausethegraphicIdea.jpgisntaswhiteaswedlikeit,welliterateoveritsimageDataandconverttheoff-whitepixelstopurewhiteandthenwellsavethisintoanewfileIdea_white.jpg.InpracticeitsunlikelythatyoudeverdothiskindofprogramminginSWT,howeveritsagoodexampletouseshowinghowImageDatacanbeanalyzedandmanipulated.
Thefirststepistoloadtheimageandtheniterateovereachpixelindividuallylookingatitscolor.BecauseIdea.jpgisusingadirectpalette,thepixelvalueisanintthatcontainsthered,greenandbluecomponentasmaskedbitareas.Thesemaskvaluecanbeobtainedfromthepalette.

ImageDataideaImageData=newImageData(getClass().getResourceAsStream("Idea.jpg"));intredMask=ideaImageData.palette.redMask;intblueMask=ideaImageData.palette.blueMask;intgreenMask=ideaImageData.palette.greenMask;

ForanypixelvaluewecanbitwiseANDitwiththemasktoseewhatthecolorcomponentis.Theredcomponentistheloworderbitssothiswillbetheactualvalue(from0to255),howeverthegreenandbluevaluesneedadjustingastheyarethehighorderbitsinthepixelvalue.Tomakethisadjustmentthecolorcomponentcanbebitshiftedtotherightusingthe>>operator.Ifyouarewritinggenericcodetodothiskindofmanipulating,takecarethatdirectpalettesforcolordepthsof24or32storetheircolorcomponentswithredbeingtheloworderbits,howeverforcolordepthof16thecolorsarereversedandredishighorderwithbluebeingloworder.ThereasonforthisistobethesameashowWindowsstoresimagesinternallysothereislessconversionwhencreatingtheimage.

TwoforloopswilliterateovertheimageData.Thefirstistraversingtheimagefromtoptobottomalineatatime,andcreatesanint[]toholdeachlineofdata.ThemethodImageData.getPixels(intx,inty,intgetWidth,int[]pixels,intstartIndex)isusedtoextractalineatatimefromtheimageDatasbytes.TheAPIforthismethodisslightlyirregular,becauseratherthanreturningtheresultingdataitinsteadisdeclaredasvoid,andtheresultingpixeldataisinjectedintotheint[]thatispassedinasamethodargument.Theint[]ofpixelsistheniteratedoverandeachvaluehasitsred,greenandbluecomponentextracted.Thedesiredeffectwewantistodeterminewhetherthepixelisoff-whiteandifsotomakeitpurewhite-arulethatworkswellistoassumethatanythingwhoseredandgreencomponentarehigherthan230andbluecomponenthigherthan150isanoff-white.

int[]lineData=newint[ideaImageData.width];for(inty=0;y<ideaImageData.height;y++){ideaImageData.getPixels(0,y,width,lineData,0);//Analyzeeachpixelvalueinthelinefor(intx=0;x<lineData.length;x++){//Extractthered,greenandbluecomponentintpixelValue=lineData[x];intr=pixelValue&redShift;intg=(pixelValue&greenShift)>>8;intb=(pixelValue&blueShift)>>16;if(r>230&&g>230&&b>150){ideaImageData.setPixel(x,y,0xFFFFFF);}}};

HavingmanipulatedtherawbytesmakinguptheImageDatawehavenowsuccessfullychangedtheoff-whitevaluestopurewhite.
SavingImagesNowthatwehavetheImageDatawhereallofthewhitishpixelshavebeenconvertedtowhite,andthetransparencypixelofthepalettehasbeensettobethecolorwhite,wellsavethisimagesothatnexttimeanSWTprogramneedsthepurewhiteJPFitcanjustloadthefileanduseitasis.TosaveImageDataintoafileusetheclassorg.eclipse.swt.graphics.ImageLoader.TheimageloaderhasapublicfielddatatypedtoImageData[].ThereasonthedatafieldisanarrayofImageDataistosupportimagefileformatswithmorethanoneframesuchasanimatedGIFsorinterlacedJPEGfiles.ThesearecoveredmoreintheAnimationsectionlater.
ImageLoaderimageLoader=newImageLoader();imageLoader.data=newImageData[]{ideaImageData};imageLoader.save("C:/temp/Idea_PureWhite.jpg",SWT.IMAGE_JPEG);

Thefinishedresultisshownbelow.



ItdoesntlookmuchdifferenttotheoriginalIdea.jpgbecauseitisdrawnonawhitebackground,butwhenitisdrawnonaCanvaswiththewhitepixelsettobethetransparentpixelthebackgroundshowsthroughachievingthedesiredeffect.

ImageDatapureWhiteIdeaImageData=newImageData("C:/temp/Idea_PureWhite.jpg");pureWhiteIdeaImageData.transparentPixel=pureWhiteIdeaImageData.palette.getPixel(newRGB(255,255,255));finalImagetransparentIdeaImage=newImage(display,pureWhiteIdeaImageData);Canvascanvas=newCanvas(shell,SWT.NONE);canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEvente){e.gc.drawImage(transparentIdeaImage,0,0);}});



ItmightseemoddthatintheabovecodethatafterloadingtheIdea_PureWhite.jpgfilethetransparentpixelwassettobewhite.WhynotsetthetransparentpixelbeforeweusedtheImageLoadertocreatethepersistedIdea_PureWhite.jpgfile?ThereasonisthattheJPEGimagefileformatdoesnotsupporttransparency.AGIFfilesupportsnativetransparency,howeverchangingthefiletypetoSWT.IMAGE_GIFontheImageLoaderwouldnothaveworked,becauseGIFsupportsamaximumimagedepthof8andusesanindexedpalette,whereastheJPEGhasanimagedepthof24andadirectpalette.ToconvertbetweenthetwoformatswouldrequireanalyzingthecoloursusedbytheJPEGtocreatethebestfit256colorpalette,beforeiteratingovereachJPEGpixelvalueandcreatingtheGIFimagedatabyfindingtheclosestcolor.Doingthisconversionisoutsidethescopeofthisarticle,althoughitcanbedonebymostcommercialgraphicstools.Tomatchpixelvaluesasthecolordepthdecreasesfrom24to8involvesalgorithmsthatfindtherightcolormatchforablockofpixelsratherthanasinglepixelvalue,andiswhyimagequalitycansometimesbereducedwhenswitchingbetweendifferentformats.

WehaveshownhowanImageDataisanarrayofintvaluesrepresentingeachpixelcoordinate,andhoweachpixelvalueismappedtoacolorthroughthepalette.ThisallowedustoiterateovertheimagedatafortheIdea.jpg,querypixelvaluesthatwereclosetowhite,andconvertthesetoapurewhiteRGBvalue.TheendresultofthiswasthatwewereabletocreatetheIdea_PureWhite.jpgfilethatcanbeusedasatransparentJPGbysettingthewhitepixeltobetransparent.Transparencyworksbyhavingasourcepixelvalue(theimagebeingdrawn),adestinationpixelvalue(theimagebeingdrawnonto)andarulebywhichtheresultingdestinationpixelvalueisdetermined.Fortransparencytheruleisthatthesourcepixelvalueisusedunlessitstransparentinwhichcasethedestinationpixelisused.Anothertechniqueistousealphavaluesthatspecifytheweightappliedtothesourcerelativetothedestinationtocreatethefinalpixelvalue.Thisallowstheblendingbetweenthesourceimageandtheexistingbackgrounditisbeingdrawnonto.
BlendingAlphablendingisatechniqueusedtomergetwopixelvalues,wherethesourceanddestinationpixeleachspecifyanalphavaluethatweightshowmuchtheywillaffectthefinaldestinationpixel.Analphavalueof255isfullweight,and0isnoweight.SWTsupportsasinglealphavaluefortheentireImageData,orelseeachpixelcanhaveitsownalphavalue.SinglealphavalueTheintfieldalphaValueofImageDataisusedtospecifyasinglevaluethatweightshowthesourcepixelsarecombinedwiththedestinationinputtocreatethedestinationoutput.ThelistingbelowshowshowthesameImageDatafortheIdea_PureWhite.jpgisusedforthreeseparateimages.Thefirstistheoriginal,thenanalphaof128isused,andfinallyanalphaof64.NotethatthesameImageDataiscontinuallymanipulatedbyhavingitsalphachangedbeforecreatingeachImage,andchangingtheImageDatahasnoaffectonImagesalreadyconstructedusingit.ThisisbecausetheImageispreparedfordisplayonthedevicefromtheImageDataatconstructiontime.
Shellshell=newShell(display);shell.setLayout(newFillLayout());ImageDataimageData=newImageData("C:/temp/Idea_PureWhite.jpg");finalImagefullImage=newImage(display,imageData);imageData.alpha=128;finalImagehalfImage=newImage(display,imageData);imageData.alpha=64;finalImagequarterImage=newImage(display,imageData);

Canvascanvas=newCanvas(shell,SWT.NO_REDRAW_RESIZE);canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEvente){e.gc.drawImage(fullImage,0,0);e.gc.drawImage(halfImage,140,0);e.gc.drawImage(quarterImage,280,0);}});


DifferentalphavalueperpixelAswellashavingasinglealphavaluethatappliedtoallpixelsinthesourceimage,ImageDataallowseachpixeltohaveitsownindividualalphavalue.Thisisdonewiththebyte[]fieldalphaData.Thisallowseffectstobeachieved,suchashavinganimagefadefromitstoptobottom.
ThefollowingcodecreatesanalphaDatabyte[],andthenhastwoloops.Theouterloopyisfrom0totheimageDatasheight,andtheinnerloopcreatesabyte[]forthewidthoftheimageDataandinitializesitwithavaluethatincreasesfrom0forthetoprowthroughto255forthebottomrow.ASystem.arrayCopythenbuildsupthealphaDatabyte[]witheachrow.

ImageDatafullImageData=newImageData("C:/temp/Idea_PureWhite.jpg");intwidth=fullImageData.width;intheight=fullImageData.height;byte[]alphaData=newbyte[height*width];for(inty=0;y<height;y++){byte[]alphaRow=newbyte[width];for(intx=0;x<width;x++){alphaRow[x]=(byte)((255*y)/height);}System.arraycopy(alphaRow,0,alphaData,y*width,width);}fullImageData.alphaData=alphaData;ImagefullImage=newImage(display,fullImageData);

Theresultingimageisshownbelow,andthealphaDatabyte[]makesthetopoftheimagetransparentandthebottomopaque,withagradualfadingbetweenthetwo.


ImageeffectsAswellasarbitraryimageeffectsthatcanbeachievedbymanipulatingimagedata,SWTprovidesanumberofpre-definedwaysofcreatingnewimagesbasedonexistingimagescombinedwithcertainstyles.Thisisusedif,forexample,youhaveanimagebeingusedonatoolbarbuttonandyouwishtocreateaversionthatcanbeusedtoindicatethatthebuttonisdisabledorthatitisinactive.
TocreateaneffectbasedonanexistingimageandastyleflagusetheconstructorImage(Displaydisplay,Imageimage,intflag).TheflagargumentisastaticconstantofeitherSWT.IMAGE_COPY,SWT.IMAGE_DISABLEorSWT.IMAGE_GRAY.CopycreatesanewimagebasedontheoriginalbutwithacopyofitsimageData,whereasDisableandGraycreateanewimageapplyingplatformspecificeffects.ThefollowingcodeshowstheIdea.jpg,togetherwiththreemoreimagesthatwecreatedusingthestylebitsIMAGE_DISABLE,IMAGE_GRAYandIMAGE_COPY.ToshowthatIMAGE_COPYcreatesanewimageaGCisusedtodrawontoitthataffectsonlythecopiedimage,nottheoriginal.

ImageideaImage=newImage(display,getClass().getResourceAsStream("/icons/Idea.jpg");ImagedisabledImage=newImage(display,image,SWT.IMAGE_DISABLE);ImagegrayImage=newImage(display,image,SWT.IMAGE_GRAY);ImagecopyImage=newImage(display,ideaImage,SWT.IMAGE_COPY);GCgc=newGC(copyImage);gc.drawText("Thisisacopy",0,0);gc.dispose();
GIFAnimationAnotherimportanttopicforimagesisunderstandinganimationwhereanimagecancontainanumberofframesthatareanimatedinsequence.ImageanimationissupportedbyGIFimages,whereasingleimagefilecancontainmultiplesetsofImageData.Webbrowserssupportnativeanimation,andthefollowingimageismadeupof15framesshowingthepenrotatingandwritingthewordsSWTbeneaththeIdealogo.


WhilethewebbrowseryoureusingtoreadthisarticleshouldshowtheIdea_SWT_Animation.giffileasasequencewiththemovingpen,thisisnottrueofnativeSWTcontrolsdisplayingthegraphic.Theanimationmustbedoneprogrammatically,andtheclassorg.eclipse.swt.examples.ImageAnalyzershowshowthiscanbeachieved.TheImageAnalyzerclasscanbeobtainedfromtheSWTexamplesprojectintheEclipseCVSrepository.

WhenananimatedGIFisloadedbytheImageLoaderclass,eachindividualframeisaseparateelementinthedatafieldarraytypedtoImageData[].IntheanimationsequenceeachImageDatarecordshowmanymillisecondsitshouldbedisplayedforintheintfielddelayTime.Thenumberoftimesthesequenceshouldrepeatcanberetrievedfromthefieldloader.repeatCount,avalueof-1indicatesthattheanimationshouldrepeatindefinitelyandIdea_SWT_Animation.gifhasthisvalue.Whenswitchingfromoneframetothenexttherearethreewaysthatthenewframecanreplacethepreviousone,specifiedbytheintfieldImageData.disposalMethod.Thiscantakethefollowingvaluesdefinedintheconstantclassorg.eclipse.swt.SWT.DM_FILL_NONELeavethepreviousimageinplaceandjustdrawtheimageontop.Eachframeaddstothepreviousone.DM_FILL_BACKGROUNDFillwiththebackgroundcolorbeforepaintingeachframe.Thepixelvalueforthisisdefinedinthefieldloader.backgroundPixelDM_FILL_PREVIOUSRestorethepreviouspictureDM_FILL_UNSPECIFIEDNodisposalmethodhasbeendefined

Toconserveonspace,animatedGIFsaregenerallyoptimizedtojuststorethedeltathatneedstobeappliedtothepreviousimage.IntheIdea_SWT_Animation.gifabovethe15framesareshownbelow.Eachframestoresadeltaagainstthepreviousimage,andthiswasautomaticallygeneratedbythetoolIcreatetheanimatedGIFwith.ThedisposalmethodforeachframeisDM_NONEsotheeachimageshouldbedrawnontopofthepreviousone.EachindividualImageDataelementhasthexandyforitstopleftcorner,aswellasitswidthandheight.Theoverallsizetousecanbeobtainedfromthefieldsloader.logicalScreenWidthandloader.logicalScreenHeight.



ToillustratehowtodisplayananimatedGIFinSWTwellcreateaninitialImagefromthefirstframeandacountertostorewhichframeisbeingdisplayed.TheimageisdrawnapainteventonaCanvas,andaGCiscreatedthatwillbeusedtodrawthesubsequentframesontotheimage.

ImageLoaderloader=newImageLoader();loader.load(getClass().getResourceAsStream("Idea_SWT_Animation.gif"));Canvascanvas=newCanvas(shell,SWT.NONE);image=newImage(display,loader.data[0]);intimageNumber;finalGCgc=newGC(image);canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEventevent){event.gc.drawImage(image,0,0);}});

Thebodyoftheexamplewillcreateathreadthatiteratesthrougheachframe,waitinguntilthedelayTimehaspassed.ForeachframetheImageDataisretrievedfromtheloaderandatemporaryImagecreated.Thisisthendrawnontotheimagebeingdisplayedonthecanvas,atthexandypositionspecifiedbytheframesImageData.BecausewecreatedthetemporaryframeImagewemustdisposeitwhenitsnolongerbeingusedtofreeuptheunderlyingresource.

Threadthread=newThread(){publicvoidrun(){longcurrentTime=System.currentTimeMillis();intdelayTime=loader.data[imageNumber].delayTime;while(currentTime+delayTime*10>System.currentTimeMillis()){//Waittillthedelaytimehaspassed}display.asyncExec(newRunnable(){publicvoidrun(){//IncreasethevariableholdingtheframenumberimageNumber=imageNumber==loader.data.length-1?0:imageNumber+1;//DrawthenewdataontotheimageImageDatanextFrameData=loader.data[imageNumber];ImageframeImage=newImage(display,nextFrameData);gc.drawImage(frameImage,nextFrameData.x,nextFrameData.y);frameImage.dispose();canvas.redraw();}});}};shell.open();thread.start();
ScalingIntheexamplessofarwehaveloadedanimagefromafileanddrawnitontheGUIatitsoriginalsize.Therearetimeswhenthiswillnotalwaysbethecaseandyouneedtostretchorshrinktheimage,andtherearetwowaystodoachievethis.ThefirstistousetheGCtostretchandclipit,usingGC.drawImage(Imageimage,intsrcX,intsrcY,intsrcWidth,intsrcHeight,intdstX,intdstY,intdstWidth,intdstHeight),andthesecondistouseImageData.scaledTo(intwidth,intheight)tocreateanewImageDataobjectbasedonscalingthereceiver.
ThefollowingcodeloadstheIdea.jpgimage,andscalesthisto1/2and2timesitsoriginalsizeusingtheImageData.scaledTo(intwidth,intheight).TheimageisalsoresizedusingGC.drawImage(...),andtheexampleshowstwowaystoachievethis.Thefirsttechniqueistospecifythenewwidthandheightaspartthepaintevent.Thisispotentiallyinefficientbecausethescalingmustbedoneeachtimethecanvasrepaintsitself.Amoreoptimizedtechniqueistocreateanimageatthefinaldesiredsize,constructaGCoverthethisandthenpaintontoitsoapermanentscaledimageexistsintheprogram.

Theendresultisshownbelow,andbothtechniquesproducealmostidenticalresults.

finalImageimage=newImage(display,getClass(),getResourceAsStream("Idea.jpg"));finalintwidth=image.getBounds().width;finalintheight=image.getBounds().height;

finalImagescaled050=newImage(display,image.getImageData().scaledTo((int)(width*0.5),(int)(height*0.5)));finalImagescaled200=newImage(display,image.getImageData().scaledTo((int)(width*2),(int)(height*2)));

finalImagescaledGC200=newImage(display,(int)(width*2),(int)(height*2));GCgc=newGC(scaledGC200);gc.drawImage(image,0,0,width,height,0,0,width*2,height*2);gc.dispose();

canvas.addPaintListener(newPaintListener(){publicvoidpaintControl(PaintEvente){e.gc.drawImage(image,0,0,width,height,0,0,(int)(width*0.5),(int)(height*0.5));e.gc.drawImage(scaled050,100,0);e.gc.drawImage(scaledGC200,0,75);e.gc.drawImage(scaled200,225,175);}});



WhentouseGCscaling,andwhentouseImageData.scaledTo(...),dependsontheparticularscenario.TheGCscalingisfasterbecauseitisnative,howeveritdoesassumethatyouhaveaGCandanImagetoworkwith.UsingjusttheImageDatameansthatyoudontneedtohavepreparedanImage(thatrequiresanativeresourceandrequiresdisposing),andanImageDatacanbeloadeddirectlyfromagraphicfile(usingtheconstructorImageData(StringfileName)orImageData(InputStreamstream)).ByusingrawImageDatayouaredelayingthepointatwhichyouwillneednativedisplayresources,howeveryouwilleventuallyneedtocreateanImagefromthescaledImageDatabeforeitcanberenderedontoadevice.
CursorThefinalsectionofthisarticlecoverstheclassorg.eclipse.swt.graphics.Cursorresponsibleformanagingtheoperatingsystemresourceassociatedwiththemousepointer.Thereasoncursorsarecoveredinanarticleonimagesisbecauseyoucancreatearbitrarycursorsfromimages,andtheyillustratehowimagemaskswork.
Cursorscanbecreatedintwoways,eitherfromapre-definedstyleorusingsourceandmaskimages.
PlatformcursorsThelistofpre-definedstylesareSWTconstantsshownbelow,togetherwithsampleimagesalthoughthesewillvarydependingontheoperatingsystemandplatformsettings.CURSOR_APPSTARTINGCURSOR_IBEAMCURSOR_SIZENECURSOR_ARROWCURSOR_NOCURSOR_SIZENESWCURSOR_CROSSCURSOR_SIZEALLCURSOR_SIZENSCURSOR_HANDCURSOR_SIZEECURSOR_SIZENWCURSOR_HELPCURSOR_SIZENCURSOR_SIZESNWSECURSOR_SIZESCURSOR_SIZESECURSOR_SIZESWCURSOR_SIZEWECURSOR_UPARROWCURSOR_WAIT
EveryControlcanhaveacursorassociatedwithit,andwhenthemousepointermovesoverthecontrolitchangestothespecifiedcursor.Changingacursoralsoaffectsanychildcontrols,soifyouupdatethecursoronaShellthisaffectsthemousepointerforanywhereontheshell,althoughifthechildcontrolitselfhasanexplicitcursor,orusesitsowncursorsuchasanIbeanforTextorCombo,thistakesprecedenceovertheparentsdefinedcursor.Thefollowingcodeillustratesthis,bychangingtheshellscursortobehandcursor,andthelistscursortoacross.Whenthemouseisovertheshell(oritschildButtonthathasnoexplicitcursor)itisahand,andwhenitisoverthelistitisacross.

Listlist=newList(shell,SWT.BORDER);Buttonbutton=newButton(shell,SWT.NONE);button.setText("Button");CursorhandCursor=newCursor(display,SWT.CURSOR_HAND);shell.setCursor(handCursor);CursorcrossCursor=newCursor(display,SWT.CURSOR_CROSS);list.setCursor(crossCursor);



Cursorsuseunderlyingnativeresourcesandshouldbedisposedwhentheyarenolongerrequired.IntheabovecodethiswouldbewhentheshellhasbeendisposedandtherearenoremainingcontrolsusingeitherthehandCursororcrossCursorfields.
CustomcursorsAswellasusingapre-definedstyle,acursorcanbecreatedfromimagesusingtheconstructorCursor(Devicedevice,ImageDatasource,ImageDatamask,inthotspotx,inthotspoty).Thesourceimagedataargumentisthegraphicforthecursorshape,andthemaskisusedtospecifytransparency.Thefollowingexampleshowshowtocreateamonochromecustomcursor,wherethethesourceandmaskimagedatahaveacolordepthof1andindexedpaletteswithtwocolors.TheImageData’sheightandwidthshouldbenolargerthan32anditdoesnotnecessarilyhavetobesquare,althoughthemaskandsourceshouldbethesamesize.Thehotspotisthepointonthecursorthatrepresentsthepreciselocationofthemousepointer.
Thesourceandmaskimagedatapixelsarecombinedtodeterminewhetherthecursorpixelshouldbewhite,blackortransparent.ImageMaskCursorcolor10Transparent00Black11Black01White

TheImageDatacanbeloadedfromfiles,andEclipseitselfdoesthisforsomeofitsdraganddropcursorsdefinedinorg.eclipse.ui/icons/full/dnd.Youcanalsodirectlycreateandmanipulatetheimagedatawithinyourprogram.Thecodesamplebelowcreatesanindexedpalettewithtwocolors.ThesourceandmaskImageDataare32by32withacolordepthof1.Theint[]forthesourceandmaskdefineanuparrow,the0sforthesourceand1sforthemaskareshowninboldtoshowhowthearrowisdefinedwiththearrays.0and1makeswhitewhichisthecenterofthearrow,1and1isblackfortheedgeofthearrow,and1and0transparentfortheremainder.Thetipofthearrowis16,3sothisismadethecursorhotspotwhenitiscreated.

PaletteDatapaletteData=newPaletteData(newRGB[]{newRGB(0,0,0),newRGB(255,255,255)});ImageDatasourceData=newImageData(32,32,1,paletteData);ImageDatamaskData=newImageData(32,32,1,paletteData);

int[]cursorSource=newint[]{1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0,1,1,1,1,1,1,1,1,1,1,1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0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,};

sourceData.setPixels(0,0,1024,cursorSource,0);maskData.setPixels(0,0,1024,cursorMask,0);Cursorcursor=newCursor(display,sourceData,maskData,16,3);shell.setCursor(cursor);



Tokeepthecodelistingsnarrowtheabovesampleusedanint[]todefinethesourceandmaskimageData.Abyteuseslessmemorythananunsignedint,sowhencreatingcustomcursorsitismoreefficienttouseabyte[]instead,suchas:

byte[]cursorSource=newbyte[]{(byte)0x00,(byte)0x00,(byte)0x01,(byte)0x01,(byte)0x00,//etc...

Customcursorsneedtobedisposedinjustthesamewayaspre-definedsystemcursors,sowhenthereisnoremainingcontrolusingthecursorismustbesendthemethoddispose()tofreeuptheunderlyingnativeresource.

Althoughtheaboveexampleisforamonochromecursor,Eclipse3.0supportscolorcursorsonplatformsthatallowit(suchasWindows).TwoSWTcodesnippetsshowinghowtodothisarehere:snippet1,snippet2.
ConclusionThisarticlehasshowedhowtocreateandmanipulateSWTimages.UnderlyingeachImageisitsImageDatathatrecordsthepixelvalueforeachcoordinate,andthepalettethatmapsthistoaspecificcolor.ByunderstandingImageDataitispossibletoachieveeffectssuchastransparency,alphablending,animation,aswellascustomizedcursors.
JavaandallJava-basedtrademarksandlogosaretrademarksorregisteredtrademarksofSunMicrosystems,Inc.intheUnitedStates,othercountries,orboth.

WindowsisatrademarkofMicrosoftcorporationintheUnitedStates,othercountries,orboth.

Othercompany,product,andservicenamesmaybetrademarksorservicemarksofothers.

唉!都是钱闹的1.Swing和.net开发比较------从市场份额看.net开发主要占据大部分的中小型和中型的的桌面开发,原因是它封装了很多工具
莫相离 该用户已被删除
沙发
发表于 2015-1-18 17:48:50 | 只看该作者
是一种使网页(Web Page)由静态(Static)转变为动态(Dynamic)的语言
板凳
发表于 2015-1-22 17:50:49 | 只看该作者
是一种将安全性(Security)列为第一优先考虑的语言
透明 该用户已被删除
地板
发表于 2015-1-31 07:54:46 | 只看该作者
是一种语言,用以产生「小应用程序(Applet(s))
若天明 该用户已被删除
5#
发表于 2015-2-6 18:32:47 | 只看该作者
是一种为 Internet发展的计算机语言
乐观 该用户已被删除
6#
发表于 2015-2-14 07:19:57 | 只看该作者
所以现在应用最广泛又最好学的就是J2EE了。 J2EE又包括许多组件,如Jsp,Servlet,JavaBean,EJB,JDBC,JavaMail等。要学习起来可不是一两天的事。那么又该如何学习J2EE呢?当然Java语法得先看一看的,I/O包,Util包,Lang包你都熟悉了吗?然后再从JSP学起。
分手快乐 该用户已被删除
7#
发表于 2015-3-4 05:02:55 | 只看该作者
多重继承(以接口取代)等特性,增加了垃圾回收器功能用于回收不再被引用的对象所占据的内存空间,使得程序员不用再为内存管理而担忧。在 Java 1.5 版本中,Java 又引入了泛型编程(Generic Programming)、类型安全的枚举、不定长参数和自动装/拆箱等语言特性。
简单生活 该用户已被删除
8#
发表于 2015-3-8 13:35:03 | 只看该作者
[url]http://www.jdon.com/[/url]去下载,或到同济技术论坛的服务器[url]ftp://nro.shtdu.edu.cn[/url]去下,安装上有什么问题,可以到论坛上去提问。
深爱那片海 该用户已被删除
9#
发表于 2015-3-15 23:37:38 | 只看该作者
你现在最缺的是实际的工作经验,而不是书本上那些凭空想出来的程序。
谁可相欹 该用户已被删除
10#
发表于 2015-3-18 12:03:14 | 只看该作者
一直感觉JAVA很大,很杂,找不到学习方向,前两天在网上找到了这篇文章,感觉不错,给没有方向的我指了一个方向,先不管对不对,做下来再说。
金色的骷髅 该用户已被删除
11#
发表于 2015-3-21 12:50:57 | 只看该作者
是一种简化的C++语言 是一种安全的语言,具有阻绝计算机病毒传输的功能
山那边是海 该用户已被删除
12#
发表于 2015-4-3 23:03:24 | 只看该作者
一般学编程语言都是从C语开始学的,我也不例外,但还是可能不学过程语言而直接学面向对象语言的,你是刚接触语言,还是从C开始学比较好,基础会很深点,如果你直接学习JAVA也能上手,一般大家在学语言的时候都记一些语言的关键词,常有的包和接口等。再去做逻辑代码的编写,以后的学习过程都是从逻辑代码编写中提升的,所以这方面都是经验积累的。你要开始学习就从
海妖 该用户已被删除
13#
发表于 2015-4-13 01:39:03 | 只看该作者
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
飘飘悠悠 该用户已被删除
14#
发表于 2015-4-26 14:30:47 | 只看该作者
吧,现在很流行的Structs就是它的一种实现方式,不过Structs用起来实在是很繁,我们只要学习其精髓即可,我们完全可以设计自己的MVC结构。然后你再研究一下软件Refactoring (重构)和极限XP编程,相信你又会上一个台阶。 做完这些,你不如整理一下你的Java代码,把那些经典的程序和常见的应用整理出来,再精心打造一番,提高其重用性和可扩展性。你再找几个志同道合的朋友成立一个工作室吧
admin 该用户已被删除
15#
发表于 2015-5-4 07:25:39 | 只看该作者
接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。
灵魂腐蚀 该用户已被删除
16#
发表于 2015-5-10 03:51:08 | 只看该作者
接着就是EJB了,EJB就是Enterprise JavaBean, 看名字好象它是Javabean,可是它和Javabean还是有区别的。它是一个体系结构,你可以搭建更安全、更稳定的企业应用。它的大量代码已由中间件(也就是我们常听到的 Weblogic,Websphere这些J2EE服务器)完成了,所以我们要做的程序代码量很少,大部分工作都在设计和配置中间件上。
蒙在股里 该用户已被删除
17#
发表于 2015-6-5 10:03:04 | 只看该作者
是一种为 Internet发展的计算机语言
柔情似水 该用户已被删除
18#
发表于 2015-6-9 22:09:24 | 只看该作者
是一种突破用户端机器环境和CPU
愤怒的大鸟 该用户已被删除
19#
发表于 2015-6-19 09:52:46 | 只看该作者
应用在电视机、电话、闹钟、烤面包机等家用电器的控制和通信。由于这些智能化家电的市场需求没有预期的高,Sun公司放弃了该项计划。随着1990年代互联网的发展
爱飞 该用户已被删除
20#
发表于 2015-6-19 21:59:12 | 只看该作者
《JAVA语言程序设计》或《JAVA从入门到精通》这两本书开始学,等你编程有感觉的时候也可以回看一下。《JAVA读书笔记》这本书,因为讲的代码很多,也很容易看懂,涉及到面也到位。是你学习技术巩固的好书,学完后就看看《JAVA编程思想》这本书,找找一个自己写的代码跟书上的代码有什么不一样。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-12-23 02:41

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表