Question

16. What do the following two code fragments do given integers a and b? a-a + b b-a b just print the code you wrote with comments RGBA color format. Some of Javas classes (Bufferedlmage, PixelGrabber) use a special encoding called RGBA to store the color of each pixel. The format consists of tour integers, representing the red, green, and blue intensities from 0 (not present) to 255 (fully used), and also the alpha transparency value from O (transparent) to 255 (opaque). The four 8-bit integers are compacted into a single 32-bit integer. Write a code fragment to extract the four components from the RGBA integer, and to go the other way.

0 0
Add a comment Improve this question Transcribed image text
Answer #1
import java.util.Scanner;

public class RGBA {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter a 32-bit integer: ");
        int num = in.nextInt();
        int r = (num >> 24) & 0xFF;
        int g = (num >> 16) & 0xFF;
        int b = (num >> 8) & 0xFF;
        int a = num & 0xFF;
        System.out.println("Red = " + r);
        System.out.println("Green = " + g);
        System.out.println("Blue = " + b);
        System.out.println("Alpha = " + a);
    }

}

Enter a 32-bit integer: Red 127 Green = 2 5 5 Blue = 255 Alpha = 248 Process finished with e xit code 0

Add a comment
Know the answer?
Add Answer to:
16. What do the following two code fragments do given integers a and b? a-a +...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • the following python code edits BMP image file to negative but I need help with modifying...

    the following python code edits BMP image file to negative but I need help with modifying this code so it turns it the same BMP image file into a black and white instead of a negative. heres python code I have so far [pasted below] ## # This program processes a digital image by creating a negative of a BMP image. # from io import SEEK_CUR from sys import exit def main() : filename = input("Please enter the file name:...

  • Python Project

    AssignmentBitmap files map three 8-bit (1-byte) color channels per pixel. A pixel is a light-emitting "dot" on your screen. Whenever you buy a new monitor, you will see the pixel configuration by its width and height, such as 1920 x 1080 (1080p) or 3840x2160 (4K). This tells us that we have 1080 rows (height), and each row has 1920 (width) pixels.The bitmap file format is fairly straight forward where we tell the file what our width and height are. When...

  • could you please help me with this problem, also I need a little text so I...

    could you please help me with this problem, also I need a little text so I can understand how you solved the problem? import java.io.File; import java.util.Scanner; /** * This program lists the files in a directory specified by * the user. The user is asked to type in a directory name. * If the name entered by the user is not a directory, a * message is printed and the program ends. */ public class DirectoryList { public static...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT