Home > Software design >  Adding a JButton to a Canvas
Adding a JButton to a Canvas

Time:09-25

I've tried rearranging this a whole bunch and using different methods that weren't static but the button never appears. all of this code is meant for chess but I working on adding buttons to make it playable, and I've been having a hard time with this for the past few days. there is other issues like the mouse not doing anything, but i jut removed everything to try and focus on the buttons. a

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

public class Main extends Canvas implements ActionListener, MouseListener {
    private static JButton try144Button = new JButton();

public static void main(String[] args) {
    JFrame frame = new JFrame("Chess");
    Canvas canvas = new Main();
    canvas.setSize(1000, 1000);
    frame.add(canvas);
    frame.add(try144Button);
    frame.pack();
    frame.setVisible(true);
}
public Main(){
   
    try144Button.setBounds(0, 0, 500, 500);
    try144Button.setText("CLICK ME");
    try144Button.setBounds(210, 60, 150, 150);}

private void add(JButton try144Button) {
}


public void paint(Graphics g) {
    int xshift;
    int yshift = 0;

    g.setColor(new Color(0, 0, 0));
    for (int i = 0; i < 4; i  ) {
        xshift = 125;
        for (int j = 0; j < 4; j  ) {
            g.fillRect(xshift, yshift, 125, 125);
            xshift  = 250;
        }
        xshift = 0;
        yshift  = 125;

        for (int k = 0; k < 4; k  ) {
            g.fillRect(xshift, yshift, 125, 125);
            xshift  = 250;
        }
        yshift  = 125;
    }
    initalprnt(g);
}

public void initalprnt(Graphics g) {
    int xshift = 125;
    int yshift = 0;
    g.setColor(new Color(0, 100, 0));
    for (int i = 0; i < 3; i  ) {
        if (i == 2) {
            xshift = 125;
        }
        for (int j = 0; j < 4; j  ) {
            g.fillOval(xshift, yshift, 125, 125);
            xshift  = 250;
        }
        xshift = 0;
        yshift  = 125;
    }
    yshift  = 250;
    g.setColor(new Color(0, 0, 100));
    for (int k = 0; k < 3; k  ) {
        if (k == 2) {
            xshift = 0;
        }
        for (int j = 0; j < 4; j  ) {
            g.fillOval(xshift, yshift, 125, 125);
            xshift  = 250;
        }
        xshift = 125;
        yshift  = 125;
    }
}

public void actionPerformed(ActionEvent evt) {
    Object src = evt.getSource();
    if (src == try144Button)
        System.out.println("this worked");
}
    public void mouseClicked(MouseEvent e) {}
    public void mousePressed(MouseEvent e) {}
    public void mouseReleased(MouseEvent e) {}
    public void mouseEntered(MouseEvent e) {}
    public void mouseExited(MouseEvent e) {}
}

CodePudding user response:

Unless you're doing some super low level work, you should avoid using Canvas, in your case, a JPanel will do just fine.

If you override a method of a class, you should beware of what that method does and either be prepared to replicate its work (as much as your implementation requires it to) or call it's super method.

I would recommend you start by looking at boardPlay it again same

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.RadialGradientPaint;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

public class Test {

    public static void main(String[] args) {
        new Test();
    }

    public Test() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                JFrame frame = new JFrame("Chess");
                JPanel contentPane = new JPanel(new GridBagLayout());
                GridBagConstraints gbc = new GridBagConstraints();
                gbc.gridx = 0;
                gbc.gridy = 0;
                gbc.weightx = 1;
                gbc.weighty = 1;
                gbc.fill = gbc.BOTH;

