How can I create a slider with two arrow buttons from the side? The arrows/triangle should turn white when clicked. And the slider should move each time the arrow button is clicked.
here's the link to what it should look like
and here's what I've done so far
int x=75;
void setup() {
size(600,400);
}
void draw() {
background(100);
fill (200);
rect (75, 25, 400, 50);
stroke(0);
if(mousePressed) {
if (mouseX >75 && mouseX <= 475)
{x=mouseX;}
}
fill(127,0,0);
rect (x, 20, 9, 60);
fill (255);
fill (200);
rect (10, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (50, 60, 50, 40, 15, 50);
}
fill (200);
rect (490, 25, 50, 50);
{
if (mousePressed == true) {
fill(255);
} else {
fill(0);
}
triangle (500, 60, 500, 40, 535, 50);
}
println(x);
}
When I click anywhere on the screen, my problem is that both arrows turn white. I need it to individually function. And the slider is not moving every time I click the arrow buttons
The following approach to this problem uses 4 different classes: ForwardArrow, BackArrow, Slider, and ValueField. Arrow fill color is controlled by the press() method of its respective class.
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);
color GREEN = color(32, 175, 47);
ForwardArrow _fwdArrw;
BackArrow _backArrw;
ValueField _valueFld;
Slider _slider;
final int _sliderX = 90;
final int _sliderY = 40;
final int _sliderW = 200;
final int _sliderH = 30;
final int _txtSize = 22;
final int _initValue = 40;
final int _maxValue = 200;
final int _minValue = 0;
int value = _initValue;
class ValueField {
float x, y, w, h;
String title;
color fldColor;
color txtColor;
// Constructor
ValueField(int xpos, int ypos, float wt, float ht, String valueStr, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
title = valueStr;
fldColor = background;
txtColor = foreground;
}
void display(int val) {
// **** Value Field **** //
fill(fldColor); // erase old value
rect(x, y, w, h);
fill(txtColor); // text color
textSize(_txtSize);
textAlign(CENTER);
text(str(val), x, y, w, h);
// **** Slider bar **** //
fill(255);
rect(_sliderX, _sliderY, _sliderW*val/_maxValue, _sliderH);
}
}
class ForwardArrow {
float x, y, w, h;
color arrwColor;
// Constructor
ForwardArrow(int xpos, int ypos, float wt, float ht, color background) {
x = xpos;
y = ypos;
w = wt;
h = ht;
arrwColor = background;
}
void display() {
fill(arrwColor); // arrow color
noStroke();
triangle(x, y, x, y + h, x + w, y + h/2 );
}
void press() {
fill(255); // arrow color
noStroke();
triangle(x, y, x, y + h, x + w, y + h/2 );
}
}
class BackArrow {
float x, y, w, h;
color arrwColor;
// Constructor
BackArrow(int xpos, int ypos, float wt, float ht, color background) {
x = xpos;
y = ypos;
w = wt;
h = ht;
arrwColor = background;
}
void display() {
fill(arrwColor);
noStroke();
triangle(x, y + h/2, x + w, y, x + w, y + h );
}
void press() {
fill(255);
noStroke();
triangle(x, y + h/2, x + w, y, x + w, y + h );
}
}
class Slider {
float x, y, w, h;
color barColor;
color trimColor;
// Constructor
Slider(int xpos, int ypos, float wt, float ht, color background, color foreground) {
x = xpos;
y = ypos;
w = wt;
h = ht;
barColor = background;
trimColor = foreground;
}
void display() {
stroke(0);
strokeWeight(1);
noFill();
rect(x, y, w, h);
}
}
void setup() {
size(500, 250);
background(BLUE);
_slider = new Slider(_sliderX, _sliderY, _sliderW, _sliderH, WHITE, BLACK);
_backArrw = new BackArrow(50, 40, 30, 30, GREEN);
_fwdArrw = new ForwardArrow(300, 40, 30, 30, GREEN);
_valueFld = new ValueField(380, 40, 60, 30, str(_initValue), WHITE, BLACK);
}
void draw() {
background(BLUE);
_valueFld.display(value);
_fwdArrw.display();
_backArrw.display();
_slider.display();
// FwdArrw Long Press
if ((mouseX >= _fwdArrw.x) && (mouseX <= _fwdArrw.x + _fwdArrw.w) && (mouseY >= _fwdArrw.y) && (mouseY <= _fwdArrw.y + _fwdArrw.h)) {
if (mousePressed == true) {
_fwdArrw.press();
value++;
if (value > _maxValue) {
value = _maxValue;
}
_valueFld.display(value);
}
}
// BackArrw Long Press
if ((mouseX >= _backArrw.x) && (mouseX <= _backArrw.x + _backArrw.w) && (mouseY >= _backArrw.y) && (mouseY <= _backArrw.y + _backArrw.h)) {
if (mousePressed == true) {
_backArrw.press();
value--;
if (value < _minValue) {
value = _minValue;
}
_valueFld.display(value);
}
}
}
A revision of your code follows;
int x=75;
void setup() {
size(600, 400);
}
void draw() {
background(100);
fill (200);
rect (75, 25, 400, 50); // slider bar
stroke(0);
fill(127, 0, 0);
rect (x, 20, 9, 60); // slider thumb
fill (200);
rect (10, 25, 50, 50); // back arrow
fill(0);
triangle (50, 60, 50, 40, 15, 50);
if ((mouseX >= 10) && (mouseX <= 10 + 50) && (mouseY >= 25) && (mouseY <= 25 + 50) ) {
if (mousePressed == true) {
fill(255);
triangle (50, 60, 50, 40, 15, 50);
x--;
if (x<75) {
x = 75; // minValue
}
} else {
fill(0);
triangle (50, 60, 50, 40, 15, 50);
}
}
fill (200);
rect (490, 25, 50, 50); //forward arrow
fill(0);
triangle (500, 60, 500, 40, 535, 50);
if ((mouseX >= 490) && (mouseX <= 490 + 50) && (mouseY >= 25) && (mouseY <= 25 + 50) ) {
if (mousePressed == true) {
fill(255);
triangle (500, 60, 500, 40, 535, 50);
x++;
if (x>466) {
x = 466; // maxValue
}
} else {
fill(0);
triangle (500, 60, 500, 40, 535, 50);
}
}
}
You've got part of the logic right for the slider/trackbar so it changes x only within a range. This happens horizontally only at this stage, but you can use the same logic to check horizontal limits as well. Similarly, you can check if the cursor is within the bounds of any rectangle (be it the slider or either of the buttons):
int x=75;
void setup() {
size(600, 400);
}
void draw() {
background(100);
// slider
fill (200);
rect (75, 25, 400, 50);
stroke(0);
if (mousePressed) {
if (mouseX >75 && mouseX <= 475)
{
x=mouseX;
}
}
fill(127, 0, 0);
rect (x, 20, 9, 60);
fill (255);
// left arrow button
fill (200);
rect (10, 25, 50, 50);
fill(0);
if (mousePressed == true) {
if (mouseX > 10 && mouseX <= 10 + 50 && mouseY > 25 && mouseY <= 25 + 50){
fill(255);
}
}
triangle (50, 60, 50, 40, 15, 50);
// right arrow button
fill (200);
rect (490, 25, 50, 50);
fill(0);
if (mousePressed == true) {
if (mouseX > 490 && mouseX <= 490 + 50 && mouseY > 25 && mouseY <= 25 + 50){
fill(255);
}
}
triangle (500, 60, 500, 40, 535, 50);
println(x);
}
Wouldn't it be nice if you could take that logic and instead of copy/pasting the different x,y,width,height parameters for the same 4 statements you could group that functionality in a reusable block of code ?
That what functions are for. You're already using them already (defining setup()/draw(), calling background()/fill()/etc.
The Processing Button example already provides the boolean overRect(int x, int y, int width, int height) function which is perfect for you're trying to achieve: pass in the x,y,width,height or a button and get back boolean value.
Here's your code using the overRect():
int x=75;
void setup() {
size(600, 400);
}
void draw() {
background(100);
// slider
fill (200);
rect (75, 25, 400, 50);
stroke(0);
if (mousePressed) {
if (mouseX >75 && mouseX <= 475)
{
x=mouseX;
}
}
fill(127, 0, 0);
rect (x, 20, 9, 60);
fill (255);
// left arrow button
fill (200);
rect (10, 25, 50, 50);
fill(0);
if (mousePressed && overRect(10, 25, 50, 50)) {
fill(255);
x--;
}
triangle (50, 60, 50, 40, 15, 50);
// right arrow button
fill (200);
rect (490, 25, 50, 50);
fill(0);
if (mousePressed && overRect(490, 25, 50, 50)){
fill(255);
x++;
}
triangle (500, 60, 500, 40, 535, 50);
// ensure x remains within the slide limits
x = constrain(x, 75, 475);
println(x);
}
boolean overRect(int x, int y, int width, int height) {
if (mouseX >= x && mouseX <= x+width &&
mouseY >= y && mouseY <= y+height) {
return true;
} else {
return false;
}
}
Related
I am doing a Roll Over Animation.
How will I revert back the color to its original after if fades away when the mouse cursor comes back to the tile?
in my code, the color fades to white then does not come back when I bring back the cursor to the tile
float addColorValue;
boolean clicked = false;
void setup() {
size(400,400); //pixel size of the program
}
void draw() {
background(255); //setting up the white background
line(width/2,height,width/2,0); //drawing the vertical line
line(width,height/2,0,width/2); //drawing the horizontal line
if(clicked){
if(mouseX > 200 && mouseY > 200){ // draws the 4th quadrant
fill(255,255,addColorValue+=1); rect(200,200,200,200); //draws rectangle
fill(255); textSize(50); text("4TH", 250, 300); //display which number of the quadrant
}
//draws the 3rd quadrant
else if(mouseX > 0 && mouseY > 200){
fill(addColorValue+=1,addColorValue+=1,255); rect(0,200,200,200);
fill(255); textSize(50); text("3RD", 50, 300);
}
//draws the 2nd quadrant
else if(mouseX > 200 && mouseY > 0){
fill(addColorValue+=1,255,addColorValue+=1); rect(200,0,200,200);
fill(255); textSize(50); text("2ND", 250, 100);
}
//draws the 1st quadrant
else if(mouseX > 0 && mouseY > 0){
fill(255,addColorValue+=1,addColorValue+=1); rect(0,0,200,200);
fill(255); textSize(50); text("1ST", 50, 100);
}
}
else{
background(0);
}
}
//switches on and off the lights of the program
void mousePressed() {
clicked = !clicked;
}
Associate each quad to an index and store the index in a globale variable (quad). Get the index of the current quad (new_quad) and compare it to the quad. If the index has changed, then set addColorValue = 0. This will restart the fading effect:
if (quad != new_quad) {
quad = new_quad;
addColorValue = 0;
}
See the example:
float addColorValue;
boolean clicked = false;
int quad = 0;
void setup() {
size(400,400); //pixel size of the program
}
void draw() {
if (clicked) {
background(255); //setting up the white background
line(width/2,height,width/2,0); //drawing the vertical line
line(width,height/2,0,width/2); //drawing the horizontal line
int new_quad = -1;
if (mouseX > 200 && mouseY > 200){
new_quad = 0;
drawRect("4TH", 200, 200, 200, 200, color(255, 255, addColorValue+=1));
}
else if(mouseX > 0 && mouseY > 200){
new_quad = 1;
drawRect("3RD", 0, 200, 200, 200, color(addColorValue+=1, addColorValue+=1, 255));
}
else if(mouseX > 200 && mouseY > 0){
new_quad = 2;
drawRect("2ND", 200, 0, 200, 200, color(addColorValue+=1, 255, addColorValue+=1));
}
else if(mouseX > 0 && mouseY > 0) {
new_quad = 3;
drawRect("1ST", 0, 0, 200, 200, color(255, addColorValue+=1, addColorValue+=1));
}
if (quad != new_quad) {
quad = new_quad;
addColorValue = 0;
}
}
else{
background(0);
}
}
void drawRect(String text, int x, int y, int w, int h, color c) {
fill(c);
rect(x, y, w, h);
fill(255);
textSize(50);
text(text, x+50, y+100);
}
//switches on and off the lights of the program
void mousePressed(){
clicked = !clicked;
}
I have a code for moving sprites in an animation - all but one of my sprites moves and I can't figure out why. Can someone explain?
My Sprite class:
abstract class SimpleSprite {
// basic x,y movement,keeps a master list of Sprites
public static final ArrayList<SimpleSprite> sprites = new ArrayList<SimpleSprite>();
float x, y, dx, dy; // position and velocity (pixels/TIMER_MSEC)
public SimpleSprite(float x, float y, float dx, float dy) {
// initial position and velocity
this.x = x;
this.y = y;
this.dx = dx;
this.dy = dy;
sprites.add(this);
}
public void update() { // update position and velocity every n milliSec
// default - just move at constant velocity
x += dx; // velocity in x direction
y += dy; // velocity in y direction
}
abstract public void draw(Graphics2D g2d);
// just draw at current position, no updating.
}
My sprite that doesn't work:
class Carpaint extends SimpleSprite {
public Carpaint(float x, float y, float dx, float dy) {
super(x, y, dx, dy);
}
#Override
public void draw(Graphics2D g2d){
g2d.setColor(Color.pink);
g2d.fillRect(50, 50, 40, 60);
g2d.setColor(Color.black);
g2d.drawRect(50, 50, 40, 60);
g2d.drawRect(60, 60, 20, 40);
g2d.fillRect(45, 50, 5, 15);
g2d.fillRect(90, 50, 5 , 15);
g2d.fillRect(45, 95, 5, 16);
g2d.fillRect(90, 95, 5, 16);
}
}
My main:
public static void main(String[] args) {
// create and display the animation in a JFrame
final JFrame frame = new JFrame("Animation 2 (close window to exit)");
Animation2 animationPanel = new Animation2(600, 500);
frame.add(animationPanel);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// add some sprites...
new Square(0, 0, 3, 2, 40);
new Ball(500, 0, -3, 3, 20);
new Ball(0, 500, 2, -5, 30);
new Carpaint(100, 100, 20, -6);
}
I expected all sprites to move but the car doesn't (see here: https://i.gyazo.com/0219127277d2543735b3a4727e7c7e72.mp4)
Answer came from #RealSkeptic - I wasn't painting the car with reference to x and y.
New Code:
class Carpaint extends SimpleSprite {
public Carpaint(float x, float y, float dx, float dy) {
super(x, y, dx, dy);
}
#Override
public void draw(Graphics2D g2d){
g2d.setColor(Color.pink);
g2d.fillRect((int) x, (int) y, 40, 60);
g2d.setColor(Color.black);
g2d.drawRect((int) x, (int) y, 40, 60);
g2d.drawRect((int) x + 10, (int) y + 10, 20, 40);
g2d.fillRect((int) x-5, (int) y, 5, 15);
g2d.fillRect((int) x + 40, (int) y, 5 , 15);
g2d.fillRect((int) x - 5, (int) y + 45, 5, 16);
g2d.fillRect((int) x + 40, (int) y + 45, 5, 16);
}
}
This is my code:
boolean keyPressed = true;
boolean mousePressed = true;
PShape circle;
int x;
int y;
void setup() {
background(0);
fullScreen(1);
frameRate(144);
fill(255, 255, 0);
circle = createShape(ELLIPSE, 0, 0, 50, 50);
}
void draw() {
background(0);
if (mousePressed == true) {
noCursor();
}
if (keyPressed == true && key == 'w') {
x = int(random(width));
y = int(random(height));
rect(x, y, 40, 40);
fill(255, 0, 255);
}
if (key == 's') {
background(0);
}
translate(mouseX, mouseY);
shape(circle);
}
Currently when pressing W the shape appears rapidly. I would like to have it where the shape spawns once, then when the background is cleared and W is pressed again the shape is located somewhere else.
I recently started with Java and now I got stuck with a simple project. I want to draw a line and that works, but when i draw another line the first line disappears. I have no idea how to get it so I can keep drawing lines.
This is my code:
package com.example.paint;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public class ClickEvent extends MouseAdapter {
private Paint paint;
boolean click = false;
boolean clear = false;
int startX;
int startY;
int endX;
int endY;
int firstTime = 1;
public ClickEvent(Paint paint, Handler handler, Line line) {
this.paint = paint;
}
public void mousePressed(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if (mouseOver(mx, my, 750, 560, 83, 40)) {
clear = true;
}
if (mouseOver(mx, my, 0, 0, 40, 40)) {
paint.color = "Black";
}
if (mouseOver(mx, my, 0, 40, 40, 40)) {
paint.color = "Blue";
}
if (mouseOver(mx, my, 0, 80, 40, 40)) {
paint.color = "Green";
}
if (mouseOver(mx, my, 0, 120, 40, 40)) {
paint.color = "Red";
}
if (!mouseOver(mx, my, 0, 0, 40, 160) && !mouseOver(mx, my, 750, 560, 83, 40)) {
clear = false;
startX = mx;
startY = my;
click = true;
}
}
public void mouseReleased(MouseEvent e) {
int mx = e.getX();
int my = e.getY();
if (!mouseOver(mx, my, 0, 0, 40, 160) && !mouseOver(mx, my, 750, 560, 83, 40)) {
endX = mx;
endY = my;
click = false;
}
}
private boolean mouseOver(int mx, int my, int x, int y, int width, int height) {
if (mx > x && mx < x + width) {
if (my > y && my < y + height) {
return true;
} else
return false;
} else
return false;
}
public void tick() {
}
public void render(Graphics g) {
Font font = new Font("Arial", 1, 30);
Font font1 = new Font("Arial", 1, 13);
g.setColor(Color.black);
g.fillRect(0, 0, 40, 40);
g.setColor(Color.blue);
g.fillRect(0, 40, 40, 40);
g.setColor(Color.green);
g.fillRect(0, 80, 40, 40);
g.setColor(Color.red);
g.fillRect(0, 120, 40, 40);
g.setColor(Color.black);
g.drawRect(750, 560, 83, 40);
g.setFont(font);
g.drawString("Clear", 755, 590);
g.setFont(font1);
if (paint.color == "Red") {
g.setColor(Color.red);
g.drawString("Color: " + paint.color, 745, 15);
} else if (paint.color == "Blue") {
g.setColor(Color.blue);
g.drawString("Color: " + paint.color, 745, 15);
} else if (paint.color == "Green") {
g.setColor(Color.green);
g.drawString("Color: " + paint.color, 745, 15);
} else if (paint.color == "Black") {
g.setColor(Color.black);
g.drawString("Color: " + paint.color, 745, 15);
} else {
g.setColor(Color.black);
g.drawString("Color: ", 745, 15);
}
if (!clear) {
if ("Red".equals(paint.color) && !click) {
g.setColor(Color.red);
g.drawLine(startX, startY, endX, endY);
} else if ("Green".equals(paint.color) && !click) {
g.setColor(Color.green);
g.drawLine(startX, startY, endX, endY);
} else if ("Blue".equals(paint.color) && !click) {
g.setColor(Color.blue);
g.drawLine(startX, startY, endX, endY);
} else if ("Black".equals(paint.color) && !click) {
g.setColor(Color.black);
g.drawLine(startX, startY, endX, endY);
}
}
}
}
Some of the code is part of a basic tutorial, but the rest I tried to do myself. If anyone knows how I can get it to keep drawing lines or if anyone has any other improvements I would really appreciate it.
The problem is that you're only saving one line to draw later, in the form of startX, startY, endX, endY.
Rather you could, at the end of each click, save the line as a new line object. Something like this:
class MyLine {
private final int startX, startY;
private final int endX, endY;
private final Color color;
public MyLine(int startX, int startY, int endX, int endY, Color color) {
this.startX = startX;
this.startY = startY;
this.endX = endX;
this.endY = endY;
this.color = color;
}
public void draw(Graphics g) {
g.setColor(color);
g.drawLine(startX, startY, endX, endY);
}
}
Add a list as a field to your class:
List<MyLine> lineList = new ArrayList<>();
And then, in the mouseReleased method, add a new Line to the list:
if (!mouseOver(mx, my, 0, 0, 40, 160) && !mouseOver(mx, my, 750, 560, 83, 40)) {
endX = mx;
endY = my;
click = false;
lineList.add(new MyLine(startX, starY, endX, endY, paint.color));
}
Additionally, you would have to change Paint, so that it holds the color as a Color object:
class Paint {
public Color color;
...
}
~~~~
paint.color = Color.black;
Which is much easier than saving strings.
Finally, loop over all of your lines to draw them, within your render method:
for(MyLine l : lineList) {
l.draw(g);
}
private JPanel contentPane;
private KeyListener myKeyListener;
JLabel lblUp;
JLabel lblMiddle;
JLabel lblDown;
JButton btnStart;
JLabel lblScore;
int lblu = 0;
int x = 0;
int y = 50;
int u = 1;
int w = 1;
int rxx = 0;
int ryy = 0;
int s = 0;
Timer timer;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Game_1 frame = new Game_1();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the frame.
*/
public Game_1() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(contentPane);
contentPane.setLayout(null);
JLabel lblScore = new JLabel("0");
lblScore.setHorizontalAlignment(SwingConstants.CENTER);
lblScore.setForeground(Color.GREEN);
lblScore.setBounds(388, 0, 46, 14);
contentPane.add(lblScore);
addKeyListener(this);
}
public void keyPressed(KeyEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
if(y < 0){
y = maxh -50;
}
if(y > maxh-45){
y = 0;
}
if (arg0.getKeyCode() == KeyEvent.VK_UP) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
y = y - 10;
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_RIGHT) {
if(u ==1){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
Timer timer = new Timer(100, this);
timer.start();
u = 0;
}else if(u ==0){
x = x +10;
}
} else if (arg0.getKeyCode() == KeyEvent.VK_DOWN) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
y = y+ 10;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
}
} else if (arg0.getKeyCode() == KeyEvent.VK_LEFT) {
if(u ==0){
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.BLACK);
x = x- 10;
if (x < 0) {
pen.fillRect(x - 30, y, 50, 50);
x = maxw;
}
}
}
}
public void keyReleased(KeyEvent arg0) {
}
public void keyTyped(KeyEvent arg0) {
}
public void run() {
}
#Override
public void actionPerformed(ActionEvent arg0) {
Graphics pen = this.contentPane.getGraphics();
int maxh = contentPane.getHeight();
int maxw = contentPane.getWidth();
pen.setColor(Color.BLACK);
pen.fillRect(0, 0, maxw, maxh);
pen.setColor(Color.GREEN);
pen.fillRect(x, y, 50, 50);
pen.setColor(Color.BLACK);
pen.fillRect(x - 50, y, 50, 50);
x = x + 1;
if (x >= maxw) {
pen.fillRect(x - 30, y, 50, 50);
x = 0;
}
if(w ==1){
Random r = new Random();
int ry = r.nextInt(maxh - 0) + 100;
int rx = r.nextInt(maxw - 0) + 100;
rxx = rx;
ryy = ry;
pen.setColor(Color.RED);
pen.fillRect(rx, ry, 10, 10);
w = 0;
}
pen.setColor(Color.RED);
pen.fillRect(rxx, ryy, 10, 10);
if(x-50 <= rxx && x > rxx && y > ryy && y-50 <= ryy){
s ++;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(rxx, ryy, 10, 10);
w = 1;
}
}
}
here is the Problem: it only detects that they touch in the left top >corner(the wrong code is at the end, the last if)
you can start the game by pressing the Right arrow Button on you're Keyboard.Moving upwards: up Key on you're Keyboard.Moving downwards:down Key on your're Keyboard.The right Button also lets you move to the right, the left Button to the left.Picture of the Game
I want, that the Rect detects that it touches the other in every part
of it, not only in the left top corner
Let's assume you've got first rect with: x1, y1, width1, height1 and second rect with x2, y2, width2, height2. The first rect touched the second in every part of it (so the second one is contained in the first one) when x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height
so
if (x1 <= x2 && x2+width2 <= x1+width2 && y1 <= y2 && y2+height <= y1+height) {
// the second rect is contained in the first rect
}
So I finally fixed it. For those who are interested, here is the fixed part of the code:
if (x+d >= x2 && x <= x2 && y+d >=y2 && y <= y2) {
s++;
d = d + s*10;
System.out.println("PUNKT");
pen.setColor(Color.BLACK);
pen.fillRect(x2, y2, 10, 10);
w = 1;
if (s == 10) {
JOptionPane.showMessageDialog(frame, "Achievement get: " + s + "Punkte!");
}