Thursday, 15 September 2011

java - Using mapTree collection and need more colors -



java - Using mapTree collection and need more colors -

this project in school have 2 different questions advice on please. here requirements teacher:

"use java collections store pairs of unique colors , unique hexadecimal values. store 20 of these pairs. write gui displays color and/or hexadecimal values using radio buttons select value. when selected background of gui should alter color."

i read mean store them pairs create gui uses jradiobuttons alter color of background.

i have done , working fine. issue sense there should more efficient way create jrb treemap. thinking along lines of calling next set fill jrb, similar using array maybe.

second part colors themselves. see limited amount of colors in netbeans (i.e. no purple!) how can these colors included in project accuracy?

not sure advise much appreciated.

import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.hashmap; import java.util.map; import java.util.treemap; import javax.swing.border.etchedborder; import javax.swing.border.titledborder; public final class finalproject extends jframe { private static final int frame_width = 700; private static final int frame_height = 800; private jradiobutton redbutton; private jradiobutton greenbutton; private jradiobutton bluebutton; private jradiobutton aquabutton; private jradiobutton graybutton; private jradiobutton fuchsiabutton; private jradiobutton blackbutton; private jradiobutton limebutton; private jradiobutton silverbutton; private jradiobutton maroonbutton; private jradiobutton navybutton; private jradiobutton olivebutton; private jradiobutton purplebutton; private jradiobutton tealbutton; private jradiobutton yellowbutton; private jradiobutton cyanbutton; private jradiobutton orangebutton; private jradiobutton tanbutton; private jradiobutton lavanderbutton; private jradiobutton plumbutton; private actionlistener listener; public finalproject() { treemap<string,string> colorselect = new treemap<string,string>(); colorselect.put("00ffff","aqua"); colorselect.put("008000","green"); colorselect.put("808080","gray"); colorselect.put("ff00ff","fuchaia"); colorselect.put("0000ff","blue"); colorselect.put("000000","black"); colorselect.put("00ff00","lime"); colorselect.put("c0c0c0","silver"); colorselect.put("800000","maroon"); colorselect.put("000080","olive"); colorselect.put("ff0000","red"); colorselect.put("800080","purple"); colorselect.put("008080","teal"); colorselect.put("ffff00","yellow"); colorselect.put("00ffff","cyan"); colorselect.put("ffa500","orange"); colorselect.put("d2b4bc","tan"); colorselect.put("e6e6fa","lavander"); colorselect.put("dda0dd","plum"); // listener shared among components class choicelistener implements actionlistener { @override public void actionperformed(actionevent event) { setcolor(); } } listener = new choicelistener(); createcontrolpanel(); setsize(frame_width, frame_height); } public void createcontrolpanel() { jpanel stylegrouppanel = createradiobuttons(); jpanel controlpanel = new jpanel(); controlpanel.setlayout(new gridlayout()); controlpanel.add(stylegrouppanel); add(controlpanel, borderlayout.north); } public jpanel createradiobuttons() { redbutton = new jradiobutton("red"); redbutton.addactionlistener(listener); greenbutton = new jradiobutton("green"); greenbutton.addactionlistener(listener); aquabutton = new jradiobutton("aqua"); aquabutton.addactionlistener(listener); graybutton = new jradiobutton("gray"); graybutton.addactionlistener(listener); fuchsiabutton = new jradiobutton("fuchsia"); fuchsiabutton.addactionlistener(listener); blackbutton = new jradiobutton("black"); blackbutton.addactionlistener(listener); limebutton = new jradiobutton("lime"); limebutton.addactionlistener(listener); silverbutton = new jradiobutton("silver"); silverbutton.addactionlistener(listener); maroonbutton = new jradiobutton("maroon"); maroonbutton.addactionlistener(listener); navybutton = new jradiobutton("navy"); navybutton.addactionlistener(listener); olivebutton = new jradiobutton("olive"); olivebutton.addactionlistener(listener); purplebutton = new jradiobutton("purple"); purplebutton.addactionlistener(listener); tealbutton = new jradiobutton("teal"); tealbutton.addactionlistener(listener); yellowbutton = new jradiobutton("yellow"); yellowbutton.addactionlistener(listener); cyanbutton = new jradiobutton("cyan"); cyanbutton.addactionlistener(listener); orangebutton = new jradiobutton("orange"); orangebutton.addactionlistener(listener); tanbutton = new jradiobutton("tan"); tanbutton.addactionlistener(listener); lavanderbutton = new jradiobutton("lavander"); lavanderbutton.addactionlistener(listener); plumbutton = new jradiobutton("plum"); plumbutton.addactionlistener(listener); bluebutton = new jradiobutton("blue"); bluebutton.addactionlistener(listener); bluebutton.setselected(true); getcontentpane().setbackground(color.blue); buttongroup grouping = new buttongroup(); group.add(redbutton); group.add(greenbutton); group.add(bluebutton); group.add(plumbutton); group.add(aquabutton); group.add(graybutton); group.add(fuchsiabutton); group.add(blackbutton); group.add(limebutton); group.add(silverbutton); group.add(maroonbutton); group.add(navybutton); group.add(olivebutton); group.add(purplebutton); group.add(tealbutton); group.add(yellowbutton); group.add(cyanbutton); group.add(orangebutton); group.add(tanbutton); group.add(lavanderbutton); jpanel panel = new jpanel(); panel.add(redbutton); panel.add(greenbutton); panel.add(bluebutton); panel.add(aquabutton); panel.add(graybutton); panel.add(fuchsiabutton); panel.add(blackbutton); panel.add(limebutton); panel.add(silverbutton); panel.add(maroonbutton); panel.add(navybutton); panel.add(olivebutton); panel.add(purplebutton); panel.add(tealbutton); panel.add(yellowbutton); panel.add(cyanbutton); panel.add(orangebutton); panel.add(tanbutton); panel.add(lavanderbutton); panel.add(plumbutton); panel.setpreferredsize(new dimension(100,200)); homecoming panel; } public void setcolor() { if(redbutton.isselected()) { getcontentpane().setbackground(color.red); } if(greenbutton.isselected()) { getcontentpane().setbackground(color.green); } if(bluebutton.isselected()) { getcontentpane().setbackground(color.blue); } if(aquabutton.isselected()) { getcontentpane().setbackground(color.green); } if(graybutton.isselected()) { getcontentpane().setbackground(color.gray); } if(fuchsiabutton.isselected()) { getcontentpane().setbackground(color.pink); } if(blackbutton.isselected()) { getcontentpane().setbackground(color.black); } if(limebutton.isselected()) { getcontentpane().setbackground(color.green); } if(silverbutton.isselected()) { getcontentpane().setbackground(color.light_gray); } if(maroonbutton.isselected()) { getcontentpane().setbackground(color.red); } if(navybutton.isselected()) { getcontentpane().setbackground(color.blue); } if(olivebutton.isselected()) { getcontentpane().setbackground(color.green); } if(purplebutton.isselected()) { getcontentpane().setbackground(color.magenta); } if(tealbutton.isselected()) { getcontentpane().setbackground(color.blue); } if(yellowbutton.isselected()) { getcontentpane().setbackground(color.yellow); } if(cyanbutton.isselected()) { getcontentpane().setbackground(color.cyan); } if(orangebutton.isselected()) { getcontentpane().setbackground(color.orange); } if(tanbutton.isselected()) { getcontentpane().setbackground(color.darkgray); } if(lavanderbutton.isselected()) { getcontentpane().setbackground(color.magenta); } if(plumbutton.isselected()) { getcontentpane().setbackground(color.magenta); } } }

first, think misinterpreting teacher's instructions. think when says "store 20 pairs" means programme should flexible plenty take 20 pairs, not 20 pairs. , implies programme not know colors in advance. also, when says "unique", believe means have utilize java collections create sure colors , values unique.

and did not intend fill collection without using it. thought utilize collection build gui.

be may, creating separate variable each color button , going through list of if statement not programming practice. whenever encounter big grouping of similar items, on have perform similar actions, should cue utilize collection/array , loop rather write same code on , over. if don't know if need utilize 10 colors or 20.

so:

decide on way input colors programme - command line, file, or whatever. decide on collections help maintain both color name , color value unique. write loop reads input, checking uniqueness, , stopping @ 20 or when info done. when have colors in collections, utilize iteration create buttons , add together them button grouping , panel.

even if decide don't want work hard on doing input, @ to the lowest degree set colors in static array can alter it, add together , remove colors, , create sure read array collections using loop , not 1 1 - if getting kind of input source.

the of import part have 1 place alter data. in way implemented this, if decide utilize maroon instead of teal, have go through many parts of code, teal, delete , utilize maroon instead.

now, actionlistener. again, if write dozens of similar if statements, on wrong path. here, missing fact have reference actual selected button within actionevent passed actionperformed. can utilize reference know color use. example, when create button, if set name of button color, can use

object eventsource = event.getsource(); string colorname; if ( eventsource instanceof jradiobutton ) { colorname = ((jradiobutton)eventsource).getname(); // color value collection, build color , alter background. }

there other options using map<jradiobutton,color>. when create radio buttons, create associated color , set pair in map. in actionperformed can eventsource got in map, , color object.

java user-interface collections map colors

No comments:

Post a Comment