如何修改像素并使用mouseclickedaction?

问题描述 投票:1回答:1

我正在为学校设计一个图片实验室项目,但我不知道如何修改像素的rgb。我的项目是一项视力测试游戏,玩家在其中选择一种与其他颜色不同的颜色。我应该修改背景像素还是空白图片像素?另外,如何实现mouseClickedAction(在单击鼠标时运行的给定方法)?

到目前为止,我有一些裸露的东西:

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);

    }
}
java image rgb
1个回答
0
投票

我认为您将找到的用于绘制图形的最简单方法将不是绘制图像,而是直接绘制到JPanel。如果您有扩展JPanel的类,则可以实现

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);
}

有了这个,您不必担心处理图像,如果您仍然想使用图像,则可以使用BufferedImage,该文档中有出色的文档介绍了如何使用它:https://docs.oracle.com/javase/tutorial/2d/images/drawonimage.html(您仍将需要显示这些图像,无论如何,很可能仍在上面使用paintComponent方法显示,并且如果要绘制一个循环,则必须将该循环放在另一个线程上,请注意)

关于mouseClicked,您将需要实现一个MouseListener,这很简单:

在您的类中创建一个MouseListener对象,您将必须在其中实现许多方法,其中大多数方法可能会不使用。

在代码中的某个地方(可能是构造函数),您需要将MouseListener添加到要等待单击的任何组件中(可能是您正在绘制的面板)

单击该组件时,将调用mouseClicked方法,从那里您可以执行所需的任何操作,就像调用那里的其他方法来处理鼠标单击一样。

侦听器中的MouseEvent对象具有yu所需的所有有用信息,例如位置(相对于您向其添加侦听器的组件)

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

        }

    }


}
© www.soinside.com 2019 - 2024. All rights reserved.