import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class MyPanel extends JPanel{
ImageIcon icon4 = new ImageIcon("Tetris backg.png");
Image img = icon4.getImage();
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
class Main extends JFrame {
ImageIcon icon1 = new ImageIcon("Tetris Block 1.png");
ImageIcon icon2 = new ImageIcon("Tetris Block 2.png");
ImageIcon icon3 = new ImageIcon("Tetris Block 3.png");
JLabel la5=new JLabel();
int i;
public Main() {
setDefaultCloseOperation(EXIT_ON_CLOSE);
setContentPane(new MyPanel());
Container c=getContentPane();
c.setLayout(new FlowLayout());
JLabel[]la=new JLabel[4];
ImageIcon[] names = {icon1, icon2, icon3};
for(i=0;i<3;i++) {
la[i]=new JLabel(names[i]);
c.add(la[i]);
la[i].addMouseListener(new MyMouseListener());
la[i].addKeyListener(new MyKeyListener());
c.add(la[i]);
}
c.add(la5);
c.setFocusable(true);
c.requestFocus();
//c.setBackground(c);
setSize(500,500);
setVisible(true);
}
class MyMouseListener extends MouseAdapter{
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
int a=(int)(Math.random()*3);
if(a==1) {
la5.setIcon(icon1);
}
else if(a==2) {
la5.setIcon(icon2);
}
else if(a==3){
la5.setIcon(icon3);
}
int x = (int)(Math.random() * (getWidth() - la5.getWidth())); // Generate random x-coordinate
int y = 0; // Initial y-coordinate
la5.setBounds(x, y, la5.getWidth(), la5.getHeight()); // Set initial position
new Timer(40, new ActionListener() {
int y = 0;
@Override
public void actionPerformed(ActionEvent e) {
y += 8; // Adjust this value to control falling speed
la5.setBounds(la5.getX(), y, la5.getWidth(), la5.getHeight());
if (y >= getHeight()) { // Stop the timer when label reaches bottom
((Timer) e.getSource()).stop();
}
}
}).start();
}
}
class MyKeyListener extends KeyAdapter{
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
}
}
public static void main(String[] args) {
new Main();
}
}