How to remove a shape without using background(255); - java

I am trying to make the drawRedlines function appear and disappear inside the mousePressed function and mouseReleased function. I have tried using background(255); in my mouseReleased but it also removes my crossBars function for some reason. I tried changing the cords for the redLine rect's to 0 but it still is in the same position when I release the mouse.
final int NUM_LINES = 15;
final int LINE_THICK = 20;
final int CONVERGE_VERTICAL_OFFSET = 175;
final int CONVERGE_VERTICAL_OFFSET2 = 100;
int capSize = 20;
int width = 500;
int height = 500;
int x1 = 30;
int crossWidth = width - (30*2);
int y1 = height - 20;
int crossHeight = 20;
int capX = x1;
int capY = y1 + crossHeight/2;
int capX2 = width - x1;
int diffY = 60;
int diffX = 10;
int redLinex = 145;
int redLinex2 = 350;
int redLiney = 0;
int redlineHeight = height;
int redlineWidth = 5;
void setup(){
size(500,500);
drawConvergingvertical();
horizontalCross();
drawYellowlines();
}
void draw(){
}
void mousePressed(){
drawRedlines();
}
void mouseReleased(){
drawConvergingvertical();
horizontalCross();
drawYellowlines();
}
void horizontalCross(){
for(int rows = 0; rows < NUM_LINES; rows++){
//println(rows, capX, capX2); Used this to Debug;
drawCrossbars();
y1 = y1 - diffY;
capY = capY - diffY - 1;
x1 = x1 + diffX;
capX = capX + diffX;
crossWidth = crossWidth - diffX*2;
capX2 = capX2 - diffX;
crossHeight = crossHeight - 2;
capSize = capSize - 2;
}
}
void drawCrossbars(){
rect(x1,y1,crossWidth,crossHeight);
ellipse(capX,capY,capSize,capSize);
ellipse(capX2,capY,capSize,capSize);
}
void drawConvergingvertical(){
fill(0);
stroke(0);
quad(width/2-CONVERGE_VERTICAL_OFFSET,height,width/2-CONVERGE_VERTICAL_OFFSET2,0,width/2-CONVERGE_VERTICAL_OFFSET2+LINE_THICK,0,width/2-CONVERGE_VERTICAL_OFFSET+LINE_THICK,height);
quad(width/2+CONVERGE_VERTICAL_OFFSET,height,width/2+CONVERGE_VERTICAL_OFFSET2,0,width/2+CONVERGE_VERTICAL_OFFSET2-LINE_THICK,0,width/2+CONVERGE_VERTICAL_OFFSET-LINE_THICK,height);
}
void drawYellowlines(){
fill(255,255,0);
stroke(255,255,0);
rect(150, height - 100, 200, 10);
rect(150, 100, 200, 10);
}
void drawRedlines(){
redLinex = 145;
redLinex2 = 350;
redLiney = 0;
redlineHeight = height;
redlineWidth = 5;
fill(255,0,0);
stroke(255,0,0);
rect(redLinex,redLiney,redlineWidth,redlineHeight);
rect(redLinex2,redLiney,redlineWidth,redlineHeight);
}

How to remove a shape without using background(255); [...]
Not at all. You cant "remove" something what is drawn in the window. All you can do is to draw something different, what covers the shape. background doesn't remove anything. It just draws an uniform color in the entire window.
You've to redraw the entire scene in draw().
Use the built-in variable mousePressed to identify if the mouse is pressed and call drawRedlines() dependent on the state of the variable. Furthermore you've to reset the state of some variables in in every frame:
void draw() {
capSize = 20;
x1 = 30;
crossWidth = width - (30*2);
y1 = height - 20;
crossHeight = 20;
capX = x1;
capY = y1 + crossHeight/2;
capX2 = width - x1;
diffY = 60;
diffX = 10;
redLinex = 145;
redLinex2 = 350;
redLiney = 0;
redlineHeight = height;
redlineWidth = 5;
background(200);
drawConvergingvertical();
horizontalCross();
drawYellowlines();
if (mousePressed) {
drawRedlines();
}
}
Note, it is not necessary to draw anything in the mouse callback functions mousePressed respectively mouseReleased.
void mousePressed(){
}
void mouseReleased(){
}
Side note: Remove declaration of the variables width and height from your code. This are built-in variables, so it is not necessary to declare them at all, especially since the dependent variables are set in draw.
int width = 500;
int height = 500;

