Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I'm trying to draw a grid for tetris by extending JComponent. Then, I want to draw a 2 by 2 grid on top of the

I'm trying to draw a grid for tetris by extending JComponent.

Then, I want to draw a 2 by 2 grid on top of the grid (like one layer above it). I am using an icon to draw it.

However, when I run it, the grid appears fine, but then it disappears and then one yellow line appears.

Can someone explain why it draws the two separately and how to fix it?

And why instead of four rectangles, only one yellow line appears?

Thanks!

import java.awt.*;

import java.awt.geom.Rectangle2D;

import javax.swing.JComponent;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class Grid extends JComponent {

/**

*

*/

private static final long serialVersionUID = 1L;

boolean isGameOver;

public void paintComponent(Graphics g)

{

Graphics2D g2 = (Graphics2D)g;

for (int w = 0; w < COLUMNS; w++)

{

for (int h = 0; h < ROWS; h++ )

{

g2.drawRect(getPixelUnitX(w), getPixelUnitY(h), BLOCK_LENGTH, BLOCK_LENGTH);

}

}

}

private static int getGridUnitX(int pixel)

{

return pixel/BLOCK_LENGTH;

}

private static int getGridUnitY(int pixel)

{

return pixel/BLOCK_LENGTH;

}

private static int getPixelUnitX(int gridUnit)

{

return gridUnit * BLOCK_LENGTH;

}

private static int getPixelUnitY(int gridUnit)

{

return gridUnit * BLOCK_LENGTH;

}

public static final int WIDTH = 300;

public static final int HEIGHT = 660;

public static final int ROWS = 22;

public static final int COLUMNS = 10;

public static final int BLOCK_LENGTH = WIDTH/COLUMNS;

}




import java.awt.Color;

import java.awt.Graphics2D;

import java.awt.Rectangle;

import java.awt.geom.Rectangle2D;

import java.util.Random;

public class OMini extends Tetromini {

public OMini()

{

Random generator = new Random();

topLeft = 30*generator.nextInt(9);

}

//random later

public void draw(Graphics2D g2) {

// TODO Auto-generated method stub

rect = new Rectangle2D[4];

rect[0] = new Rectangle(topLeft,0, Grid.BLOCK_LENGTH,Grid.BLOCK_LENGTH);

rect[1] = new Rectangle(topLeft+30,0, Grid.BLOCK_LENGTH,Grid.BLOCK_LENGTH);

rect[2] = new Rectangle(topLeft,30, Grid.BLOCK_LENGTH,Grid.BLOCK_LENGTH);

rect[3] = new Rectangle(topLeft+30,30, Grid.BLOCK_LENGTH,Grid.BLOCK_LENGTH);

g2.setColor(Color.YELLOW);

g2.draw(rect[0]);

g2.draw(rect[1]);

g2.draw(rect[2]);

g2.draw(rect[3]);

}

@Override

public void translate(int dx, int dy) {

// TODO Auto-generated method stub

}

@Override

void rotate() {

// TODO Auto-generated method stub

return;

}

@Override

void fall() {

// TODO Auto-generated method stub

}

private Rectangle2D[] rect;

private int topLeft;

@Override

int getStartingLocation() {

// TODO Auto-generated method stub

return 0;

}

@Override

Rectangle2D[] getRectangles() {

// TODO Auto-generated method stub

return rect;






import java.awt.Graphics2D;

import java.awt.geom.Rectangle2D;

public abstract class Tetromini implements MoveableShape {

protected boolean isFalling;

abstract int getStartingLocation();

abstract void rotate();

abstract void fall();

abstract Rectangle2D[] getRectangles();

protected static int getGridUnitX(int pixel)

{

return pixel/Grid.BLOCK_LENGTH;

}

}

}

import java.awt.Graphics2D;

/**

   A shape that can be moved around.

*/

public interface MoveableShape

{

   /**

      Draws the shape.

      @param g2 the graphics context

   */

   void draw(Graphics2D g2);

   /**

      Moves the shape by a given amount.

      @param dx the amount to translate in x-direction

      @param dy the amount to translate in y-direction

   */

   void translate(int dx, int dy);

}

}

import java.awt.*;

import javax.swing.JFrame;

import javax.swing.JLabel;

public class TetrisTester {

public static void main(String[] args) {

JFrame frame = new JFrame();

frame.setSize(300,685);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(new Grid());

frame.setVisible(true);

MoveableShape shape = new OMini();

ShapeIcon icon = new ShapeIcon(shape, 30, 30 );

final JLabel label = new JLabel(icon);

frame.setLayout(new FlowLayout());

frame.add(label);

frame.setVisible(true);

}

}

Step by Step Solution

3.42 Rating (152 Votes )

There are 3 Steps involved in it

Step: 1

import javaawtComponent import javaawtDimension import javaawtGraphics import javaawteventActionEvent import ... blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Conceptual Physics

Authors: Paul G. Hewitt

11th edition

321568095, 9780-032166256, 321662563, 978-0321568090

More Books

Students also viewed these Programming questions

Question

What is the formula for computing a Pearson residual?

Answered: 1 week ago