我是java的新手并且决定制作一个程序:当你左键单击时在窗口中设置一个点。然后再次点击时的另一点。依此类推...然后它将所有点与线条连接起来,并根据线条边上的点数对线条的一边进行着色。(像这样)
问题在这里开始:
现在我还需要一个功能。当我右键单击时,我想要显示鼠标坐标处像素的透明度。因此,当点击中间时,它会显示它比我右键单击亮区时更透明(或更暗)。
我做了一些谷歌搜索,但找不到答案。我最接近的是用机器人创建截图并将其用作缓冲图像,然后分析像素。然而,我似乎没有工作,因为无论我在哪里右键单击我得到255,255,255,255 ARGB。有时像AGBA这样奇怪的东西,如255,234,236,245。
希望你能按照我的目标去做。
这是我的代码。
我的Main类为程序加注星星,创建窗口并执行鼠标界面:
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));
}
}
}
}
答案 0 :(得分:1)
很难说出一个像素的透明度,因为一旦它被绘制就没有了...
透明度仅在您绘制像素时有效:您在特定背景上绘制具有特定Alpha的特定颜色。结果,一旦绘制了像素本身就没有alpha了......
你有什么:
你要做什么
例如:白色(0xFFFFFF)背景上的红色像素(原始0xFF0000)为0x7F0000意味着您的透明度为0x7F(50%)
但这里缺乏我的知识......你肯定可以自己计算透明度,我真希望如此^^