草庐IT

java - 鼠标下像素的透明度 - Java JFrame

coder 2024-04-05 原文

我是 Java 的新手,决定制作一个程序:当您左键单击时,在窗口中设置一个点。然后当您再次单击时,另一个点。依此类推...然后它将所有点与线连接起来,并根据线的一侧有多少点对线的一侧进行着色。(像这样)

问题从这里开始:

现在我还需要一项功能。当我右键单击时,我希望出现鼠标坐标处像素的透明度。因此,当在中间单击时,它会比我右键单击亮区时更透明(或更暗)。

我做了一些谷歌搜索但找不到答案。我最接近的是用机器人创建一个屏幕截图并将其用作缓冲区图像,然后以这种方式分析像素。但是我似乎没有工作,因为无论我在哪里右键单击,我都会为 ARGB 获得 255,255,255,255。有时还有奇怪的东西,比如 AGBA 的 255,234,236,245。

希望你能按照我的意思去做。

这是我的代码。

启动程序的我的主类创建窗口并执行鼠标界面:

import javax.swing.*;

import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.event.*;
import java.awt.image.BufferedImage;

public class mouse {
static DataTransfer data = new DataTransfer();
private static int x,y ;
private static draw object = new draw();
public static int numPoints = 0;
public static final int maxPoints = 4;

public static void main(String[] args) throws Exception {

        JFrame frame = new JFrame();
        Robot robot = new Robot();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("Drawing");
        frame.setSize(500, 400 );
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        frame.add(object);

        data.getRobot(robot,frame.getSize());
        object.addMouseListener(new AL() );
}


public static class AL extends MouseAdapter {


    public void mouseClicked( MouseEvent e){


        if (e.getButton() == MouseEvent.BUTTON1)
        {
            if (numPoints < maxPoints){
            x = e.getX();
            y = e.getY();
            object.drawing(x,y,numPoints);
            System.out.println("NUMBRER"+numPoints);
            numPoints++;
            }
        }
        if (e.getButton() == MouseEvent.BUTTON3)
        { 
            x = e.getX();
            y = e.getY();

            int pixel = data.getScreen().getRGB(x, y);

            int alpha = (pixel >> 24) & 0xff;
            int red = (pixel >> 16) & 0xff;
            int green = (pixel >> 8) & 0xff;
            int blue = (pixel) & 0xff;
            System.out.println("argb: " + alpha + ", " + red + ", " + green + ", " + blue);


        }



    }

}
// Here I tried to access the Robot in the mouse class, but I couldn't so I had to create this transition class in hope of it working
public static class DataTransfer{

    BufferedImage screen ;
    public void getRobot(Robot robot,Dimension size)
    {
        screen =  robot.createScreenCapture(new Rectangle( size ));
    }
    public BufferedImage getScreen(){
        return screen;
    }

}
}

还有我的 Graphics 类,它处理阴影和线条图等...(大多数情况下会针对每种阴影重复自身,因此您无需阅读整个内容)

import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JPanel;


@SuppressWarnings("serial")
public class draw extends JPanel {
    int[] polX = new int[5];
    int[] polY = new int[5];
    int[] aX = new int[mouse.maxPoints];
    int[] aY = new int[mouse.maxPoints];
    float m, c;
    int corners;
    //float triAng = 30;
    float triAng = 255/(((mouse.maxPoints)*(mouse.maxPoints + 1))/2);
    public void drawing( int xx, int yy, int Number ) {
        aX[Number] = xx;
        aY[Number] = yy;
        repaint();

    }

    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(new Color(0,255,0,255));

        g.setColor(new Color(255,0,0,(int)triAng ));


