Join 150,429 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,083 people online right now. Registration is fast and FREE... Join Now!
I made a picture of Mondrian. Now I have 3 buttons, going to three methods. In those 3 methods, the picture of Mondrian must change to different colors. Lets say in One method I would have to change one box that is blue to green, and so on the other boxes. My teacher took of one point for duplicate code in the the methods (9/10).
MY CODE:
CODE
import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * Class MondrianPictureOnly - write a description of the class here * * @author (your name) * @version (a version number) */ public class MondrianPictureOnly extends JApplet implements ActionListener { private JButton choice1Button, choice2Button, choice3Button, intialButton; private Canvas drawSpace; private JPanel choicePanel; public void init() { choice1Button = new JButton("Choice One"); choice2Button= new JButton("Choice Two"); choice3Button = new JButton("Choice Three"); intialButton = new JButton("Click here First"); intialButton.addActionListener(this); choice1Button.addActionListener(this); choice2Button.addActionListener(this); choice3Button.addActionListener(this);
Container window = getContentPane();//window //window.setLayout( new FlowLayout()); BorderLayout layout = new BorderLayout();
choicePanel = new JPanel(); choicePanel.setLayout(new FlowLayout()); choicePanel.add(intialButton); choicePanel.add(choice1Button); choicePanel.add(choice2Button); choicePanel.add(choice3Button);
window.add(choicePanel, BorderLayout.NORTH); drawSpace = new Canvas (); drawSpace.setBackground(Color.black); window.add(drawSpace, BorderLayout.CENTER); } public void actionPerformed( ActionEvent ae){
import java.awt.event.*; import java.awt.*; import javax.swing.*; /** * Class MondrianPictureOnly - write a description of the class here * * @author (your name) * @version (a version number) */ public class MondrianPictureOnly extends JApplet implements ActionListener { private JButton choice1Button, choice2Button, choice3Button, intialButton; private Canvas drawSpace; private JPanel choicePanel; Graphics g; public void init()
{
choice1Button = new JButton("Choice One"); choice2Button= new JButton("Choice Two"); choice3Button = new JButton("Choice Three"); intialButton = new JButton("Click here First"); intialButton.addActionListener(this); choice1Button.addActionListener(this); choice2Button.addActionListener(this); choice3Button.addActionListener(this);
Container window = getContentPane();//window //window.setLayout( new FlowLayout()); BorderLayout layout = new BorderLayout();
choicePanel = new JPanel(); choicePanel.setLayout(new FlowLayout()); choicePanel.add(intialButton); choicePanel.add(choice1Button); choicePanel.add(choice2Button); choicePanel.add(choice3Button);
window.add(choicePanel, BorderLayout.NORTH); drawSpace = new Canvas (); drawSpace.setBackground(Color.black); window.add(drawSpace, BorderLayout.CENTER); } public void actionPerformed( ActionEvent ae){
I made a picture of Mondrian. Now I have 3 buttons, going to three methods. In those 3 methods, the picture of Mondrian must change to different colors. Lets say in One method I would have to change one box that is blue to green, and so on the other boxes. My teacher took of one point for duplicate code in the the methods (9/10).
I guess he's right an probably would have like something like that:
To build on what hallizh is showing you, it is the process known as "refactoring" and it is a trick you will want to learn well because you will need to use it a lot. What you do is first identify a few places that look like they are doing almost the exact thing (maybe with a few minor variations) like your three function. Then you create a function which basically abstracts it, like a template where the slight variations would be the variables you pass in. Like the the only difference between your three functions is the color you use for setColor. So you would pass in the colors to the function and replace the variations with your variables. Now everywhere you use one of the three functions you would just call one function instead.
What this does is reduces 30 lines that are roughly the same and boils it down into one function that is 10 lines and may variate ever so slightly.
It reduces lines, it reduces complexity, it increases speed, and it makes it easier to maintain because now you just need to change one line in one function instead of three exact same lines in three different functions.
Irw, guess i made a bug of some kind, when writing a fix. Anyhow i wanted to show how code could be designed if there where a lot more then 4 buttons to generate different images.
/** * Class MondrianPictureOnly - write a description of the class here */ public class MondrianPictureOnly extends JApplet implements ActionListener {
private static final long serialVersionUID = 4521234837503565212L;
public class exButton extends JButton { private static final long serialVersionUID = 4676425712555477294L; private Color[] _colors;
Here's another attack. I took the colorizing from pbl, blame him.
I don't really like lots of large scope variables floating about, so I tend to stomp them. Here we make the button itself responsible for holding it's color data. We can even go so far as to make it do our drawing. This means we don't have to test the button, just check it's class and call it's method.