1

For my code that I am working on. I have made a small design. I was able to, using the code that I had to make the design into an image. But when I try to reuse the image, nothing happens with my code.

How do I make it so that my code will take the image it makes and put it into a larger canvas so that I can eventually flip the image multiple times? My code feels kind of cluttered and I think the first method prevents it from becoming larger but I am unsure. Currently I can only make the code make the design into an image. This is what I have:

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();
    }
}
Jonny Henly
  • 4,023
  • 4
  • 26
  • 43
J. Listn
  • 11
  • 2

0 Answers0