Как разделить свой код на разные файлы? (Java, используя JGrasp) [Закрыто]JAVA

Программисты JAVA общаются здесь
Anonymous
Как разделить свой код на разные файлы? (Java, используя JGrasp) [Закрыто]

Сообщение Anonymous »

Новый кодер здесь. Недавно я разрабатывал эту простую игру с кликером, и я понял, что мой код был ужасно грязным, находясь в одном файле (я включу свой оригинальный код ниже). Мне было интересно, может ли кто -нибудь помочь мне начать отделение кода, так как я не уверен, с чего начать. Я понимаю, что мой вопрос очень широкий, но я действительно понятия не имею, с чего начать. Спасибо! < /P>
clicker.java
import javax.swing.*;
import java.awt.*;

public class Clicker extends JPanel {
private int money = 0, costAuto = 10, costClick = 10, moneyAdded = 1, moneyPerSecond = 0, power = 1, powerAdded = 1, errorLength = 750, amountAwarded;

private JPanel upgradePanel, statsPanel, secretButtonPanel, upgrades;
private JLabel moneyLabel, clicksLabel, powerLabel, errorLabel, upgradeLabel, upgradeLine, statsLabel, statsLine;
private JButton moneyClicker, upgradeAuto, upgradeClick, secretButton;
private Timer passiveIncomeTimer, buttonError, buttonErrorAuto, upgradeClickAlert, upgradeAutoAlert;
private boolean buttonPressed = false;

public Clicker() {
setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

moneyLabel = new JLabel("You have " + money + " coins");
clicksLabel = new JLabel("Money per second: " + moneyPerSecond + "c");
powerLabel = new JLabel("Clicking power: " + power);

upgradeAuto = new JButton("Auto click upgrade: " + costAuto + "c");
upgradeClick = new JButton("Click power upgrade: " + costClick + "c");

upgradeAuto.setBackground(Color.WHITE);
upgradeAuto.setForeground(Color.BLACK);

upgradeClick.setBackground(Color.WHITE);
upgradeClick.setForeground(Color.BLACK);

secretButtonPanel = new JPanel();
secretButtonPanel.setLayout(null);
secretButtonPanel.setPreferredSize(new Dimension(600, 300));

secretButton = new JButton(" ");
secretButton.setBounds(0, 0, 100, 50);

secretButton.setOpaque(false);
secretButton.setContentAreaFilled(false);
secretButton.setBorderPainted(false);
secretButton.setFocusable(true);

// Action listener for secret button
secretButton.addActionListener(e -> {
if(buttonPressed == false){
buttonPressed = true;
if(money>100){
amountAwarded = (int)(money * 0.15);
} else {
amountAwarded = 15;
}
money += amountAwarded;
updateMoneyLabel();
JOptionPane.showMessageDialog(this, "Secret Button Pressed! " + amountAwarded + "c added.");
} else {
JOptionPane.showMessageDialog(this, "You already found this secret!");
}
});

// Add the secret button to the button panel
secretButtonPanel.add(secretButton);

errorLabel = new JLabel("Not enough money!");
upgradeLabel = new JLabel("Upgrades");
upgradeLine = new JLabel("----------");
statsLabel = new JLabel("Stats");
statsLine = new JLabel("----------");
Font font = new Font("", Font.BOLD, 20);
errorLabel.setFont(font);
upgradeLabel.setFont(font);
statsLabel.setFont(font);

moneyClicker = new JButton("Click for money");
moneyClicker.setPreferredSize(new Dimension(150, 50));

moneyLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
clicksLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
powerLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
upgradeAuto.setAlignmentX(Component.CENTER_ALIGNMENT);
moneyClicker.setAlignmentX(Component.CENTER_ALIGNMENT);
upgradeClick.setAlignmentX(Component.CENTER_ALIGNMENT);
errorLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
upgradeLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
upgradeLine.setAlignmentX(Component.CENTER_ALIGNMENT);
statsLabel.setAlignmentX(Component.CENTER_ALIGNMENT);
statsLine.setAlignmentX(Component.CENTER_ALIGNMENT);

statsPanel = new JPanel();
statsPanel.setLayout(new BoxLayout(statsPanel, BoxLayout.Y_AXIS));

statsPanel.add(statsLabel);
statsPanel.add(statsLine);
statsPanel.add(moneyLabel);
statsPanel.add(clicksLabel);
statsPanel.add(powerLabel);

add(moneyClicker);

upgrades = new JPanel(new FlowLayout());
upgrades.add(upgradeAuto);
upgrades.add(upgradeClick);

add(upgrades);
add(errorLabel);
errorLabel.setVisible(false);

upgradePanel = new JPanel();
upgradePanel.setLayout(new BoxLayout(upgradePanel, BoxLayout.Y_AXIS));

upgradePanel.add(upgradeLabel);
upgradePanel.add(upgradeLine);
upgradePanel.add(upgrades);
upgradePanel.add(errorLabel);

add(statsPanel);
add(upgradePanel);

add(secretButtonPanel);

moneyClicker.addActionListener(e -> {
money += moneyAdded * power;
updateMoneyLabel();
});

upgradeAuto.addActionListener(e -> {
upgradeAuto();
updateClicksLabel();
});

upgradeClick.addActionListener(e -> {
upgradeClick();
updateClicksLabel();
});

startPassiveIncome();
}

private void startPassiveIncome() {
if (passiveIncomeTimer != null) {
passiveIncomeTimer.stop();
}

if (moneyPerSecond > 0) {
int interval = 1000 / moneyPerSecond;
passiveIncomeTimer = new Timer(interval, e -> {
money += 1;
updateMoneyLabel();
});

passiveIncomeTimer.start();
}
}

private void upgradeAuto() {
if (money >= costAuto) {
money -= costAuto;
updateMoneyLabel();
costAuto = (int)(costAuto * 1.75);
moneyPerSecond += moneyAdded;
updateUpgradeLabel();
startPassiveIncome();

upgradeAuto.setBackground(Color.GREEN);
upgradeAuto.setForeground(Color.BLACK);

upgradeAutoAlert = new Timer(errorLength, e -> {
upgradeAuto.setBackground(Color.WHITE);
upgradeAuto.setForeground(Color.BLACK);
});

upgradeAutoAlert.setRepeats(false);
upgradeAutoAlert.start();
} else {
errorLabel.setVisible(true);
upgradeAuto.setBackground(Color.RED);
upgradeAuto.setForeground(Color.WHITE);

buttonErrorAuto = new Timer(errorLength, e -> {
upgradeAuto.setBackground(Color.WHITE);
upgradeAuto.setForeground(Color.BLACK);
errorLabel.setVisible(false);
});

buttonErrorAuto.setRepeats(false);
buttonErrorAuto.start();
}
}

private void upgradeClick() {
if (money >= costClick) {
money -= costClick;
updateMoneyLabel();
costClick = (int)(costClick * 1.75);
power += powerAdded;
updatePowerLabel();
updateUpgradeLabelClickPower();

upgradeClick.setBackground(Color.GREEN);
upgradeClick.setForeground(Color.BLACK);

upgradeClickAlert = new Timer(errorLength, e -> {
upgradeClick.setBackground(Color.WHITE);
upgradeClick.setForeground(Color.BLACK);
});

upgradeClickAlert.setRepeats(false);
upgradeClickAlert.start();
} else {
errorLabel.setVisible(true);
upgradeClick.setBackground(Color.RED);
upgradeClick.setForeground(Color.WHITE);

buttonError = new Timer(errorLength, e -> {
upgradeClick.setBackground(Color.WHITE);
upgradeClick.setForeground(Color.BLACK);
errorLabel.setVisible(false);
});

buttonError.setRepeats(false);
buttonError.start();
}
}

private void updateMoneyLabel() {
moneyLabel.setText("You have " + money + " coins");
}

private void updateClicksLabel() {
clicksLabel.setText("Money per second: " + moneyPerSecond + "c");
}

private void updatePowerLabel() {
powerLabel.setText("Clicking power: " + power);
}

private void updateUpgradeLabel() {
upgradeAuto.setText("Auto click upgrade: " + costAuto + "c");
}

private void updateUpgradeLabelClickPower() {
upgradeClick.setText("Click power upgrade: " + costClick + "c");
}

public static void main(String[] args) {
JFrame frame = new JFrame("Clicker Game");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 300);
frame.setResizable(false);
Clicker clickerPanel = new Clicker();
frame.add(clickerPanel);
frame.setVisible(true);
}
}


Подробнее здесь: https://stackoverflow.com/questions/794 ... ing-jgrasp

Вернуться в «JAVA»