The draw() method is a loop. If you draw your stuff inside that loop, you can update it in real time. I modified your loop a little bit so the red lines appears only while the left mouse button si down:
final int NUM_LINES = 15;
final int LINE_THICK = 20;
final int CONVERGE_VERTICAL_OFFSET = 175;
final int CONVERGE_VERTICAL_OFFSET2 = 100;
int capSize = 20;
int width = 500;
int height = 500;
int x1 = 30;
int crossWidth = width - (30*2);
int y1 = height - 20;
int crossHeight = 20;
int capX = x1;
int capY = y1 + crossHeight/2;
int capX2 = width - x1;
int diffY = 60;
int diffX = 10;
int redLinex = 145;
int redLinex2 = 350;
int redLiney = 0;
int redlineHeight = height;
int redlineWidth = 5;
boolean redLineVisible = false;
void setup(){
size(500,500);
//drawConvergingvertical();
//horizontalCross();
//drawYellowlines();
}
void draw(){
background(255);
drawConvergingvertical();
horizontalCross();
drawYellowlines();
drawRedlines();
}
void mousePressed(){
//drawRedlines();
redLineVisible = true;
}
void mouseReleased(){
//drawConvergingvertical();
//horizontalCross();
//drawYellowlines();
//drawRedlines();
redLineVisible = false;
}
void horizontalCross(){
for(int rows = 0; rows < NUM_LINES; rows++){
//println(rows, capX, capX2); Used this to Debug;
drawCrossbars();
y1 = y1 - diffY;
capY = capY - diffY - 1;
x1 = x1 + diffX;
capX = capX + diffX;
crossWidth = crossWidth - diffX*2;
capX2 = capX2 - diffX;
crossHeight = crossHeight - 2;
capSize = capSize - 2;
}
}
void drawCrossbars(){
rect(x1,y1,crossWidth,crossHeight);
ellipse(capX,capY,capSize,capSize);
ellipse(capX2,capY,capSize,capSize);
}
void drawConvergingvertical(){
fill(0);
stroke(0);
quad(width/2-CONVERGE_VERTICAL_OFFSET,height,width/2-CONVERGE_VERTICAL_OFFSET2,0,width/2-CONVERGE_VERTICAL_OFFSET2+LINE_THICK,0,width/2-CONVERGE_VERTICAL_OFFSET+LINE_THICK,height);
quad(width/2+CONVERGE_VERTICAL_OFFSET,height,width/2+CONVERGE_VERTICAL_OFFSET2,0,width/2+CONVERGE_VERTICAL_OFFSET2-LINE_THICK,0,width/2+CONVERGE_VERTICAL_OFFSET-LINE_THICK,height);
}
void drawYellowlines(){
fill(255,255,0);
stroke(255,255,0);
rect(150, height - 100, 200, 10);
rect(150, 100, 200, 10);
}
void drawRedlines(){
if (redLineVisible) {
redLinex = 145;
redLinex2 = 350;
redLiney = 0;
redlineHeight = height;
redlineWidth = 5;
fill(255,0,0);
stroke(255,0,0);
rect(redLinex,redLiney,redlineWidth,redlineHeight);
rect(redLinex2,redLiney,redlineWidth,redlineHeight);
}
}
Have fun.

Related

Java Graphic problem with getting correct generation on my graphic lines

