How to make tetris blocks fall down every second? - java

I am making a Tetris clone and I am using a tick method for the game loop (not sure if this is necessary). I want to increase the y value of the block object by 50 pixels every second or so. Can I add some type of delay in my tick method, or is there an easier way?
public class SquareBlock extends GameObject{
public SquareBlock(int x, int y, ID id){
super(x,y,id);
}
public void tick(){
y += 50;
}
public void render(Graphics g){
g.setColor(Color.red);
g.fillRect(x, y, 50, 50);
g.fillRect(x, y + 50, 50, 50);
g.fillRect(x + 50, y, 50, 50);
g.fillRect(x + 50, y + 50, 50, 50);
}
}
Thanks

Related

(JAVA) How to differentiate 2d object

one simple question. I try to use graphic 2d to make objects . My question is how can we differentiate the variable between 2 object.
For example the below code;
public void paintComponent(Graphics g) {
super.paintComponent(g);
r1 = new Rectangle(10, 10, 50, 30);
r2 = new Rectangle(450, 10, 50, 30);
g.setColor(Color.BLUE);
g.fillRect(10, 10, 50, 30);
g.setColor(Color.RED);
g.fillRect(450, 10, 50, 30);
}
public void actionPerformed(ActionEvent e) {
if (r1.intersects(r2)) {..............
}
from above code,i have 2 rectangle variable r1 and r2, and i can do "if (r1.intersects(r2))"
in the actionPerformed method.
But how about below code when using Graphic2d, how can i differentiate between these 2 triangle, so that it can fit into "if (r1.intersects(r2))", as only one variable "lukis" only exist and it is used to make 2 triangle. Thank you.
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D lukis = (Graphics2D) g;
int[] x = {100, 200, 300};
int[] y = {300, 100, 300};
lukis.setColor(Color.RED);
lukis.drawPolygon(x, y, 3);
int[] x1 = {600, 700, 800};
int[] y1 = {300, 100, 300};
lukis.setColor(Color.RED);
lukis.drawPolygon(x1, y1, 3);
}

I'm trying to make my triangles redraw themselves when I click the mouse, but they only just multiply when I mouse click

When I run this program the green triangles appear all over the place when I click instead I want it to reposition itself to a random place and remove the original green triangle
float x;
void setup()
{
size (800, 600);
background(29, 154, 178);
x=random(width);
frameRate(30);
}
void draw()
{
noStroke();
fill(91, 180, 118 );
triangle(x, 20, 0, height, width, height);
fill(82, 45, 80);
triangle(width/2-200, 120, 0, height, width, height);
fill(82, 45, 60);
triangle(width/2+150, 220, 0, height, width, height);
fill(82, 45, 28);
triangle(width/2-100, 320, 0, height, width, height);
fill(243, 245, 158);
rect(0, 525, 800, 100);
if(mousePressed == true)
{
x=random(width);
}
}
Then add noLoop() in your setup so you're not drawing the same thing 60 times a second, and use mouse click handling instead, with a redraw call so you only draw when there is something new to draw:
float x, y, triangleSideLength;
void setup() {
size(300,300);
noLoop();
triangleSideLength = 30; // or whatever value it needs to be of course
}
void draw() {
x = random(0, width - triangleSideLength);
y = random(0, height - triangleSideLength);
drawTriangleAt(x, y);
}
void drawTriangleAt(float x, float y) {
//...triangle drawing code here...
}
void mouseClicked() {
redraw();
}
Also note that you usually want to constrain your x/y coordinates so that a triangle will always "fit on the screen" instead of getting an x/y coordinate that happens to have x=width and now your triangle is basically 100% out of view.

Java paint not painting on top of previously called methods

