mercredi 5 avril 2023

Java: trying to get random numbers out of class and read and changed in another [duplicate]

I am trying to make it so when you click on the buttons where it says "Power" + n or "power" + N it gives random value added to p. I try to make the variables static but it doesn't like it.

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import java.awt.*;
import javax.swing.*;

class DungeonRandomizer {
    
    public static class DungeonRand extends Canvas {
        public void paint(Graphics g) {
            int p = 0;
            int w = 5;
            int c = 0;
            Random rand = new Random();
            int n = rand.nextInt(25);
            int N = rand.nextInt(25);
            System.out.println("N" + N);
            System.out.println("n" + n);
            Color NeonBlue = new Color(64, 46, 255);
            Color Brown = new Color(64, 46, 32);
            g.setFont(g.getFont().deriveFont(20.0f));
            g.setColor(Brown);
            g.fillRect(0, 0, 1000, 100);
            g.setColor(NeonBlue);
            g.drawString("Your Power " + p, 10, 50);
            g.setColor(Brown);
            g.fillRoundRect(150, 400, 200, 200, 25, 25);
            g.fillRoundRect(650, 400, 200, 200, 25, 25);
            g.setColor(NeonBlue);
            g.drawString("Power" +   N, 215, 510);
            g.drawString("Power" + n, 715, 510);
            setBackground(Color.darkGray);
            setVisible(true);
        }

        static int l = 0;
        static int x = 0;
        final static  DungeonRand[] m = {new DungeonRand()};

        public abstract static class Button extends JFrame implements ActionListener {
            
            public static void main(String[] args) {
                JButton B = new JButton("Select");
                JButton b = new JButton("Select");
                JFrame f = new JFrame("Floor" + l);
                JFrame F = new JFrame("Floor" + l);
                Random rand = new Random();
                f.setSize(1000, 1000);
                f.setVisible(true);
                f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                f.add(b);
                f.add(B);
                f.add(m[0]);
                Color Brown = new Color(64, 46, 32);
                b.setLocation(150, 525);
                b.setSize(200, 200);
                b.setBorderPainted(false);
                b.setBackground(Brown);
                Color NeonBlue = new Color(64, 46, 255);
                b.setForeground(NeonBlue);
                b.setFont(b.getFont().deriveFont(20.0f));
                B.setLocation(650, 525);
                B.setBorderPainted(false);
                B.setBackground(Brown);
                B.setForeground(NeonBlue);
                B.setFont(b.getFont().deriveFont(20.0f));
                B.setSize(200, 200);
                b.enableInputMethods(true);
                ActionListener ButtonListener = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        p+=n;
                        l+=1;
                        f.dispose();
                        F.setSize(1000, 1000);
                        F.setVisible(true);
                        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        F.add(b);
                        F.add(B);
                        F.add(m[0]);
                        Color Brown = new Color(64, 46, 32);
                        b.setLocation(150, 525);
                        b.setSize(200, 200);
                        b.setBorderPainted(false);
                        b.setBackground(Brown);
                        Color NeonBlue = new Color(64, 46, 255);
                        b.setForeground(NeonBlue);
                        b.setFont(b.getFont().deriveFont(20.0f));
                        B.setLocation(650, 525);
                        B.setBorderPainted(false);
                        B.setBackground(Brown);
                        B.setForeground(NeonBlue);
                        B.setFont(b.getFont().deriveFont(20.0f));
                        B.setSize(200, 200);
                        b.enableInputMethods(true);
                        JOptionPane.showMessageDialog(null, "Nice pick");
                    }
                };
                ActionListener ButtonListener2 = new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        p+=N;
                        l+=1;
                        f.dispose();
                        F.setSize(1000, 1000);
                        F.setVisible(true);
                        F.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                        F.add(b);
                        F.add(B);
                        F.add(m[0]);
                        Color Brown = new Color(64, 46, 32);
                        b.setLocation(150, 525);
                        b.setSize(200, 200);
                        b.setBorderPainted(false);
                        b.setBackground(Brown);
                        Color NeonBlue = new Color(64, 46, 255);
                        b.setForeground(NeonBlue);
                        b.setFont(b.getFont().deriveFont(20.0f));
                        B.setLocation(650, 525);
                        B.setBorderPainted(false);
                        B.setBackground(Brown);
                        B.setForeground(NeonBlue);
                        B.setFont(b.getFont().deriveFont(20.0f));
                        B.setSize(200, 200);
                        b.enableInputMethods(true);
                        JOptionPane.showMessageDialog(null, "Nice pick");
                    }
                };
                b.addActionListener(ButtonListener);
                B.addActionListener(ButtonListener2);
            }
        }
    }
}

I cant get the variables to be read and changed out side of the local class and I need that to happen. the variables i need to get are p n N. when i click on the buttons (the action listeners) they should take the random value displayed on screen and add to the var p and change on screen.




Aucun commentaire:

Enregistrer un commentaire