I am trying to finish a project that I have been working on for fun on Java, and I'm genuinely trying to figure this out. Here is the code. I put a comment where the problem starts.
// Lab04vst.java
//
import java.awt.*;
import java.applet.*;
public class Lab04vsthome extends Applet
{
public void paint(Graphics g)
{
int width = 980;
int height = 630;
int x1 = 10;
int y1 = 640;
int x2 = 990;
int y2 = 640;
g.drawRect(10,10,width,height);
int hdiv = height/50;
int wdiv = width/50;
//bottom right
for(int k = 0; k <= 50; k++)
{
g.drawLine(x1,y1,x2,y2);
y2 -= hdiv;
x1 += wdiv;
}
x1 = 10;
y1 = 640;
x2 = 990;
y2 = 640;
//bottom left
for(int k = 0; k <= 50; k++)
{
g.drawLine(x1,y1,x2,y2);
y1 -= hdiv;
x2 -= wdiv;
}
x1 = 10;
y1 = 10;
x2 = 990;
y2 = 10;
//top right
for(int k = 0; k <= 50; k++)
{
g.drawLine(x1,y1,x2,y2);
y2 += hdiv;
x1 += wdiv;
}
x1 = 10;
y1 = 10;
x2 = 990;
y2 = 10;
//top left
for(int k = 0; k <= 50; k++)
{
g.drawLine(x1,y1,x2,y2);
y1 += hdiv;
x2 -= wdiv;
}
x1 = 10;
y1 = 640;
x2 = 990;
y2 = 640;
//problem starts here
int width2 = 500;
int height2 = 312;
g.drawRect(250, 170, width2, height2);
int x3 = 250;
int y3 = 322;
int x4 = 510;
int y4 = 322;
int w2div = width2 / 50;
int h2div = height2 / 50;
for (int k = 0; k <= 50; k++)
{
g.drawLine(x3,y3,x4,y4);
y4 -= w2div;
x3 += h2div;
}
}
}
It is supposed to generate the shapes on the outside of the box in the center, in the inside box. When I tried to make this happen with one of them, I can't figure out a way to move it and to stop it from going out, and I'm genuinely stumped at this point. The solution is probably obvious, but I'm newer to doing graphs and loops in Java so I'm stumped at this point. If it matters at all, I'm using BlueJ. Can anyone help?

How to make object dissapear when hit