        for(int i=0; i<mouse.numPoints;i++){

            for( int j = i+1; j < mouse.numPoints; j++)
            {

                // Shading the area
                //1) finding the equation of the line

                if( aX[i] != aX[j]){

                    if( aY[i] != aY[j]){

                        //Work out the Gradient
                        m = (float)(aY[j] - aY[i] ) /(aX[j] - aX[i]);
                        c = ((float)aY[i]-(((float)aX[i])*m));
                        for(int k=0;k<mouse.numPoints;k++){
                        if(m<0){
                            //Gradient is negative
                                if(k!= i && k!=j){

                                    //Below
                                    if( aX[k]*m+c < aY[k]){
                                        //Clockwise from origin
                                        // Left Right
                                        // N - no additional corner
                                        // Y- Additional Corner

                                        //  N N
                                        if( ( c >= 400) && (500*m+c) >= 0) {
                                            polX[1] = (int)((400-c)/m);
                                            polY[1] = 400;
                                            polX[2] = 500;
                                            polY[2] = (int)(500*m+c);
                                            corners = 3;

                                        }

                                        //  N Y
                                        else if( c >= 400 && (500*m+c) < 0) {
                                            polX[1] = (int)((400-c)/m);
                                            polY[1] = 400;
                                            polX[2] = (int)((0-c)/m);
                                            polY[2] = 0;
                                            polX[3] = 500;
                                            polY[3] = 0;
                                            corners = 4;
                                        }
                                        //  Y N
                                        else if( c < 400 && (500*m+c) >= 0) {
                                            polX[1] = 0;
                                            polY[1] = 400;
                                            polX[2] = 0;
                                            polY[2] = (int)c;
                                            polX[3] = 500;
                                            polY[3] = (int)(500*m+c);
                                            corners = 4;
                                        }
                                        // YY
                                        else if( c < 400 && (500*m+c) < 0){
                                            polX[1] = 0;
                                            polY[1] = 400;
                                            polX[2] = 0;
                                            polY[2] = (int)c;
                                            polX[3] = (int)((0-c)/m);
                                            polY[3] = 0;
                                            polX[4] = 500;
                                            polY[4] = 0;
                                            corners = 5;
                                        }
                                        //Origin corners
                                        polX[0] = 500;
                                        polY[0] = 400;


                                        g.fillPolygon(polX, polY, corners);
                                    }
                                    //I am here
                                    ///////////////Above
                                    if( aX[k]*m+c > aY[k]){
                                        //Clockwise from origin
                                        // Left Right
                                        // N - no additional corner
                                        // Y- Additional Corner

                                        //  N N
                                        if( ( c <= 400) && (500*m+c) <= 0) {
                                            polX[1] = (int)((0-c)/m);
                                            polY[1] = 0;
                                            polX[2] = 0;
                                            polY[2] = (int)(c);
                                            corners = 3;

                                        }

                                        //  N Y
                                        else if( c <= 400 && (500*m+c) > 0) {
                                            polX[1] = 500;
                                            polY[1] = 0;
                                            polX[2] = 500;
                                            polY[2] = (int)(500*m+c);
                                            polX[3] = 0;
                                            polY[3] = (int)c;
                                            corners = 4;
                                        }
                                        //  Y N
                                        else if( c > 400 && (500*m+c) <= 0) {
                                            polX[1] = (int)((0-c)/m);
                                            polY[1] = 0;
                                            polX[2] = (int)((400-c)/m);
                                            polY[2] = 400;
                                            polX[3] = 0;
                                            polY[3] = 400;
                                            corners = 4;
                                        }
                                        //  Y Y
                                        else if( c > 400 && (500*m+c) > 0){
                                            polX[1] = 500;
                                            polY[1] = 0;
                                            polX[2] = 500;
                                            polY[2] = (int)(500*m+c);
                                            polX[3] = (int)((400-c)/m);
                                            polY[3] = 400;
                                            polX[4] = 0;
                                            polY[4] = 400;
                                            corners = 5;
                                        }
                                        //Origin corners
                                        polX[0] = 0;
                                        polY[0] = 0;


                                        g.fillPolygon(polX, polY, corners);
                                    }
                                }
                            } 

///////////////////////////////////////////////////////////////////////////////////////////////
                        if(m>0){
                            //Gradient is Positive
                                if(k!= i && k!=j){

                                    //Below
                                    if( aX[k]*m+c < aY[k]){
                                        //Clockwise from origin
                                        // Left Right
                                        // N - no additional corner
                                        // Y- Additional Corner

                                        //  N N
                                        if( ( c >= 0 ) && (500*m+c) >= 400) {
                                            polX[1] = 0;
                                            polY[1] = (int)c;
                                            polX[2] = (int)((400-c)/m);
                                            polY[2] = 400;
                                            corners = 3;

                                        }

                                        //  N Y
                                        else if( c >= 0 && (500*m+c) < 400) {
                                            polX[1] = 0;
                                            polY[1] = (int)c;
                                            polX[2] = 500;
                                            polY[2] = (int)(500*m+c);
                                            polX[3] = 500;
                                            polY[3] = 400;
                                            corners = 4;
                                        }
                                        //  Y N
                                        else if( c < 0 && (500*m+c) >= 400) {
                                            polX[1] = 0;
                                            polY[1] = 0;
                                            polX[2] = (int)((0-c)/m);
                                            polY[2] = 0;
                                            polX[3] = (int)((400-c)/m);
                                            polY[3] = 400;
                                            corners = 4;
                                        }
                                        //  Y Y
                                        else if( c < 0 && (500*m+c) < 400){
                                            polX[1] = 0;
                                            polY[1] = 0;
                                            polX[2] = (int)((0-c)/m);
                                            polY[2] = 0;
                                            polX[3] = 500;
                                            polY[3] = (int)(500*m+c);
                                            polX[4] = 500;
                                            polY[4] = 400;
                                            corners = 5;
                                        }
                                        //Origin corners
                                        polX[0] = 0;
                                        polY[0] = 400;


                                        g.fillPolygon(polX, polY, corners);
                                    }
                                    ///////////////Above
                                    if( aX[k]*m+c > aY[k]){
                                        //Clockwise from origin
                                        // Left Right
                                        // N - no additional corner
                                        // Y- Additional Corner

                                        //  N N
                                        if( ( c <= 0) && (500*m+c) <= 400) {
                                            polX[1] = 500;
                                            polY[1] = (int)(500*m+c);
                                            polX[2] = (int)((0-c)/m);
                                            polY[2] = 0;
                                            corners = 3;

                                        }

                                        //  N Y
                                        else if( c <= 0 && (500*m+c) > 400) {
                                            polX[1] = 500;
                                            polY[1] = 400;
                                            polX[2] = (int)((400-c)/m);
                                            polY[2] = 400;
                                            polX[3] = (int)((0-c)/m);
                                            polY[3] = 0;
                                            corners = 4;
                                        }
                                        //  Y N
                                        else if( c > 0 && (500*m+c) <= 400) {
                                            polX[1] = 500;
                                            polY[1] = (int)(500*m+c);
                                            polX[2] = 0;
                                            polY[2] = (int)c;
                                            polX[3] = 0;
                                            polY[3] = 0;
                                            corners = 4;
                                        }
                                        //  Y Y
                                        else if( c > 0 && (500*m+c) > 40){
                                            polX[1] = 500;
                                            polY[1] = 400;
                                            polX[2] = (int)((400-c)/m);
                                            polY[2] = 400;
                                            polX[3] = 0;
                                            polY[3] = (int)c;
                                            polX[4] = 0;
                                            polY[4] = 0;
                                            corners = 5;
                                        }
                                        //Origin corners
                                        polX[0] = 500;
                                        polY[0] = 0;


                                        g.fillPolygon(polX, polY, corners);
                                    }
                                }
                            } 
                        }

                    }

                    else{
                        //code for horizontal line
                    }


                }

                else{
                    //Vertical

                }




            }
            }

