switching string/implementing symbol actions


full disclaimer: i'm cs student, doubt associated recently indifferent java way object-oriented programming. nonetheless we've finished console stuff, initial we've worked gui representation awt. given formula combined window calm symbol rotated by opposite colors text. following asked cgange way emanate radio buttons colors insteadthis also commanded give us use researching an api. i've already handed assignment viewed assent instructor post formula here.



what's best proceed exercise symbol actions java? after fiddling around, i combined buttons this:



class hellocomponent3 extends jcomponent
implements mousemotionlistener, actionlistener
{
int messagex = 75, messagey= 175;

twine themessage;
twine redstring = "red", bluestring = "blue", greenstring = "green";
twine magentastring = "magenta", blackstring = "black", resetstring = "reset";

jbutton resetbutton;
jradiobutton redbutton, bluebutton, greenbutton, magentabutton, blackbutton;
buttongroup colorbuttons;

open hellocomponent3(string message) {

themessage = message;

//intialize reset button
resetbutton = new jbutton("reset");
resetbutton.setactioncommand(resetstring);
resetbutton.addactionlistener(this);

//intialize the radio buttons actions labels
redbutton = new jradiobutton("red");
redbutton.setactioncommand(redstring);
...


and total movement listeners...



redbutton.addactionlistener(this);
bluebutton.addactionlistener(this);
...


a stub already combined actionperformed slight give us an thought it, nonetheless given there wholly unparalleled symbol template, wasn't pristine exercise churned buttons. i attempted switching string, nonetheless fast satisfied that, given twine isn't obsolete type, i couldn't switch statement. i have surrogate an if-else chain, nonetheless i came adult instead. seems distant elegant, there contingency improved way. there is, it? there proceed switch string? select an movement some-more scaleable fashion?



public vacant actionperformed(actionevent e){

(e.getactioncommand().equals(resetstring)) {
messagex = 75; messagey = 175;
setforeground(color.black);
blackbutton.setselected(true);
repaint();
return;
}

( e.getactioncommand().equals(redstring) ) {
setforeground(color.red);
repaint();
return;
}

( e.getactioncommand().equals(bluestring) ) {
setforeground(color.blue);
repaint();
return;
}

( e.getactioncommand().equals(greenstring) ) {
setforeground(color.green);
repaint();
return;
}

( e.getactioncommand().equals(magentastring) ) {
setforeground(color.magenta);
repaint();
return;
}

( e.getactioncommand().equals(blackstring) ) {
setforeground(color.black);
repaint();
return;
}
}


Comments

Popular posts from this blog

list macos calm editors formula editors

how hibernate @any-related annotations?

using mvc, should hoop communication between views? between models?