I was working on a brick breaker game in proccessing, and was trying to create the collision system for the game. I was able to identify when a collision happens between the ball and the brick, but I was not able to to make the brick dissapear when it gets hit by the ball. How do I do this? I would also appreciate a explanation, as I am a beginner.
Thanks.
color black = color(0,0,0);
color red = color(255,0,0);
color white = color(255,255,255);
float ballx = 412.5, bally = 600, balld = 20, ballr = balld/2, paddleX = 362.5, paddleY = 650;
float paddleW = 100, paddleH = 15;
float ballspdX, ballspdY;
boolean ball_drop = true;
float direction_choice;
float[] brickX = new float[10];
float[] brickY = new float[5];
float brickW = 50, brickH = 25;
void setup(){
size(825,800);
surface.setTitle("Brick breaker");
noCursor();
smooth();
brickX[0] = 50;
brickX[1] = 125;
brickX[2] = 200;
brickX[3] = 275;
brickX[4] = 350;
brickX[5] = 425;
brickX[6] = 500;
brickX[7] = 575;
brickX[8] = 650;
brickX[9] = 725;
brickY[0] = 50;
brickY[1] = 125;
brickY[2] = 200;
brickY[3] = 275;
brickY[4] = 350;
}
void paddle(){
noStroke();
fill(white);
rect(paddleX, paddleY, paddleW, paddleH);
}
void ball(){
noStroke();
fill(red);
circle(ballx, bally, balld);
}
void draw(){
background(black);
paddle();
ball();
if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + (paddleW / 2)){
ball_drop = false;
ballspdY = -ballspdY;
ballspdX = -4;
}
/*
if (bally + ballr == paddleY && ballx > paddleX && ballx < paddleX + paddleW){
ball_drop = false;
ballspdY = -ballspdY;
direction_choice = int(random(1,3));
if (direction_choice == 1){
ballspdX = 4;
}
if (direction_choice == 2){
ballspdX = -4;
}
}
println(direction_choice);
*/
if (bally + ballr == paddleY && ballx > paddleX + (paddleW /2) && ballx < paddleX + paddleW){
ball_drop = false;
ballspdY = -ballspdY;
ballspdX = 4;
}
if (ballx + ballr > width || ballx - ballr < 0){
ballspdX = -ballspdX;
}
if (bally - ballr < 0){
ballspdY = -ballspdY;
}
if (ball_drop){
ballspdX = 0;
ballspdY = 1;
}
for (int i = 0; i < brickX.length; i ++){
for(int j = 0; j < brickY.length; j ++){
fill(red);
rect(brickX[i], brickY[j], brickW, brickH);
if (collideLineCircle(brickX[i], brickY[j] + brickH, brickX[i] + brickW, brickY[j] + brickH, ballx, bally, ballr)){
ballspdY = -ballspdY;
}
}
}
if (bally >= 800){
bally = 600;
ballx = 412.5;
ball_drop = true;
}
bally += ballspdY;
ballx += ballspdX;
}
boolean collideLineCircle(float x1, float y1, float x2, float y2, float cx, float cy, float cr){
float A = y2 - y1, B = x1 - x2, C = x2*y1 - x1+y2;
float denom = A+A + B*B;
if (denom == 0){
return dist(x1,y1, cx, cy) < cr;
}
float Ix = (B*B*cx-A*B*cy - A*C)/denom, Iy = (A*A*cy-A*B*cx - B*C)/denom;
if (Ix >= min(x1,x2) && Ix <= max(x1,x2) & Iy >= min(y1,y2) && Iy <= max(y1,y2)) {
return abs (A*cx + B*cy + C)/sqrt(denom) < cr;
}
float d1 = dist(x1,y1,cx,cy), d2 = dist(x2,y2,cx,cy);
return min(d1,d2) < cr;
}
void mouseMoved(MouseEvent evt){
paddleX = evt.getX();
}
There are a number of ways this can be done. Perhaps the simplest change, given the data structures you already have is to create another array, this time a boolean array, and call it something like brickIsVisible.
Then you can use a simple if statement, e.g., if (brickIsVisible[i]) in your loops to determine how to color the brick. The same variable can be used to tell whether or not to do a collision test between the ball and the brick.
As for the coloring itself, there are a couple options.
I'm not sure how you are drawing your board. If you redraw the entire board with each gameloop frame, then presumably you first draw the background (which covers over the bricks) and then you draw the bricks. If this what is happening, then if brickIsVisible[i] is false you can simply omit drawing that brick on top of the background.
Another technique that is sometimes used is to make use of a transparent color. This is done by adding an alpha channel value to the color definition. Thus there are four variables instead of three. They are referred to as RGBA instead of RGB. If the fourth variable is 0, the color will be transparent. If it is 255 it will be fully visible. Intermediate values in sequence can be used if you want to create a fade in or fade out.
I am guessing you are using java.awt.Color. Following is an example that uses the four argument constructor, with the fourth being the alpha channel.
Color invisiblePurple = new Color(255, 0, 255, 0);
If the color is invisible, the RGB values don't really matter.
These days I mostly use JavaFX for graphics, so I might not be up on all the tricks of the trade with AWT/Swing. With JavaFX, one can directly set an opacity property for a graphic being displayed, which is pretty convenient. I think part of the reason that this is available in JavaFX is that screen graphics are structured more like a DOM tree (like an HTML domain object model), and the redrawing is handled behind the scenes on the current state of the tree, rather than explicitly for each object, as it is with AWT/Swing.
Another possible technique shown below uses a brick class array. The class has a 'show' boolean associated with it and the bricks are only displayed when brick[id].show is true. In this demo when the brick is selected with a mouse brick[id].show is set to false and that brick is not displayed. For your project you would have to do this when a brick is hit by the ball.
/*
Creates a grid of rectangles from a Brick class array.
Syntax: brick[id] = new Brick( x, y, w, h, "title", bkgrndColor, txtColor);
ID is taken from position in array.
*/
final int _brickGridX = 40;
final int _brickGridY = 60;
final int _brickW = 120;
final int _brickH = 60;
color BLUE = color(64, 124, 188);
color LTGRAY = color(185, 180, 180);
color YELLOW = color(245, 250, 13);
color RED = color(255, 0, 0);
color BLACK = color(0, 0, 0);
color WHITE = color(255, 255, 255);
Brick[] brick;
class Brick {
float x, y, w, h;
String title;
color bkgrnd;
color txtColor;
boolean show;
// Constructor
Brick(int xpos, int ypos, float wt, float ht, String titleStr, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = titleStr;
bkgrnd = background;
txtColor = foreground;
}
void display(int id) {
if (brick[id].show) {
fill(bkgrnd); // background color
stroke(0);
rect(x, y, w, h);
fill(txtColor); // text color
textSize(42);
textAlign(CENTER, CENTER);
text(title, x, y, w, h);
}
}
void press(int id) {
println("brick id = ", id + ": show =", brick[id].show);
}
}
void brickGrid() {
int left = 0;
int top = 0;
int vg = 0; // Space between cols (vert.gutter)
int hg = 0; // Space between rows (horz.gutter)
int rows = 5;
int cols = 5;
int id = 0;
brick = new Brick[rows*cols]; // creates brick array
for (int k = 0; k < cols; k++) {
for (int j = 0; j < rows; j++) {
left = _brickGridX + j * (_brickW + vg);
top = _brickGridY + k * (_brickH + hg);
brick[id] = new Brick(left, top, _brickW, _brickH, str(id), LTGRAY, BLACK);
brick[id].show = true;
id++;
}
}
}
void setup() {
size(800, 600);
background(BLUE);
brickGrid();
}
void draw() {
background(BLUE);
for (int i = 0; i < brick.length; i++) {
brick[i].display(i);
}
}
void mousePressed() {
for (int i = 0; i < brick.length; i++) {
if ((mouseX >= brick[i].x) && (mouseX <= brick[i].x + _brickW) && (mouseY >= brick[i].y) && (mouseY <= brick[i].y + _brickH)) {
brick[i].show = false;
// brick[i].press(i);
}
}
}

