开发者

unable to apply graphics to if statement

开发者 https://www.devze.com 2023-03-31 14:45 出处:网络
I\'m writing a program that has a grid of squares that when you click on one, it changes color from black to red. I\'m writing the if statement for the first square, and It\'s not letting me do anythi

I'm writing a program that has a grid of squares that when you click on one, it changes color from black to red. I'm writing the if statement for the first square, and It's not letting me do anything with graphics. How do I 1) change the color to red, 2) draw a new square in the same place, and 3) why dosen't it like the MouseHandler class declaration?

import java.awt.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class toggle extends JPanel{
    public void paintComponent(Graphics g){
        super.paintComponents(g);
        g.fillRect(0,0,20,20);
        g.fillRect(21,0,20,20);
        g.fillRect(42,0,20,20);
        g.fillRect(63,0,20,20);
        g.fillRect(84,0,20,20);
        g.fillRect(105,0,20,20);
        g.fillRect(126,0,20,20);
        g.fillRect(147,0,20,20);
        g.fillRect(168,0,20,20);
        g.fillRect(189,0,20,20);
        g.fillRect(190,0,20,20);
        //
        g.fillRect(0,21,20,20);
        g.fillRect(21,21,20,20);
        g.fillRect(42,21,20,20);
        g.fillRect(63,21,20,20);
        g.fillRect(84,21,20,20);
        g.fillRect(105,21,20,20);
        g.fillRect(126,21,20,20);
        g.fillRect(147,21,20,20);
        g.fillRect(168,21,20,20);
        g.fillRect(189,21,20,20);
        g.fillRect(190,21,20,20);
        //
        g.fillRect(0,42,20,20);
        g.fillRect(21,42,20,20);
        g.fillRect(42,42,20,20);
        g.fillRect(63,42,20,20);
        g.fillRect(84,42,20,20);
        g.fillRect(105,42,20,20);
        g.fillRect(126,42,20,20);
        g.fillRect(147,42,20,20);
        g.fillRect(168,42,20,20);
        g.fillRect(189,42,20,20);
        g.fillRect(190,42,20,20);
        //
        g.fillRect(0,63,20,20);
        g.fillRect(21,63,20,20);
        g.fillRect(42,63,20,20);
        g.fillRect(63,63,20,20);
        g.fillRect(84,63,20,20);
        g.fillRect(105,63,20,20);
        g.fillRect(126,63,20,20);
        g.fillRect(147,63,20,20);
        g.fillRect(168,63,20,20);
        g.fillRect(189,63,20,20);
        g.fillRect(190,63,20,20);
        //
        g.fillRect(0,84,20,20);
        g.fillRect(21开发者_C百科,84,20,20);
        g.fillRect(42,84,20,20);
        g.fillRect(63,84,20,20);
        g.fillRect(84,84,20,20);
        g.fillRect(105,84,20,20);
        g.fillRect(126,84,20,20);
        g.fillRect(147,84,20,20);
        g.fillRect(168,84,20,20);
        g.fillRect(189,84,20,20);
        g.fillRect(190,84,20,20);
        //
        g.fillRect(0,105,20,20);
        g.fillRect(21,105,20,20);
        g.fillRect(42,105,20,20);
        g.fillRect(63,105,20,20);
        g.fillRect(84,105,20,20);
        g.fillRect(105,105,20,20);
        g.fillRect(126,105,20,20);
        g.fillRect(147,105,20,20);
        g.fillRect(168,105,20,20);
        g.fillRect(189,105,20,20);
        g.fillRect(190,105,20,20);
        //
        g.fillRect(0,126,20,20);
        g.fillRect(21,126,20,20);
        g.fillRect(42,126,20,20);
        g.fillRect(63,126,20,20);
        g.fillRect(84,126,20,20);
        g.fillRect(105,126,20,20);
        g.fillRect(126,126,20,20);
        g.fillRect(147,126,20,20);
        g.fillRect(168,126,20,20);
        g.fillRect(189,126,20,20);
        g.fillRect(190,126,20,20);
        //
        g.fillRect(0,147,20,20);
        g.fillRect(21,147,20,20);
        g.fillRect(42,147,20,20);
        g.fillRect(63,147,20,20);
        g.fillRect(84,147,20,20);
        g.fillRect(105,147,20,20);
        g.fillRect(126,147,20,20);
        g.fillRect(147,147,20,20);
        g.fillRect(168,147,20,20);
        g.fillRect(189,147,20,20);
        g.fillRect(190,147,20,20);
        //
        g.fillRect(0,168,20,20);
        g.fillRect(21,168,20,20);
        g.fillRect(42,168,20,20);
        g.fillRect(63,168,20,20);
        g.fillRect(84,168,20,20);
        g.fillRect(105,168,20,20);
        g.fillRect(126,168,20,20);
        g.fillRect(147,168,20,20);
        g.fillRect(168,168,20,20);
        g.fillRect(189,168,20,20);
        g.fillRect(190,168,20,20);
        //
        g.fillRect(0,189,20,20);
        g.fillRect(21,189,20,20);
        g.fillRect(42,189,20,20);
        g.fillRect(63,189,20,20);
        g.fillRect(84,189,20,20);
        g.fillRect(105,189,20,20);
        g.fillRect(126,189,20,20);
        g.fillRect(147,189,20,20);
        g.fillRect(168,189,20,20);
        g.fillRect(189,189,20,20);
        g.fillRect(190,189,20,20);
    }

    public toggle(){
        super();
        JFrame frame=new JFrame();
        frame.setTitle("toggle");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.add(this);
        frame.setSize(226, 247);
        frame.setVisible(true);

        MouseHandler hand=new MouseHandler();
        addMouseListener(hand);

    }

    public class MouseHandler implements MouseListener{
        public void MousePressed (MouseEvent e){

            if(e.getX()<20&&e.getX()>0&&e.getY()<20&&e.getY()>0){


            }
        }
    }

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


}


Also consider this alternative implementation using JLabel and a MouseAdapter.


Surely you don't need all of them g.fillRect(0,0,20,20); You could use a loop to handle this since there is an occurring pattern for the first param in each line.


1 and 2) You need to keep state for each square, in a boolean[][] field, for instance, which would tell if each square in your grid is clicked or not. In your paintComponent method, check for the state of each square and call g.setColor, passing Color.BLACK or Color.RED as argument accordingly.

3) You need to implement all methods for the MouseListener interface:

public class MouseHandler implements MouseListener {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    public void mouseReleased( MouseEvent e ) {
        // leave method empty, since you don't need to listen to this event
    }

    // Same as above for mouseClicked, mouseEntered, mouseExited
}

or extend the MouseAdapter class and override mousePressed if you don't care for the other MouseListener events:

public class MouseHandler extends MouseAdapter {
    public void mousePressed( MouseEvent e ) {
        // your logic here
    }

    // No need to override the other methods, since
    // MouseAdapter already has empty implementations for them
}

Also, the correct name for the method you're implementing is mousePressed (note the lowercase m).


3) why dosen't it like the MouseHandler class declaration?

When you implement an interface you must provide an implementation for all the methods or declare the implementation abstract. In this case, you haven't implemented a single method - Java is case-sensitive and MousePressed is not the same as mousePressed, the method you do want to implement.

Also, you'll find that there are a few interfaces that have a lot more methods than you're interested in and you'll end up providing empty methods in order to satisfy the compiler. In such a case, you might look to see if there's an 'adapter' class you can extend. In this case, you'll find the MouseAdapter useful.

0

精彩评论

暂无评论...
验证码 换一张
取 消