<commit_before>package com.kgp.level;

// Ribbon.java
// <NAME>, April 2005, <EMAIL>

/* A ribbon manages an image which is wider than the game panel's
 width: width >= pWidth

 When a sprite is instructed to move left or right, the 
 sprite doesn't actually move, instead the ribbon moves in
 the _opposite_direction (right or left). The amount of movement 
 is specified in moveSize.

 The image is wrapped around the panel, so at a given moment
 the tail of the image, followed by its head may be visible 
 in the panel.

 A collection of ribbons are managed by a RibbonsManager object.
 */

import java.awt.*;

import javax.swing.*;

import java.awt.geom.AffineTransform;
import java.awt.image.*;

public class Ribbon {
	private BufferedImage im;
	private int width; // the width of the image (>= pWidth)
	private int pWidth, pHeight; // dimensions of display panel

	private int moveSize; // size of the image move (in pixels)
	private boolean isMovingRight; // movement flags
	private boolean isMovingLeft;

	private int xImHead;
	private int head;
	private int tail;
	
	AffineTransform self;
	AffineTransform pre;
	AffineTransform post;
	

	/*
	 * The x-coord in the panel where the start of the image (its head) should
	 * be drawn. It can range between -width to width (exclusive), so can have a
	 * value beyond the confines of the panel (0-pWidth).
	 * 
	 * As xImHead varies, the on-screen ribbon will usually be a combination of
	 * its tail followed by its head.
	 */

	public Ribbon(int w, int h, BufferedImage im, int moveSz) {
		pWidth = w;
		pHeight = h;

		this.im = im;
		width = im.getWidth(); // no need to store the height
		if (width < pWidth)
			System.out.println("Ribbon width < panel width");

		head = 0;

		this.moveSize = moveSz;
		
		self = AffineTransform.getTranslateInstance(0, 0);
		pre = AffineTransform.getTranslateInstance(-width, 0);
		post = AffineTransform.getTranslateInstance(width, 0);
	} // end of Ribbon()

	/*
	 * Increment the xImHead value depending on the movement flags. It can range
	 * between -width to width (exclusive), which is the width of the image.
	 */
	public void update(int dir)
	{

		//backgrounds move inversely from the player
		if (dir > 0)
		{
			head -= moveSize;
		}
		else if (dir < 0)
		{
			head += moveSize;
		}
		head %= width;
		
		self.setToTranslation(head, 0);
		pre.setToTranslation(head-width, 0);
		post.setToTranslation(head+width, 0);

		// System.out.println("xImHead is " + xImHead);
	} // end of update()

	public void display(Graphics2D g)
	/*
	 * Consider 5 cases: 
	 *   head > 0, when xImHead == 0, draw only the im head when xImHead ><commit_msg>Add ribbon scaling when larger than frame<commit_after>package com.kgp.level;

// Ribbon.java
// <NAME>, April 2005, <EMAIL>

/* A ribbon manages an image which is wider than the game panel's
 width: width >= pWidth

 When a sprite is instructed to move left or right, the 
 sprite doesn't actually move, instead the ribbon moves in
 the _opposite_direction (right or left). The amount of movement 
 is specified in moveSize.

 The image is wrapped around the panel, so at a given moment
 the tail of the image, followed by its head may be visible 
 in the panel.

 A collection of ribbons are managed by a RibbonsManager object.
 */

import java.awt.*;

import javax.swing.*;

import java.awt.geom.AffineTransform;
import java.awt.image.*;

public class Ribbon {
	private BufferedImage im;
	private float width; // the width of the image (>= pWidth)
	private float height;
	
	private float wScale, hScale;
	
	private int pWidth, pHeight; // dimensions of display panel

	private int moveSize; // size of the image move (in pixels)
	private boolean isMovingRight; // movement flags
	private boolean isMovingLeft;

	private int head;
	private int tail;
	
	AffineTransform self;
	AffineTransform pre;
	AffineTransform post;
	

	/*
	 * The x-coord in the panel where the start of the image (its head) should
	 * be drawn. It can range between -width to width (exclusive), so can have a
	 * value beyond the confines of the panel (0-pWidth).
	 * 
	 * As xImHead varies, the on-screen ribbon will usually be a combination of
	 * its tail followed by its head.
	 */

	public Ribbon(int w, int h, BufferedImage im, int moveSz) {
		pWidth = w;
		pHeight = h;

		this.im = im;
		width = im.getWidth(); // no need to store the height
		if (width < pWidth)
			System.out.println("Ribbon width < panel width");

		head = 0;

		this.moveSize = moveSz;
		
		self = AffineTransform.getTranslateInstance(0, 0);
		pre = AffineTransform.getTranslateInstance(-width, 0);
		post = AffineTransform.getTranslateInstance(width, 0);
		
		height = Math.max(im.getHeight(), pHeight);
		width *= pHeight/height;
		wScale = pHeight/height;
		hScale = pHeight/height;
	} // end of Ribbon()

	/*
	 * Increment the xImHead value depending on the movement flags. It can range
	 * between -width to width (exclusive), which is the width of the image.
	 */
	public void update(int dir)
	{

		//backgrounds move inversely from the player
		if (dir > 0)
		{
			head -= moveSize;
		}
		else if (dir < 0)
		{
			head += moveSize;
		}
		head %= width;
		
		self.setToTranslation(head, 0);
		pre.setToTranslation(head-width, 0);
		post.setToTranslation(head+width, 0);
		
		self.scale(wScale, hScale);
		pre.scale(wScale, hScale);
		post.scale(wScale, hScale);

		// System.out.println("xImHead is " + xImHead);
	} // end of update()

	public void display(Graphics2D g)
	/*
	 * Consider 5 cases: 
	 *   head > 0, when xImHead == 0, draw only the im head when xImHead >
{'fraction_non_alphanumeric': 0.0951867816091954, 'fraction_numerical': 0.006824712643678161, 'mean_word_length': 3.347384855581577, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5874540', 'n_tokens_mistral': 1916, 'n_tokens_neox': 1685, 'n_words': 693}