Problem with keypressed when drawing a square

I am trying to add a square to the canvas when a mouse key is pressed and i want it to remain on the canvas when the mouse key is released, but it disappears when is released the key. Can anybody help me, what am i doing wrong?
int squareSize = 6;
final float DIFF_SIZE = 1;
final int MIN_COLOUR = 1;
final int MAX_COLOUR = 10;
final float INIT_RED = 100, INIT_GREEN = 50, INIT_BLUE = 80;
final float FINAL_RED = 255, FINAL_GREEN = 200, FINAL_BLUE = 250;
final float MAX_SIZE = 40;
final float X_SPACING = 10;
final float Y_SPACING = 10;
float squareX, squareY;
void setup() {
size(600, 600);
}
void draw() {
squareX = mouseX - squareSize / 2;
squareY = mouseY - squareSize / 2;
background(255);
drawRowsOfBlocks();
}
void drawOneBlock() {
rect(squareX, squareY, squareSize, squareSize);
for (int i = MIN_COLOUR; mousePressed && i <= MAX_COLOUR / 10; i++)
{
float redValue = INIT_RED + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR)(FINAL_RED - INIT_RED);
float greenValue = INIT_GREEN + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR)(FINAL_GREEN - INIT_GREEN);
float blueValue = INIT_BLUE + (i - MIN_COLOUR) / (MAX_COLOUR - MIN_COLOUR) * (FINAL_BLUE - INIT_BLUE);
fill(redValue, greenValue, blueValue);
rect(squareX, squareY, squareSize, squareSize);
squareSize += DIFF_SIZE;
}
if (squareSize > MAX_SIZE) {
squareSize = 6;
}
}
void drawRowsOfBlocks() {
drawOneBlock();
for (int i = 1; keyPressed && i <= 2; i++) {
drawOneBlock();
float squareY2;
squareY2 = squareY + squareSize + Y_SPACING;
squareY = squareY2;
}
}
Create a class which can handle a rectangle. The calls needs a method to draw the rectangle (Draw()) and a method to update the position and size of the rectangle (Update()):
final int DIFF_SIZE = 1;
final int MIN_SIZE = 6;
final int MAX_SIZE = 40;
final float INIT_RED = 100, INIT_GREEN = 50, INIT_BLUE = 80;
final float FINAL_RED = 255, FINAL_GREEN = 200, FINAL_BLUE = 250;
class Rectangle {
int pos_x, pos_y, size;
color col;
Rectangle(int px, int py, int s, color c) {
col = c;
pos_x = px; pos_y = py;
size = s;
}
void Update(int px, int py, int inc_size) {
pos_x = px; pos_y = py;
size += inc_size;
if (size > MAX_SIZE)
size = MIN_SIZE;
float w = float(size - MIN_SIZE) / float(MAX_SIZE - MIN_SIZE);
float redValue = INIT_RED + w * (FINAL_RED - INIT_RED);
float greenValue = INIT_GREEN + w * (FINAL_GREEN - INIT_GREEN);
float blueValue = INIT_BLUE + w * (FINAL_BLUE - INIT_BLUE);
col = color(int(redValue), int(greenValue), int(blueValue));
}
void Draw() {
fill(col);
rect(pos_x-size/2, pos_y-size/2, size, size);
}
}
Use ArrayList to store all the drawn rectangles:
ArrayList<Rectangle> rectangles = new ArrayList<Rectangle>();
Add a global, boolean state (drawingRect) which indicates if the mouse button is currently pressed. Set the state and add new rectangle at the end of the list when the mousePressed() event occurs. rest the state when the mouseReleased() event occurs
boolean drawingRect = false;
void mousePressed() {
drawingRect = true;
color col = color(int(INIT_RED), int(INIT_GREEN), int(INIT_BLUE));
rectangles.add(new Rectangle(mouseX, mouseY, MIN_SIZE, col));
}
void mouseReleased() {
drawingRect = false;
}
Use the method .Update(), to change the location and size of the last rectangle in the list as long as the state drawingRect indicates that a mouse button is pressed.
Continuously draw all te rectangles in a loop:
void setup() {
size(600, 600);
}
void draw() {
if (drawingRect && rectangles.size() > 0) {
Rectangle lastRect = rectangles.get(rectangles.size()-1);
lastRect.Update(mouseX, mouseY, DIFF_SIZE);
}
background(255);
for (int i = 0; i < rectangles.size(); i++) {
Rectangle rect = rectangles.get(i);
rect.Draw();
}
}

