Dlaczego to mi każe castować na BufferedImage?

0

Witam! Mam taki kawałek kodu:

	BufferedImage buf = robot.createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
		
		screenShots[0] = buf.getScaledInstance(290, 190, BufferedImage.SCALE_SMOOTH);

w drugiej linijce każe mi castować z Image na BufferedImage, a to jest typ BufferedImage! Nie rozumiem o co chodzi...

0

No musisz rzutować, bo metoda getScaledInstance zwraca, niezgodnie z Twoimi wyobrażeniami, typ Image.
Masz spore ego, piszesz o sobie z dużej litery. ;)

0

Jak zadasz pytanie w miejscu do tego przeznaczonym (komentarz nie jest takim miejscem), to Ci powiem co masz zrobić.

0
bogdans napisał(a):

No musisz rzutować, bo metoda getScaledInstance zwraca, niezgodnie z Twoimi wyobrażeniami, typ Image.
Masz spore ego, piszesz o sobie z dużej litery. ;)

Co ma zrobić?

0

Co ma zrobić? W czyim imieniu pytasz?

0
bogdans napisał(a):

Co ma zrobić? W czyim imieniu pytasz?

Ja rozumiem, że większość informatyków/programistów ma specyficzne poczucie humoru, ale to nie jest śmieszne. Co * ** mam** * zrobić?

1

Ja, do konwersji Image => BufferdImage stosuję taką funkcję:

    BufferedImage toBufferedImage(Image image) throws HeadlessException, IllegalArgumentException, InterruptedException
    {
        BufferedImage bi = null;
        boolean hasAlpha = false;
        try
        {
            hasAlpha = hasAlpha(image);
            GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();            
            int transparency = Transparency.OPAQUE;
            if (hasAlpha)
            {
                transparency = Transparency.BITMASK;
            }
            GraphicsDevice gs = ge.getDefaultScreenDevice();
            GraphicsConfiguration gc = gs.getDefaultConfiguration();
            bi = gc.createCompatibleImage(image.getWidth(null),image.getHeight(null),transparency);
        }
        catch (HeadlessException | IllegalArgumentException | InterruptedException e)
        {
            throw e;
        }

        if (bi == null)
        {
            // Create a buffered image using the default color model
            int type = BufferedImage.TYPE_INT_RGB;
            if (hasAlpha)
            {
                type = BufferedImage.TYPE_INT_ARGB;
            }
            bi = new BufferedImage(image.getWidth(null),image.getHeight(null),type);
        }

        // Copy image to buffered image
        Graphics g = bi.createGraphics();

        // Paint the image onto the buffered image
        g.drawImage(image,0,0,null);
        g.dispose();

        return bi;
    }

1 użytkowników online, w tym zalogowanych: 0, gości: 1