itextpdf : rectangle modify width - java

Is it possible to modify width of existing rectangle ?
I have :
#Override
public void onGenericTag(PdfWriter writer, Document document, Rectangle rect, String text){
Rectangle rectangle = new Rectangle(rect);
//something like that :
rectangle.setWidth(400f);
}

You can (and should) not use a method called setWidth(). Whatever that method would do would be very ambiguous.
Suppose that you would have a rectangle with lower-left x coordinate equal to 36 and with upper-right x coordinate equal to 559. (I didn't choose these numbers at random: those are the default margins inside the default A4 page when using iText.) Now when you change the width of such a rectangle: do you mean to extend the rectangle to the left, to the right, or both? I hope this example shows that having a setWidth() method doesn't make sense.
Instead, you should use setLeft() or setRight() when you change the x value of the left or right coordinate of the rectangle, you automatically change the width and there can be no confusion about the direction in which you're changing the width.

Related

(UPDATE)I want to draw circles within circles that get increasingly small by 10 pixels in diameter that are placed around a ring

So my ultimate goal is to draw 50 circles(with processing.core.PApplet) structured around a ring that transitions colors like a neon sign so that it would look like Psychadelics
The circles have to be random sizes and with a diameter of under 210 pixels the circles have to have 8 bars with each "shells" changing colors in order, and at the center, there must be an empty circle with the same color as the background.
Right now I am trying to break this problem into lots of small problems, and currently, I am struggling to make the spaces between the bars to be equally spaced.
Culprit:
(I found the problem to be the ratio between the bar size and the circle size, due to the undefined range random sizing it made seemingly empty circles)
Here is my next problem, The circles seem to be vibrating instead of remaining static, I want the 50 circles to be static while the colors on each shell change each frame.
Here is the part where I tell it to draw smaller and smaller circles
This is the "Drawing component"
public Donut(float x, float y , float d) { //constructor
this.x =x;
this.y =y;
diameter=d;
}
public void draw(PApplet p) {
p.circle(x, y, diameter);
float bar=(float)(Math.random()*(1-10)-1)+1;
for(int i =0; i<8; i++) {
bar+=10;
p.fill(REDS[i],GREENS[i],BLUES[i]);
p.circle(x, y, diameter-bar);
}
}
And here is the part where I tell it to have random sizes and positions(still haven't told it to be placed around a ring yet) //This is the Main Class
public class Psychadelics extends PApplet{
Donut [] DonutList = new Donut [50];
public static void main(String[] args) {
PApplet.main("test.Psychadelics");
}
public void settings() {
size(SCR_W, SCR_H);
}
public void setup() {
for(int i =0; i<DonutList.length;i++) {
float x = (float)(Math.random()*600);
float y = (float)(Math.random()*400);
float diameter = (float)(Math.random()*210);
DonutList [i]= new Donut(x,y,diameter);
}
I have another drawing method inside the main class to tell the Donut class to keep drawing and to keep updating it.
I expect each circle to remain static and to transition colors, each frame but my actual results were the circles each with different colors on each shell vibrating on their specified coordinates
The vibration is caused by float bar=(float)(Math.random()*(1-10)-1)+1;. Because bar is determined inside the draw function, the exact diameter will be slightly different on each frame. Instead, you should create an array of these random floats in the constructor and use random_diameter[i] in the draw loop, so the sizes of the inner circles are created random, but remain constant.
The color remaining constant is caused by p.fill(REDS[i],GREENS[i],BLUES[i]);. You assign specific colors, based on the index, to a circle that is also based on the index. This is where you should use random. For red, green and blue use int(Math.random(0,255));. That way, on each frame, a random color is generated for each circle. If you want more gradual color changes, you'll need to store the color for each circle and add/subtract a small random number. If you want to limit the number of colors, you can use the function you have now, but instead of i, use a random number with the size of the array.
I hope you see that, interestingly, the problems/solutions are each others inverse :)

Is it possible to compare an index in an array to a color?

I'm trying to code a board game and have created the board with for loops. As it stands now I can click a rectangle and am able to change its color. Now I want to have it where if I click a rectangle that is a certain color to change it to a specific color. For example iff it's blue then make it grey. However I get the error Unlikely argument type for equals(): Color seems to be unrelated to Rectangle and my squares turn black instead. Here is my code. Thank you.
public void game (MouseEvent eventGame) {
for(Rectangle r: rectangles) {
if (r.equals(Color.BLUE)) {
r.setOnMouseClicked(event->{
r.setFill(Color.GREY);
});
} else { r.setOnMouseClicked(event->{
r.setFill(Color.BLACK);
});}
}
}
I should also mention when creating the Array I do this: r.setFill(Color.BLUE);.
By calling
r.equals(Color.BLUE)
you are trying to compare a rectangle instance with a Color type. Looking at the API of Rectangle, its equals-method is described as follows:
Checks whether two rectangles are equal.
The result is true if and only if the argument is not null and is a Rectangle object that has the same upper-left corner, width, and height as this Rectangle.
Instead you need to compare the actual Color of the rectangle by calling
r.getFill().equals(Color.WHITE)
(see Post Can you return the color of a rectangle object in java?)
Hope, I could help you.

Using negative height in g.fillRect()

Hello I am trying to paint a bar graph using g.fillRect() to make each bar.
The bars will start at the grid height, y: 700, and fill upwards to y: 350 for example.
I am unable to set a negative x-height for my bar:
public void paint(Graphics g) {
for (Bar b : this.bars) {
g.fillRect(b.x_location,b.y_location,b.width,b.height * (-1));
}
}
This won't paint the bars.
Fix:
public void paint(Graphics g) {
for (Bar b : this.bars) {
g.fillRect(b.x_location,b.y_location - b.height,b.width,b.height);
}
}
You can't, because of the way the API works, it extends down and to the right. Instead, subtract the distance from the y coordinate and maintain the height as a positive value
From the JavaDocs
Fills the specified rectangle. The left and right edges of the rectangle are at x and x + width - 1. The top and bottom edges are at y and y + height - 1. The resulting rectangle covers an area width pixels wide by height pixels tall. The rectangle is filled using the graphics context's current color.
While not a direct duplicate, this example demonstrates the basic concepts

Line intersect Object

Rectangle rEnemy = new Rectangle();
rEnemy.setBounds(0,0,40,40);
Rectangle rPlayer = new Rectangle();
rPlayer.setBounds(200,0,40,40);
my question: how can i make detection/intersection for rPlayer using something like "line" long for 100px?
example: rEnemy is at (0,0), rPlayer is at (100,0) and now rPlayer is intersecting line. there is a method rPlayer.intersect(rEnemy); but i what keep them 100px away from each other and get detection. of course i mean line which is aplicable for others positions of our rectangles because rPlayer and rEnemy are still in move.
I'm not exactly sure what you want, it sounds like you want to check if they are 100px away but intersecting on the x (or y) coordinate, not a 100px radius.
If you're trying to detect if they are within that bounds, you could merely make 1 or 2 rectangles that extend 100px in the x and y coordinates of either the enemy or player and check if the other piece intersects that.
But it would probably be better to just add 100px to the x coordinate of the enemy and check if the player comes within that boundary, that way you aren't drawing more rectangles. I just mentioned that before because I thought it might be easier to visualize.
I just slove this by creating 3rd rect starting from rEnemy cords and making it width and height to rPlayer cords, then:
double calculate = Math.pow(Math.abs(rPlayer.x-rEnemy.x-Camera.posX), 2)+Math.pow(Math.abs(rEnemy.y-rPlayer.y+Camera.posY), 2);
int distance = (int)Math.sqrt(calculate);
variable distance is value of px which separate rPlayer and rEnemy at any position in shortest way.

Rectangle2D getY() returns value less than getMaxY()

As I interpret the documentation, getY() is supposed to return the upper-left hand Y coordinate of a rectangle; i.e the biggest Y coordinate. However, when calling getMaxY() (which is inherited from the RectangularShape class) I get back a bigger value!
In code:
Path2D bg = polygons.get(polyId2GeoId.get(id));
Rectangle2D bgBox = bg.getBounds2D();
boolean omgwtfbbqrsvp = bgBox.getY()<bgBox.getMaxY();
omgwtfbbqrsvp is true... What am I missing here?
My x values contain negative numbers idk if that makes a difference. It sames like bgBox.getY() == bgBox.getMinY() (which is wrong if getY is upper coord) but bgBox.getX() == bgBox.getMinX() (which is correct if getX is left coord). The heights and widths appear correct.
Thanks!
The problem is that the coordinate system used here has its point (0,0) on the upper left corner. The point (n,n) is on the lower right corner.

Categories

Resources