2017-07-28 20 views
1
/** 
* 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方法有錯誤,因爲demo程序沒有錯誤。drawImage錯誤;沒有找到drawImage的方法

如果你是好奇這是我複製演示:

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; 
 
    } 
 
}

+0

什麼錯誤? –

+0

該錯誤在scalePicture方法中的drawImage中。它說沒有找到合適的方法drawImage –

回答

0

貌似錯誤是行:

g2V2.drawImage(canvas2.getImage(), scaleTransform, null); - 有在java.awt.Graphics沒有這樣的方法接口。

您應該使用方法與其他簽名: drawImage(Image img, int x, int y, ImageObserver observer) - 見here

+0

我意識到這是錯誤,但我需要縮放到原來的0.75 x 0.75的圖像。我只是在複製一個演示,演示沒有錯誤,除了我的。所以我想知道是否有辦法做到這一點,我也可以縮小我的形象。 –

+0

在這裏尋找更多關於縮放的信息:https://stackoverflow.com/questions/15558202/how-to-resize-image-in-java –

+0

我在看你在答案中發送的鏈接,我看到那裏是縮放的一個選項,但它僅用於int,是否可以將其轉換爲double? –