首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >drawImage错误;找不到用于drawImage的方法

drawImage错误;找不到用于drawImage的方法
EN

Stack Overflow用户
提问于 2017-07-29 02:45:02
回答 1查看 116关注 0票数 1
代码语言:javascript
复制
/**
 * The purpose of this program is to make an image and turn it into a kaleidoscope
 *
 * @author (Danny Meijo)
 * @version (07/27/2017)
 */
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.awt.geom.Ellipse2D;

public class KaleidoscopeImage
{
    private Picture canvas = null;
    private Picture canvas2 = null;
    private Picture pictureObj = null;
    private Picture scaledPicture = null;
    private Picture clippedPicture = null;
    private Graphics g = null;
    private Graphics g2 = null;
    private Graphics gV2 = null;
    private Graphics g2V2 = null;
    private Graphics gV3 = null;
    private Graphics g2V3 = null;


    KaleidoscopeImage(Picture Canvas, Picture image, Picture Canvas2)
    {
        canvas = Canvas;
        canvas2 = Canvas2;
        pictureObj = image;
        g = canvas.getGraphics();
        g2 = (Graphics2D)g;
    }
    public Picture firstPanel()
    {
        g2.drawImage(pictureObj.getImage(), 0, canvas.getHeight() / 2, null);


        Pixel bottomLeftPixel = null;
        Pixel topRightPixel = null;
        Color sourceColor1 = null;


        for(int ty = 0, by = canvas.getHeight(); ty < canvas.getHeight() / 2; ty++, by--)
        {
            for(int lx = 0, rx = canvas.getWidth(); lx < canvas.getWidth() / 2; lx++, rx--)
            {
                bottomLeftPixel = canvas.getPixel(lx, by - 1);
                sourceColor1 = bottomLeftPixel.getColor();
                topRightPixel = canvas.getPixel(rx - 1, ty);
                topRightPixel.setColor(sourceColor1);
            }
        }


        Pixel sourcePixel = null;
        Pixel targetPixel = null;
        Color sourceColor2 = null;
        Color targetColor = null;


        for(int y = 0; y < canvas.getHeight() / 2; y++)
        {
            for(int lx = 0, rx = canvas.getWidth(); lx < canvas.getWidth() / 2; lx++, rx--)
            {
                sourcePixel = canvas.getPixel(rx - 1,y);
                sourceColor2 = sourcePixel.getColor();
                targetPixel = canvas2.getPixel(lx,y);
                targetPixel.setColor(sourceColor2);
            }
        }

        return canvas2;

    }
    public Picture secondPanel()
    {
        Pixel leftPixel = null;
        Pixel rightPixel = null;
        Color sourceColor = null;


        for(int y = 0; y < canvas2.getHeight() / 2; y++)
        {
            for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
            {
                leftPixel = canvas2.getPixel(lx,y);
                sourceColor = leftPixel.getColor();
                rightPixel = canvas2.getPixel(rx - 1, y);
                rightPixel.setColor(sourceColor);
            }
        }

        return canvas2;

    }

    public Picture thirdPanel()
    {
        Pixel topPixel = null;
        Pixel bottomPixel = null;
        Color sourceColor = null;

        for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
        {
            for(int ty = 0, by = canvas2.getHeight(); ty < canvas2.getHeight() / 2; ty++, by--)
            {
                topPixel = canvas2.getPixel(rx - 1, ty);
                sourceColor = topPixel.getColor();
                bottomPixel = canvas2.getPixel(rx - 1, by - 1);
                bottomPixel.setColor(sourceColor);
            }
        }

        return canvas2;

    }


    public Picture fourthPanel()
    {
        Pixel leftPixel = null;
        Pixel rightPixel = null;
        Color sourceColor = null;


        for(int lx = 0, rx = canvas2.getWidth(); lx < canvas2.getWidth() / 2; lx++, rx--)
        {
            for(int ty = 0, by = canvas2.getHeight(); ty < canvas2.getHeight() / 2; ty++, by--)
            {
                leftPixel = canvas2.getPixel(rx - 1, by - 1);
                sourceColor = leftPixel.getColor();
                rightPixel = canvas2.getPixel(lx, by - 1);
                rightPixel.setColor(sourceColor);
            }
        }

        return canvas2;

    }
    public Picture scalePicture(double xFactor, double yFactor)
    {
        AffineTransform scaleTransform = new AffineTransform();
        scaleTransform.scale(xFactor, yFactor);
        scaledPicture = new Picture((int)(canvas2.getWidth() * xFactor), (int)(canvas2.getHeight() * yFactor));
        gV2 = scaledPicture.getGraphics();
        g2V2 = (Graphics2D)gV2;
        g2V2.drawImage(canvas2.getImage(), scaleTransform, null);

        return scaledPicture;

    }


    public Picture clipPicture(Color color)
    {
        Picture canvas3 = new Picture(canvas2.getWidth(), canvas2.getHeight());

        Pixel sourcePixel = null;
        Pixel targetPixel = null;
        Color sourceColor = null;
        Color targetColor = null;

        for(int y = 0; y < canvas2.getHeight(); y++)
        {
            for(int x = 0; x < canvas.getWidth(); x++)
            {
                sourcePixel = canvas2.getPixel(x,y);
                sourceColor = sourcePixel.getColor();
                targetPixel = canvas3.getPixel(x,y);
                targetPixel.setColor(sourceColor);
            }
        }

        gV3 = canvas3.getGraphics();
        g2V3 = (Graphics2D)gV3;


        canvas3.setAllPixelsToAColor(color);
        Ellipse2D.Double clip = new Ellipse2D.Double(0,0, canvas3.getHeight(), canvas3.getWidth());
        g2V3.setClip(clip);
        g2V3.drawImage(canvas2.getImage(), 0, 0, canvas3.getHeight(), canvas3.getWidth(), null);

        return canvas3;

    }
}

对不起,这是我的第一篇文章,而且我对java也很陌生,因为我整个夏天都在学习它。我不确定如何将它裁剪成我需要的部分,但我遇到的问题是scalePicture方法。我复制了我在演示程序中看到的内容,将图像缩小到0.75x0.75。但是,在我的程序中,drawImage方法有一个错误,在演示程序中没有错误。

如果你很好奇,这就是我复制的演示:

代码语言:javascript
复制
import java.awt.geom.AffineTransform;
import java.awt.Graphics;
import java.awt.Graphics2D;

class ScalingDemo
{
    private Picture originalPicture = null;
    private Picture newPicture = null;
    private Graphics g = null;
    private Graphics2D g2 = null;
    
    ScalingDemo(Picture pic)
    {
        originalPicture = pic;
    }
    
    public Picture scalePicture(double xFactor, double yFactor)
    {
        AffineTransform scaleTransform = new AffineTransform();
        scaleTransform.scale(xFactor, yFactor);
        newPicture = new Picture((int)(originalPicture.getWidth()*xFactor), (int)(originalPicture.getHeight()*yFactor));
        g = newPicture.getGraphics();
        g2 = (Graphics2D)g;
        g2.drawImage(originalPicture.getImage(), scaleTransform, null);
        
        return newPicture;
    }
}

EN

回答 1

Stack Overflow用户

发布于 2017-07-29 03:10:51

看起来错误在第行:

g2V2.drawImage(canvas2.getImage(), scaleTransform, null); -在java.awt.Graphics接口中没有这样的方法。

您应该使用带有另一个签名的方法:drawImage(Image img, int x, int y, ImageObserver observer) -请参阅here

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45380387

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档