                GameListener listener = new GameListener() {
                    private AlertPane alertPane;

                    private AlertPane getAlertPane() {
                        if (alertPane != null) {
                            return alertPane;
                        }

                        alertPane = new AlertPane();
                        alertPane.setGameListener(this);
                        return alertPane;
                    }

                    @Override
                    public void gameWasCompleted() {
                        contentPane.add(getAlertPane(), gbc);
                        contentPane.setComponentZOrder(getAlertPane(), 0);
                        contentPane.revalidate();
                        contentPane.repaint();
                    }

                    @Override
                    public void startNewGame() {
                        contentPane.remove(getAlertPane());
                        contentPane.revalidate();
                        contentPane.repaint();
                    }
                };

                MainPane mainPane = new MainPane();
                mainPane.setGameListener(listener);

                contentPane.add(mainPane, gbc);

                frame.setContentPane(contentPane);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public interface GameListener {

        public void gameWasCompleted();

        public void startNewGame();
    }

    public class AlertPane extends JPanel {

        private GameListener gameListener;

        private JButton try144Button;

        public AlertPane() {
            setLayout(new GridBagLayout());
            setOpaque(false);

            try144Button = new JButton();
            try144Button.setText("CLICK ME");
            try144Button.setBounds(210, 60, 150, 150);

            add(try144Button);

            try144Button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    gameListener.startNewGame();
                }
            });
        }

        public void setGameListener(GameListener gameListener) {
            this.gameListener = gameListener;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);

            Graphics2D g2d = (Graphics2D) g.create();
            Color startColor = new Color(255, 255, 0, 0);
            Color endColor = new Color(255, 255, 0, 192);

            RadialGradientPaint rgp = new RadialGradientPaint(
                    getWidth() / 2, getHeight() / 2,
                    Math.max(getWidth(), getHeight()),
                    new float[]{0f, 0.25f},
                    new Color[]{startColor, endColor});
            g2d.setPaint(rgp);
            g2d.fillRect(0, 0, getWidth(), getHeight());
            g2d.dispose();
        }

    }

    public class MainPane extends JPanel implements MouseListener {

        private GameListener gameListener;

        public MainPane() {
            addMouseListener(this);
        }

        public void setGameListener(GameListener gameListener) {
            this.gameListener = gameListener;
        }

        @Override
        public Dimension getPreferredSize() {
            return new Dimension(1000, 1000);
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            int xshift;
            int yshift = 0;

            g.setColor(new Color(0, 0, 0));
            for (int i = 0; i < 4; i  ) {
                xshift = 125;
                for (int j = 0; j < 4; j  ) {
                    g.fillRect(xshift, yshift, 125, 125);
                    xshift  = 250;
                }
                xshift = 0;
                yshift  = 125;

                for (int k = 0; k < 4; k  ) {
                    g.fillRect(xshift, yshift, 125, 125);
                    xshift  = 250;
                }
                yshift  = 125;
            }
            initalprnt(g);
        }

        protected void initalprnt(Graphics g) {
            int xshift = 125;
            int yshift = 0;
            g.setColor(new Color(0, 100, 0));
            for (int i = 0; i < 3; i  ) {
                if (i == 2) {
                    xshift = 125;
                }
                for (int j = 0; j < 4; j  ) {
                    g.fillOval(xshift, yshift, 125, 125);
                    xshift  = 250;
                }
                xshift = 0;
                yshift  = 125;
            }
            yshift  = 250;
            g.setColor(new Color(0, 0, 100));
            for (int k = 0; k < 3; k  ) {
                if (k == 2) {
                    xshift = 0;
                }
                for (int j = 0; j < 4; j  ) {
                    g.fillOval(xshift, yshift, 125, 125);
                    xshift  = 250;
                }
                xshift = 125;
                yshift  = 125;
            }
        }

        public void mouseClicked(MouseEvent e) {
            gameListener.gameWasCompleted();
        }

        public void mousePressed(MouseEvent e) {
        }

        public void mouseReleased(MouseEvent e) {
        }

        public void mouseEntered(MouseEvent e) {
        }

        public void mouseExited(MouseEvent e) {
        }
    }
}
  • Related