I gave myself a small project to work on and at first I wanted to use the MVC approach, but I got confused as to which part of my code should go into which class. I did some research and still don't quite understand how I should go about it.
This is the GUI I made:
I created the View class:
public class LevelUpView extends JApplet{
Image levelUp, clickMeImg, expBarImg, currentLvlImg, obtainedExpImg;
Image expBarImg2, expBarImg3, expBarImg4;
ImageIcon levelUpIcon, clickMeIcon, expBarIcon, currentLvlIcon, obtainedExpIcon;
ImageIcon expBarIcon2, expBarIcon3, expBarIcon4;
JLabel title, expBar, currentLvl, currentLevel, obtainedExp;
JLabel expTotal;
JButton clickMe;
JPanel mainPanel;
int experience = 0;
int level = 0;
String lvl = "0";
String exp = "0";
public void init(){
mainPanel = new JPanel( new BorderLayout()); // main container for layout
mainPanel.setBackground(new Color(213, 213, 210));
add(mainPanel);
createTitle(); // creates each section of the layout
createCenter();
createBottom();
}
private void createTitle(){
levelUp = getImage(getCodeBase(), "Images/LevelUpLogo.png");
levelUpIcon = new ImageIcon(levelUp);
title = new JLabel(levelUpIcon);
mainPanel.add(title, BorderLayout.NORTH);
}
private void createCenter(){
JPanel centerPanel = new JPanel();
centerPanel.setLayout( new BoxLayout( centerPanel, BoxLayout.Y_AXIS));
centerPanel.setBorder(new EmptyBorder(0,0,-140,0)); // spacing
JPanel clickMeLayout = new JPanel(new BorderLayout());
clickMeLayout.setBorder( new EmptyBorder(90,0,0,0));
JPanel expBarLayout = new JPanel( new BorderLayout());
expBarLayout.setBorder( new EmptyBorder(-190,1,1,55));
expBarLayout.setOpaque(false); // allows background to show
clickMeLayout.setOpaque(false);
centerPanel.setOpaque(false);
clickMeImg = getImage(getCodeBase(), "Images/ClickMe.png"); // getting Images
expBarImg = getImage(getCodeBase(), "Images/EXPBar.png");
expBarImg2 = getImage(getCodeBase(), "Images/EXPBar2.png");
expBarImg3 = getImage(getCodeBase(), "Images/EXPBar3.png");
expBarImg4 = getImage(getCodeBase(), "IMages/EXPBar4.png");
clickMeIcon = new ImageIcon(clickMeImg);
expBarIcon = new ImageIcon(expBarImg);
expBarIcon2 = new ImageIcon(expBarImg2);
expBarIcon3 = new ImageIcon(expBarImg3);
expBarIcon4 = new ImageIcon(expBarImg4);
clickMe = new JButton(clickMeIcon);
clickMe.setBorderPainted(false);
expBar = new JLabel(expBarIcon);
clickMeLayout.add(clickMe, BorderLayout.NORTH);
expBarLayout.add(expBar, BorderLayout.NORTH);
centerPanel.add(clickMeLayout);
centerPanel.add(expBarLayout);
mainPanel.add(centerPanel, BorderLayout.CENTER);
}
private void createBottom(){
Font numberFont = new Font("Monospaced", Font.BOLD, 25); // font for exp amount and level
JPanel bottomPanel = new JPanel( new GridLayout(2,2));
JPanel bottomFlowPanel = new JPanel( new FlowLayout());
bottomPanel.setOpaque(false);
bottomFlowPanel.setOpaque(false);
currentLvlImg = getImage( getCodeBase(), "Images/CurrentLevel.png");
obtainedExpImg = getImage(getCodeBase(), "Images/EXPObtained.png");
currentLvlIcon = new ImageIcon(currentLvlImg);
obtainedExpIcon = new ImageIcon(obtainedExpImg);
currentLvl = new JLabel(currentLvlIcon);
obtainedExp = new JLabel(obtainedExpIcon);
expTotal = new JLabel(exp);
currentLevel = new JLabel(lvl);
expTotal.setFont(numberFont);
currentLevel.setFont(numberFont);
bottomPanel.add(currentLvl);
bottomPanel.add(currentLevel);
bottomPanel.add(obtainedExp);
bottomPanel.add(expTotal);
bottomFlowPanel.add(bottomPanel);
bottomFlowPanel.setBorder(new EmptyBorder(0,255,0,0));
mainPanel.add(bottomFlowPanel, BorderLayout.SOUTH);
}
}
And I ended up just doing the rest in the Model class:
public class LevelUpModel extends LevelUpView implements ActionListener{
private int expForLevels[] = { 20, 50, 100, 160, 240, 340, 420, 560, 750, 999};
public void init(){
super.init();
clickMe.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
Object source = ae.getSource();
if( source == clickMe){
experience++;
exp = "" + experience;
expTotal.setText(exp);
levels();
}
}
private void levels(){ // as the user gains exp and levels up
int amount = Integer.parseInt(exp);
if ( amount == (expForLevels[0]/3)){
expBar.setIcon(expBarIcon2); // changes the expBar image
}
else if( amount == ((expForLevels[0]/3) + 6)){
expBar.setIcon(expBarIcon3);
}
else if( amount == (expForLevels[0] - 2)){
expBar.setIcon(expBarIcon4);
}
else if( amount == expForLevels[0]){
levelUp();
mainPanel.setBackground( new Color(58, 144, 255)); // dark blue
}
if( amount == expForLevels[1] - 20){
expBar.setIcon(expBarIcon2);
}
else if( amount == expForLevels[1]-10){
expBar.setIcon(expBarIcon3);
}
else if( amount == expForLevels[1] - 2){
expBar.setIcon(expBarIcon4);
}
else if( amount == expForLevels[1]){
levelUp();
mainPanel.setBackground(new Color(255, 133, 0)); // burnt orange
}
if( amount == expForLevels[2] - 35){
expBar.setIcon(expBarIcon2);
}
else if ( amount == expForLevels[2] - 20){
expBar.setIcon(expBarIcon3);
}
else if( amount == expForLevels[2] - 5){
expBar.setIcon(expBarIcon4);
}
else if( amount == expForLevels[2]){
levelUp();
mainPanel.setBackground(new Color(0, 178, 18)); // dark green
}
// same thing will be done up to level 10
}
private void levelUp(){ // each time there's a level up
level++;
lvl = "" + level;
currentLevel.setText(lvl);
expBar.setIcon(expBarIcon);
}
}
Once the button is clicked experience is added and the experience bar changes as it gets closer to the next level up. Once you level up, the background changes and the bar is reset to its original image. (Up to level 10).
I realized I was doing this all wrong and the best way to go about it was to get my code looked at. Also, I'm confused as to which variables should be made private?
So, what I'm asking is for an example of which part of my code should go into which class, considering I made a controller class as well. Also, an example of using private variables within my code. As well as any more problems, I need all the constructive criticism I can get.
As a beginner, I want to learn better practices as soon as I can!