Graphics paint component and loop trouble

I can not seem to figure out why my concentric circles are not lining up. My loops seem correct to me and the measurements are correct but for some reason the last few circles are off-centered. That's the first issue I'm having. The second issue is, i can't seem to get the concentric circles to print in each square. Once again, i can't seem to find any issue in my logic, but obviously there is an issue. Any help on this at all would be great.
This should be the end product
*Now this is my source code- ExampleGUI.java *
import javax.swing.*;
public class ExampleGUI {
public static void main(String args []) {
JFrame frame = new JFrame("Example Graphics");
ExamplePanel panel = new ExamplePanel();
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(3);
frame.pack();
frame.setVisible(true);
}
}
* ExamplePanel.java *
import java.awt.*;
import javax.swing.*;
public class ExamplePanel extends JPanel{
public ExamplePanel() {
setPreferredSize(new Dimension (600, 600));
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int x = 0;
int x2 = 5;
int y = 500;
int y2 = 505;
int w = 100;
int w2 = 90;
int h = 100;
int h2 = 90;
int i, j, k;
for(j = 1; j < 7; j++) {
x = 0;
x2 = x + 5;
for(i = 1; i < 7; i++) {
if ((i + j) % 2 == 0) {
g.setColor(Color.white);
} else {
g.setColor(Color.yellow);
}
g.fillRect(x, y, w, h);
g.setColor(Color.black);
g.drawRect(x, y, w, h);
g.setColor(Color.green);
g.fillOval(x2, y2, w2, h2);
for(k = 1; k < 7; k++) {
g.setColor(Color.black);
g.drawOval(x2, y2, w2, h2);
x2 = x2 + 5;
y2 = y2 + 5;
w2 = w2 - 10;
h2 = h2 - 10;
}
x = x + w;
x2 = x2 + w2 + 10;
}
x = x + w;
y = y - h;
y2 = (y2 - h2) - 10;
}
}
}
* This is what my program looks like when i run it. It doesn't look like the other picture for some reason *
Basically, you "spiral" loop is modifying the state of variables that are required elsewhere
What I would do, is create a new series of variables, initialized to the current state and modify those instead...
int ix = x2;
int iy = y2;
int ih = h2;
int iw = w2;
for (k = 1; k < 7; k++) {
g.setColor(Color.black);
g.drawOval(ix, iy, iw, ih);
ix = ix + 5;
iy = iy + 5;
iw = iw - 10;
ih = ih - 10;
}