        g.setColor(new Color(0,255,0,255));
        for(int i=0; i<mouse.numPoints; i++){
            g.fillOval(aX[i] - 10,aY[i]-10,20,20);
        }


        // Drawing The Line //////
        for(int i=0; i<mouse.numPoints;i++){

            for( int j = i+1; j < mouse.numPoints; j++){
                g.setColor(new Color(0,255,0,255));
                g.drawLine(aX[i], aY[i], aX[j], aY[j]);
                g.setColor(new Color(255,0,0,(int)triAng));

            }
        }   
    }
    }

最佳答案

很难判断一个像素的透明度,因为它一旦被绘制就没有...

透明度仅在您绘制像素时起作用:您在特定背景上绘制具有特定 alpha 的特定颜色。结果,像素本身在绘制后就不再有 alpha ...

你有什么:

  • 您可以轻松地从一个像素中获取 rgb 值(您的代码中已有该代码)...
  • 您还可以获得您通常使用的绘图颜色(如 Color.RED)...
  • 您还可以使用背景颜色(如 Color.WHITE)...

你必须做什么

  • 使用上面的数据,您必须计算 alpha 值!

例如:红色像素(原始 0xFF0000)在白色 (0xFFFFFF) 背景上为 0x7F0000 表示您的透明度为 0x7F (50%)

但这里缺乏我的知识......你当然可以自己计算透明度,我真的希望如此 ^^

关于java - 鼠标下像素的透明度 - Java JFrame,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28399799/