I have been unable to find an answer to this issue, so hopefully someone can help me out.
I am doing a class assignment where I have to make a scene using graphics. We've only had two lectures covering graphics so far, so I am hardly familiar. The issue is that she methods are not being painted in the preferred order. As you can see in the code to follow, I have two recursive methods making a sky and grass background/foreground. On top of those, I wish to have a cross (as well as many components thereafter granted I can get past this problem), but the cross does not appear as it should despite having that being the last method called within paint.
import java.awt.*;
import javax.swing.*;
public class DrawPicture extends JApplet
{
public final int SIZE = 800;
public final int DIST = 20;
public Color bg1 = new Color(0, 200, 255);
public Color bg2 = new Color(0, 175, 255);
public Color grass1 = new Color(0, 220, 60);
public Color grass2 = new Color(30, 255, 150);
public Color brown = new Color(110, 80, 20);
public void backgroundRec(int x, int y, int c, Graphics g)
{
if (c%2==0)
g.setColor(bg1);
else if (c%2==1)
g.setColor(bg2);
if (c < 40)
{
g.fillOval(x, y, SIZE, SIZE);
backgroundRec(x, y - DIST, c+1, g);
}
}
public void foregroundRec(int x, int y, int c, Graphics g)
{
if (c%2==0)
g.setColor(grass1);
else if (c%2==1)
g.setColor(grass2);
if (x < SIZE/2)
{
g.fillRect(x, y, SIZE, DIST);
foregroundRec(x, y + DIST, c+1, g);
}
}
public void cross(int x, int y, Graphics g)
{
g.setColor(brown);
g.fillRect(SIZE/2, SIZE/2, 45, 275);
g.fillRect(SIZE/2-50, SIZE/2+45, 150, 45);
}
public void paint(Graphics g)
{
//backgroundRec(0, 0, 0, g);
//foregroundRec(0, 400, 0, g);
cross(SIZE/2, SIZE/2, g);
}
}
One of the problems is, you have a StackOverflowException in foregroundRec, because nothing is seems to be stopping it from updating, probably because you're not changing the value of x.
I "think" foregroundRec(x, y + DIST, c + 1, g); should be foregroundRec(x + DIST, y + DIST, c + 1, g); or something :P - but based on your code, x has to change
You should also be calling super.paint before you do any of your own painting, this ensures that you don't end up with any weird paint artifacts
The other problem you're having is the fact that you're relying on the SIZE property, instead, you should be using getWidth or getHeight to determine what the actual size of the applet is
I'd also suggest having a look at:
Java Plugin support deprecated and Moving to a Plugin-Free Web
Painting in AWT and Swing
Performing Custom Painting
2D Graphics
Updated
Okay, after going through a few iterations, what I actually think you're trying to do is...
if (y < SIZE) {
g.fillRect(x, y, SIZE, DIST);
foregroundRec(x, y + DIST, c + 1, g);
}

how to draw heart using java awt libaray? also depends on position and size?

how to draw heart using java awt libaray? Below I wrote code which display a heart:
public class heart{
publicheart(int x, int y){...}
public heart(int x, int y, int width, int height){...}
...
int[] xp = { x, x + 41, x + 20 };
int[] yp = { y + 14, y + 14, y + 30 };
g.setColor(Color.pink.darker());
g.fillOval(x, y, 20, 20); //left circle
g.fillOval(x + 20, y, 20, 20); //to cover middle spot
g.fillOval(x + 13, y + 10, 10, 10); //right circle
g.fillPolygon(xp, yp, xp.length); //bottom triangle
}
now lets say I want to call this class and print a heart.
heart h = new heart(100, 100);
as you can see this work fine bc my x and y depends on heart class so I can create whatever position.
Problem:
How can I change my code so it depends on x, y, width, and height? ex:
heart h = new heart(100, 100, 50, 50);

Rectangle thickness java

I need to outline in blue 1 rectangle at a time when selected. The code works but I cant see the blue outline very well. what is the easiest way to make the outline thicker. Wither that be drawing another shape over it or something else.
private void displayBlock(Graphics g)
{
for(int i = 0; i < 10; i++)
{
if (occupant[i].equals("Empty"))
{
//Sets the rectangle red to show the room is occupied
g.setColor(emptyColor);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight);
}
else
{
//Sets the rectangle green to show the room is free
g.setColor(Color.red);
g.fillRect(150+(i*50), 120, aptWidth, aptHeight);
}
if (i == selectedApartment)
{
g.setColor(Color.blue);
g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
}
else
{
g.setColor(Color.black);
g.drawRect(150+(i*50), 120, aptWidth, aptHeight);
}
// Draws the numbers in the Corresponding boxes
g.setColor(Color.black);
g.drawString(Integer.toString(i+1), 150+(i*50)+15, 120+25);
}
//Drawing the key allowing the user to see what the colours refer to
g.setFont(new Font("TimesRoman", Font.PLAIN, 14));
g.drawString("Key", 20,120);
g.drawString("Occupied",60,150);
g.drawString("Empty", 60, 170);
// Drawing the boxes to show what the colours mean in the key
g.setColor(Color.red);
g.fillRect(20, 130, 25, 25);
g.setColor(Color.black);
g.drawRect(20, 130, 25, 25);
g.setColor(Color.green);
g.fillRect(20,150,25,25);
g.setColor(Color.black);
g.drawRect(20,150,25,25);
} // End of displayBlock
You can set the thickness of the rectangle by creating a Graphics2D object and setting its stroke.
java.awt.Graphics2D g2 = (java.awt.Graphics2D) g.create();
g2.setStroke(new java.awt.BasicStroke(3)); // thickness of 3.0f
g2.setColor(Color.blue);
g2.drawRect(10,10,50,100); // for example
Maybe you could draw a second rectangle immediately inside the first one. Or multiple of them.
public static void drawRectangle(Graphics g, int x, int y, int width, int height, int thickness) {
g.drawRect(x, y, width, height);
if (thickness > 1) {
drawRectangle(g, x + 1, y + 1, width - 2. height - 2, thickness - 1);
}
}

Categories