如何在较大的画布中使用创建的图像

时间:2016-08-04 01:40:07

标签: java image graphics graphics2d java-canvas

对于我正在处理的代码。我做了一个小设计。我能够使用我必须将设计的代码变成图像。但是当我尝试重用图像时,我的代码没有任何反应。

我如何制作它以便我的代码将其制作的图像放入更大的画布中,以便最终可以多次翻转图像?我的代码感觉有点杂乱,我认为第一种方法可以防止它变大,但我不确定。目前我只能让代码将设计变成图像。这就是我所拥有的:

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Color;

class PicturePattern {
    private Picture newCanvas1 = null;
    private Graphics g = null;
    private Graphics2D g2 = null;
    private Picture pic1 = null;
    private Picture pic2 = null;
    private Color color = null;

    PicturePattern(Picture canv, Picture p1) {
        newCanvas1 = canv;
        newCanvas1.setAllPixelsToAColor(Color.YELLOW);
        g = newCanvas1.getGraphics();
        g2 = (Graphics2D)g;  
        //pic1 = p1;
        //pic2 = p2;
        //color = col;
    }

    public void drawARectangle(int x1, int y1, int width, int height, Color color) {
        g.setColor(color);
        g.drawRect(x1, y1, width, height);      
    }

    public void drawAFilledRectangle(int x1, int y1, int width, int height, Color color) {
        g.setColor(color);
        g.fillRect(x1, y1, width, height);      
    }

    public void drawALine(int x1, int y1, int x2, int y2, Color color) {
        g.setColor(color);
        g.drawLine(x1, y1, x2, y2);
    }

    public Picture drawPicture() {
        //g2.setColor(Color.RED);
        //g2.fillOval(700, 30, 100, 100); 
        //g2.drawImage(pic1.getImage(),0, 0, null);
        //g2.drawImage(pic2.getImage(),45, 200, null);
        //g2.drawImage(pic2.getImage(),145, 100, null);
        //g2.drawImage(pic2.getImage(),145, 300, null);
        return newCanvas1;
    }   

}

public class PicturePatternTester1 {
    public static void main(String[] args) {
        Picture canvas = new Picture(200,200);

        Picture picture = new Picture("untitled.png");
        //Picture picture2 = new Picture("moon.jpg");
        PicturePattern draw = new PicturePattern(canvas, picture);
        draw.drawAFilledRectangle(0,0, 200, 200, Color.CYAN);
        draw.drawARectangle(50, 50, 150, 150, Color.RED);
        draw.drawALine(50, 50, 200, 200, Color.BLACK);
        draw.drawALine(200, 50, 50, 200, Color.BLACK);

        Picture picture1 = draw.drawPicture();
        picture1.show();
        picture1.write("pattern.jpg");

        /*
        Picture canvas1 = new Picture(800, 800);
        Picture picture2 = new Picture("pattern.jpg");
        PicturePattern draw1 = new PicturePattern(canvas1, picture2);
        */

        canvas = draw.drawPicture();

        canvas.show();
    }
}

0 个答案:

没有答案