有关java - 鼠标下像素的透明度 - Java JFrame的更多相关文章

  1. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  2. ruby-on-rails - 如何在 Rails Controller Action 上触发 Facebook 像素 - 2

    我有一个ruby​​onrails应用程序。我按照facebook的说明添加了一个像素。但是,要跟踪转化,Facebook要求您将页面置于达到预期结果时出现的转化中。即,如果我想显示客户已注册,我会将您注册后转到的页面作为成功对象进行跟踪。我的问题是,当客户注册时,在我的应用程序中没有登陆页面。该应用程序将用户带回主页。它在主页上显示了一条消息,所以我想看看是否有一种方法可以跟踪来自Controller操作而不是实际页面的转化。我需要计数的Action没有页面,它们是ControllerAction。是否有任何人都知道的关于如何执行此操作的gem、文档或最佳实践?这是进入布局文件的像素

  3. java - 从 JRuby 调用 Java 类的问题 - 2

    我正在尝试使用boilerpipe来自JRuby。我看过guide从JRuby调用Java,并成功地将它与另一个Java包一起使用,但无法弄清楚为什么同样的东西不能用于boilerpipe。我正在尝试基本上从JRuby中执行与此Java等效的操作:URLurl=newURL("http://www.example.com/some-location/index.html");Stringtext=ArticleExtractor.INSTANCE.getText(url);在JRuby中试过这个:require'java'url=java.net.URL.new("http://www

  4. java - 我的模型类或其他类中应该有逻辑吗 - 2

    我只想对我一直在思考的这个问题有其他意见,例如我有classuser_controller和classuserclassUserattr_accessor:name,:usernameendclassUserController//dosomethingaboutanythingaboutusersend问题是我的User类中是否应该有逻辑user=User.newuser.do_something(user1)oritshouldbeuser_controller=UserController.newuser_controller.do_something(user1,user2)我

  5. java - 什么相当于 ruby​​ 的 rack 或 python 的 Java wsgi? - 2

    什么是ruby​​的rack或python的Java的wsgi?还有一个路由库。 最佳答案 来自Python标准PEP333:Bycontrast,althoughJavahasjustasmanywebapplicationframeworksavailable,Java's"servlet"APImakesitpossibleforapplicationswrittenwithanyJavawebapplicationframeworktoruninanywebserverthatsupportstheservletAPI.ht

  6. Observability:从零开始创建 Java 微服务并监控它 (二) - 2

    这篇文章是继上一篇文章“Observability:从零开始创建Java微服务并监控它(一)”的续篇。在上一篇文章中,我们讲述了如何创建一个Javaweb应用,并使用Filebeat来收集应用所生成的日志。在今天的文章中,我来详述如何收集应用的指标,使用APM来监控应用并监督web服务的在线情况。源码可以在地址 https://github.com/liu-xiao-guo/java_observability 进行下载。摄入指标指标被视为可以随时更改的时间点值。当前请求的数量可以改变任何毫秒。你可能有1000个请求的峰值,然后一切都回到一个请求。这也意味着这些指标可能不准确,你还想提取最小/

  7. 【Java 面试合集】HashMap中为什么引入红黑树,而不是AVL树呢 - 2

    HashMap中为什么引入红黑树,而不是AVL树呢1.概述开始学习这个知识点之前我们需要知道,在JDK1.8以及之前,针对HashMap有什么不同。JDK1.7的时候,HashMap的底层实现是数组+链表JDK1.8的时候,HashMap的底层实现是数组+链表+红黑树我们要思考一个问题,为什么要从链表转为红黑树呢。首先先让我们了解下链表有什么不好???2.链表上述的截图其实就是链表的结构,我们来看下链表的增删改查的时间复杂度增:因为链表不是线性结构,所以每次添加的时候,只需要移动一个节点,所以可以理解为复杂度是N(1)删:算法时间复杂度跟增保持一致查:既然是非线性结构,所以查询某一个节点的时候

  8. 【Java入门】使用Java实现文件夹的遍历 - 2

    遍历文件夹我们通常是使用递归进行操作,这种方式比较简单,也比较容易理解。本文为大家介绍另一种不使用递归的方式,由于没有使用递归,只用到了循环和集合,所以效率更高一些!一、使用递归遍历文件夹整体思路1、使用File封装初始目录,2、打印这个目录3、获取这个目录下所有的子文件和子目录的数组。4、遍历这个数组,取出每个File对象4-1、如果File是否是一个文件,打印4-2、否则就是一个目录,递归调用代码实现publicclassSearchFile{publicstaticvoidmain(String[]args){//初始目录Filedir=newFile("d:/Dev");Datebeg

  9. java - 为什么 ruby​​ modulo 与 java/other lang 不同? - 2

    我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.

  10. java - Ruby 相当于 Java 的 Collections.unmodifiableList 和 Collections.unmodifiableMap - 2

    Java的Collections.unmodifiableList和Collections.unmodifiableMap在Ruby标准API中是否有等价物? 最佳答案 使用freeze应用程序接口(interface):Preventsfurthermodificationstoobj.ARuntimeErrorwillberaisedifmodificationisattempted.Thereisnowaytounfreezeafrozenobject.SeealsoObject#frozen?.Thismethodretur

随机推荐