简体   繁体   中英

How to modify pixels and use mouseclickedaction?

I am designing a picture lab project for school and I can't figure out how to modify the rgb of pixels. My project is an eye test game where players choose the one color that is different from the rest. Should I modify the pixels of the background or of a blank photo? Also, how do I implement mouseClickedAction (given method that runs when mouse is clicked)?

Here's some bare stuff I have so far:

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Point;
import java.awt.image.ImageObserver;
import java.util.ArrayList;

public class EagleEye extends FlexiblePictureExplorer {
    public static Picture background = new Picture(1003,1003);

    //settings
    private int gameDimensions = 4;
    private int difficulty = 30;

    private final int statsHeight = 80;

    public EagleEye(DigitalPicture Picture) {
        super(background);
        setTitle("Eagle Eye");
        int xGrid = gameDimensions;
        int yGrid = gameDimensions;
        int r1 = (int)(Math.random() * gameDimensions + 1);
        int r2 = (int)(Math.random() * gameDimensions + 1);
        colorSetter(xGrid,yGrid);

    }

    private void colorSetter(int x,int y) {
        for (int i=0;i<x;i++) {
            for (int j=0;j<y;j++) {
                fillBox(i,j);
            }
        }
    }
    private void fillBox(int x, int y) {
        int r = (int)(Math.random() * 255 + 1);
        int g = (int)(Math.random() * 255 + 1);
        int b = (int)(Math.random() * 255 + 1);
        int x1;
        int x2;
        if (x==0) {
            x1 = 0;
            x2 = 250;
        }
        else if (x==1) {
            x1 = 252;
            x2 = 501;
        }
        else if (x==2) {
            x1 = 503;
            x2 = 752;
        }
        else {
            x1 = 754;
            x2 = 1003;
        }
        int y1;
        int y2;
        if (y==0) {
            y1 = 0;
            y2 = 250;
        }
        else if (y==1) {
            y1 = 252;
            y2 = 501;
        }
        else if (y==2) {
            y1 = 503;
            y2 = 752;
        }
        else {
            y1 = 754;
            y2 = 1003;
        }
        int rgb = calculateColors(r,g,b);
        for (int i=x1;i<=x2;i++) {
            for (int j=y1;j<=y2;j++) {
                background.setBasicPixel(x1,y1,rgb);
            }
        }
        setImage(background);
    }
    private int calculateColors(int r, int g, int b) {
        int r1 = r * 65536;
        int g1 = g * 256;
        int b1 = b;
        return r1 + g1 + b1;
    }
    private void drawStats(Picture img){
        Picture statsImg = new Picture(statsHeight, imageWidth);
        Graphics g = statsImg.getGraphics();
        g.setFont(new Font("Times New Roman", Font.PLAIN, 16));
        g.setColor(Color.blue);
    }
    private void updateImage() {

    }
    public void mouseClickedAction(DigitalPicture pict, Pixel pix) {

    }
    private void endGame() {

    }
    public boolean imageUpdate(Image arg0, int arg1, int arg2, int arg3, int arg4, int arg5) {
        // TODO Auto-generated method stub
        return false;
    }
    public static void main(String[] args) {
        Picture white = new Picture(100,100);
        EagleEye game = new EagleEye(white);

    }
}

I think the easiest way that you will find for drawing and graphics will not be drawing to an image, but instead drawing directly to the JPanel. If you have a class that extends JPanel, you can implement

public void paintComponent(Graphics g) {
    //Code to draw whatever you like, e.g.
    g.setColor(new Color(255, 255, 255));
    g.drawRect(0, 0, width, height);
}

With this, you will not have to worry about handling images, If you would still like to use images, you can use the BufferedImage, which has great docs explaining how to use it: https://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html (You will still need to display these images, most likely using the paintComponent method above anyways, and if you want to draw in a loop, you will have to put that loop on another thread, be aware)

As for the mouseClicked, you will need to implement a MouseListener, which is quite simple:

Create a MouseListener object in your class, you will have to implement a number of methods inside it, most of which will likely go unused.

Somewhere in your code (probably the constructor) you will need to add the MouseListener to whatever component you want to wait for the clicks (probably the panel you are drawing on)

When that component is clicked on, the mouseClicked method will be called and from there you can do whatever you need to, like calling the other methods you have there to handle mouse clicks.

The MouseEvent object in the listener has all the useful information yu will need, like it's position (relative to the component you added the listener to)

public class YourClass {

    public YourClass() {
        this.addMouseListener(ml);
    }

    //code


    private MouseListener ml = new MouseListener() {

        @Override
        public void mouseClicked(MouseEvent arg0) {
            // TODO Auto-generated method stub
            System.out.println(arg0.getX() + ", " + arg0.getY());
        }

        @Override
        public void mouseEntered(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseExited(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mousePressed(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void mouseReleased(MouseEvent arg0) {
            // TODO Auto-generated method stub

        }

    }


}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM