0

どうもありがとうございましたが、リスナーメソッドが必要で、今すぐ解決しました。しかし、ボタンにカーソルを合わせるまでボタンが表示されません。ちょっとした間違いだと思いますが、わかりませんでした!! 今私のコードは以下のようです...

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class PaintMain extends JFrame
{
    JPanel colorPanel = new JPanel();
    JPanel drawPanel = new JPanel();
    JPanel selectPanel = new JPanel();
    JButton[][] randomColors = new JButton[5][5];
    Color selectedColor = Color.BLACK;
    Random colorGenerator = new Random();
    int curx = drawPanel.getX();
    int cury = drawPanel.getY();
    public PaintMain()
    {
        setLayout(new BorderLayout());
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        colorPanel.setLayout(new GridLayout(5,5));
        drawPanel.setLayout(new BorderLayout());
        drawPanel.setBackground(Color.WHITE);

        for (int i = 0; i < 5; i++) 
        {
            for (int j = 0; j < 5; j++) 
            {
                randomColors[i][j]=new JButton();
                randomColors[i][j].setBackground(getRandomColor());
                randomColors[i][j].addActionListener(new ActionListener() 
                {                   

                    @Override
                    public void actionPerformed(ActionEvent e) 
                    {
                        if( e.getSource() instanceof JButton) 
                        {
                               selectedColor=((JButton)e.getSource()).getBackground();
                               System.out.println(selectedColor);
                        }                       
                    }
                });
                colorPanel.add(randomColors[i][j]);
            }           
        }

        this.addMouseMotionListener(new MouseLsnr());
        add(colorPanel, BorderLayout.WEST);
        add(drawPanel, BorderLayout.CENTER);

        setVisible(true);
        pack();     
    }

    public Color getRandomColor() 
    {
        return new Color(colorGenerator.nextInt(256), colorGenerator.nextInt(256), colorGenerator.nextInt(256));
    }

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

    public void paint(Graphics g)
    {
        //super.paint(g);
        g.setColor(selectedColor);
        g.drawRect(curx, cury, 2, 1);
    }

    class MouseLsnr implements MouseMotionListener
    {       
        public void mouseDragged(MouseEvent arg0) {

            System.out.println(arg0.getX()+":"+arg0.getY());
            curx=arg0.getX();
            cury=arg0.getY();

            repaint();
        }

        public void mouseMoved(MouseEvent arg0) 
        {


        }
    }
}
4

1 に答える 1

1

カスタムペインティングを詳しく見てみたい

ここに画像の説明を入力

How to Write a Mouse Listener を読むことをお勧めします。

public class RandomColorPane {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        new RandomColorPane();
    }

    public RandomColorPane() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException ex) {
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (UnsupportedLookAndFeelException ex) {
                }

                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new MainPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }

        });
    }

    public class MainPane extends JPanel {

        JPanel colorPanel = new JPanel();
        JPanel drawPanel = new DrawPane();
        JPanel selectPanel = new JPanel();
        JPanel[][] randomColors = new JPanel[5][5];
        Color selectedColor = Color.BLACK;
        Random colorGenerator = new Random();
        int curx = drawPanel.getX();
        int cury = drawPanel.getY();

        public MainPane() {
            setLayout(new BorderLayout());

            colorPanel.setLayout(new GridLayout(5, 5));
            drawPanel.setLayout(new BorderLayout());
            drawPanel.setBackground(Color.WHITE);

            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < 5; j++) {
                    randomColors[i][j] = new JPanel();
                    randomColors[i][j].setPreferredSize(new Dimension(25, 25));
                    randomColors[i][j].setBackground(getRandomColor());
//                    randomColors[i][j].addActionListener(new ActionListener() {
//                        @Override
//                        public void actionPerformed(ActionEvent e) {
//                            if (e.getSource() instanceof JButton) {
//                                selectedColor = ((JButton) e.getSource()).getBackground();
//                                drawPanel.setForeground(selectedColor);
//                                drawPanel.repaint();
//                            }
//                        }
//
//                    });
                    randomColors[i][j].addMouseListener(new MouseAdapter() {

                        @Override
                        public void mouseEntered(MouseEvent e) {
                            drawPanel.setForeground(((JPanel)e.getSource()).getBackground());
                            drawPanel.repaint();
                        }

                    });
                    colorPanel.add(randomColors[i][j]);
                }
            }

            add(colorPanel, BorderLayout.WEST);
            add(drawPanel, BorderLayout.CENTER);
        }

        public Color getRandomColor() {
            return new Color(colorGenerator.nextInt(256), colorGenerator.nextInt(256), colorGenerator.nextInt(256));
        }

    }

    public class DrawPane extends JPanel {

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

        @Override
        protected void paintComponent(Graphics g) {

            super.paintComponent(g);

            int x = (getWidth() - 10) / 2;
            int y = (getHeight() - 10) / 2;

            g.setColor(getForeground());
            g.fillRect(x, y, 20, 20);

        }

    }

}
于 2012-10-21T19:44:19.403 に答える