java mandelbrot set moving wrong

so I've been trying to program the mandelbrot set in java, I know the code isn't very optimized but i've just started out doing this.
The idea is that when i click a pixel it will put that pixel in the center (that represents a certain complex number) and calculate the set around it. This works at the beginning but if i zoom in it will start to behave weird. I'm guessing it's either my midX, midY or the display function that's weird but i've been looking at it for a long time and can't figure it out, would appreciate some help.
class set {
DotWindow w;
int[][] arrayColor;
int max = 100;
Grayscale gray;
double zoom = 1.1;
double midX = -0.5;
double midY = 0;
public static void main(String[] args) {
new set().run();
}
void run() {
setup();
runLoop();
}
void runLoop() {
int x;
int y;
while (true) {
GameEvent event = w.getNextEvent();
switch (event.getKind()) {
case GameEvent.KEY_PRESSED:
int key = event.getKey();
if (key == 43) {
zoom = zoom * 1.1;
} else if (key == 45) {
zoom = zoom / 1.1;
}
display();
break;
case GameEvent.MOUSE_CLICKED:
midX = midX - (1 - event.getX() / 250.0);
midY = midY - (1 - event.getY() / 250.0);
System.out.println(midX);
display();
break;
}
}
}
void setup() {
w = new DotWindow(500, 500, 1);
w.checkMouse(true, false, false, false, false);
w.checkKeys(true, false, false);
arrayColor = new int[500][500];
zoom = zoom / 1.1;
display();
}
int calculate(double re, double im) {
double Zre = 0;
double Zim = 0;
double Zim2 = 0;
double Zre2 = 0;
int iterations = 0;
for (int k = 0; k < max; k++) {
if (Zre2 + Zim2 > 4.0) {
return k;
}
Zim2 = Zim * Zim;
Zre2 = Zre * Zre;
Zim = 2.0 * Zre * Zim + im;
Zre = Zre2 - Zim2 + re;
iterations = k;
}
return iterations;
}
void display() {
for (double y = 0; y < 500; y++) {
for (double x = 0; x < 500; x++) {
double value = calculate((midX - (1 - x / 250) / zoom),
(midY - (1 - y / 250) / zoom));
if (value == 99) {
w.setDot((int) x, (int) y, Color.BLACK);
} else {
w.setDot((int) x, (int) y, gray = new Grayscale(
255 - (int) value * 2));
}
}
}
}
}
case GameEvent.MOUSE_CLICKED:
midX = midX - (1 - event.getX() / 250.0)/zoom;
midY = midY - (1 - event.getY() / 250.0)/zoom;
System.out.println(midX);
display();
You've already zoomed in, say to 10x, but you change central coordinates not regarding the zoom value.

Categories