How to clear a line of ImageIcons like in tetris?

If I have a JPanel with different ImageIcons in it like tetris and if the line is filled it will be removed. Does anyone know how i can redraw the area occupied by the ImageIcons? should I redraw the entire panel "down " to overlap it? Thanks
[249 byte] By [shaselaia] at [2007-11-27 0:08:26]
# 1

Load your images with ImageIO.read(url_file_inputStream) to get BufferedImages.

There are two general approaches to something like this. One is using components: JLabels

set in a layout manager that forms a grid (eg, GridLayout or GridBagLayout). You set the

images in the labels with setIcon(new ImageIcon(image)). To get the image to disappear use

setIcon(null).

The other approach is a graphic approach in which you draw everything. Here is an example

of how you could put this together with this second approach:

import java.awt.*;

import java.awt.event.*;

import java.awt.geom.*;

import java.awt.image.BufferedImage;

import javax.swing.*;

public class DrawingControl extends JPanel {

BufferedImage[] images;

boolean[] showRows;

int size = 5;

final int PAD = 25;

public DrawingControl() {

initImages();

showRows = new boolean[size];

for(int j = 0; j < showRows.length; j += 2)

showRows[j] = true;

}

protected void paintComponent(Graphics g) {

super.paintComponent(g);

Graphics2D g2 = (Graphics2D)g;

g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,

RenderingHints.VALUE_ANTIALIAS_ON);

int w = getWidth();

int h = getHeight();

// Draw a centered grid.

double xInc = (double)(w - 2*PAD)/size;

double yInc = (double)(h - 2*PAD)/size;

g2.setPaint(Color.red);

for(int j = 0; j <= size; j++) {

double y = PAD + j*yInc;

for(int k = 0; k <= size; k++) {

g2.draw(new Line2D.Double(PAD, y, w-PAD, y));

}

}

for(int j = 0; j <= size; j++) {

double x = PAD + j*xInc;

for(int k = 0; k <= size; k++) {

g2.draw(new Line2D.Double(x, PAD, x, h-PAD));

}

}

// Draw images in visible rows.

int iw = images[0].getWidth();

int ih = images[0].getHeight();

for(int j = 0; j < size; j++) {

if(!showRows[j])

continue;

for(int k = 0; k < size; k++) {

int n = (j*size + k) % images.length;

int x = (int)(PAD + k*xInc + (xInc-iw)/2);

int y = (int)(PAD + j*yInc + (yInc-ih)/2);

g2.drawImage(images[n], x, y, this);

}

}

}

private void initImages() {

int s = 40, type = BufferedImage.TYPE_INT_RGB;

images = new BufferedImage[4];

double theta = 0;

AffineTransform at = new AffineTransform();

for(int j = 0; j < images.length; j++) {

images[j] = new BufferedImage(s, s, type);

Graphics2D g2 = images[j].createGraphics();

g2.setBackground(Color.orange);

g2.clearRect(0,0,s-1,s-1);

g2.setPaint(Color.blue);

g2.drawRect(0,0,s-1,s-1);

g2.setStroke(new BasicStroke(4f));

at.setToRotation(theta, s/2, s/2);

g2.setTransform(at);

g2.setPaint(Color.green.darker());

g2.drawLine(10,s/2,s/2,s-10);

g2.drawLine(s/2,s-10,s-10,s/2);

g2.dispose();

theta += Math.PI/2;

}

}

public static void main(String[] args) {

DrawingControl test = new DrawingControl();

test.addMouseListener(test.rowToggle);

JFrame f = new JFrame();

f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

f.getContentPane().add(test);

f.setSize(400,400);

f.setLocation(200,200);

f.setVisible(true);

}

private MouseListener rowToggle = new MouseAdapter() {

public void mousePressed(MouseEvent e) {

Point p = e.getPoint();

int w = getWidth();

double h = getHeight();

if(p.x > PAD && p.x < w-PAD && p.y > PAD && p.y < h-PAD) {

double yInc = (h - 2*PAD)/size;

int row = (int)((p.y - PAD)/yInc);

showRows[row] = !showRows[row];

repaint();

}

}

};

}

crwooda at 2007-7-11 16:07:34 > top of Java-index,Security,Cryptography...