Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8280979
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T09:52:27+00:00 2026-06-08T09:52:27+00:00

here is code that generate and paint one of tetramino figure every 5 sec.

  • 0

here is code that generate and paint one of tetramino figure every 5 sec.
However, the figure is drown twice and, therefore, twists the original picture.

Here tetramino figure consist of 10×10 pixel blocks.
Constructor Figure tells position of the blocks and color.
method paint(Graphics g) drows Figure f block by block.

import java.applet.*;

import java.awt.Color;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;


import javax.swing.Timer;
public class Tetris extends Applet  {

    Dimension size;
    int x1,y1;
    private int xs=0,ys=0;


    public int getXs() {
        return xs;
    }
    public void setXs(int xs) {
        this.xs = xs;
    }
    public int getYs() {
        return ys;
    }
    public void setYs(int ys) {
        this.ys = ys;
    }
    public void rotate(){

    }
    public Figure generate(){
        Figure i = new Figure(new int[][]{new int[]{1},new int[]{1},new int[]{1},new int[]{1}},Color.CYAN);
        Figure j = new Figure(new int[][]{new int[]{1,0,0}, new int[]{1,1,1}},Color.BLUE);
        Figure l = new Figure(new int[][]{new int[]{0,0,1}, new int[]{1,1,1}},Color.ORANGE);
        Figure o = new Figure(new int[][]{new int[]{1,1}, new int[]{1,1}},Color.YELLOW);
        Figure s = new Figure(new int[][]{new int[]{0,1,1}, new int[]{1,1,0}},Color.GREEN);
        Figure t = new Figure(new int[][]{new int[]{1,1,1}, new int[]{0,1,0}},Color.PINK);
        Figure z = new Figure(new int[][]{new int[]{1,1,0}, new int[]{0,1,1}},Color.RED);

        Figure[] genSet = {i,j,l,o,s,t,z};
        int index =((int) (Math.random() * 7)) ;
        //System.out.println(index);
        return  genSet[index];
    }
    public void drop(){

    }
    public void shift(){

    }
    public void movecheck(){

    }
    public void actiondelay(){
        ActionListener actionlistener = new ActionListener() {
            public void actionPerformed(ActionEvent actionevent){
                repaint();
            }

        };
        Timer timer = new Timer(5000,actionlistener);
        timer.start();
    }
    public void init(){
        setSize(200,400);
        setBackground(Color.black);
        size = getSize();

    }

    public void paint(Graphics g){

        super.paint(g);
        System.out.println("________________");
        Graphics2D g2d = (Graphics2D) g.create();
        Figure f = generate();
        int length = f.getX()[0].length;
        for(int j =0; j<f.getX().length;j++){
            System.out.println();
            ys = 0;                 
            for(int i=0;i<length;i++){                      

                if (f.getX()[j][i] == 1){
                    Rectangle2D p = new Rectangle2D.Double(xs,xs,xs+10,ys+10);
                    g2d.setColor(f.getY());
                    g2d.draw(p);
                    //g2d.drawRect(p.x, p.y, p.width, p.height);    
                    //g2d.fillRect(p.x, p.y, p.width, p.height);                    
                    //System.out.println("widnth: " +p.width + " | height: " + p.height + " end ");
                    System.out.print("*");
                }
                else System.out.print(" ");
                ys+=10;             
            }
            xs+=10;
        }
        xs=0;
        ys=0;
        actiondelay();                      
       //g.setColor(Color.white);
       //g.drawRect(45, 95, 55, 105);        
    }


}


import java.awt.Color;

public class Figure{
    int[][] x;
    Color y;
    public Figure(int[][] x , Color y){
        this.x=x;
        this.y=y;
    }
    public int[][] getX() {
        return x;
    }
    public void setX(int[][] x) {
        this.x = x;
    }
    public Color getY() {
        return y;
    }
    public void setY(Color y) {
        this.y = y;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T09:52:28+00:00Added an answer on June 8, 2026 at 9:52 am

    Your image is not twisted because figure is drawn twice, but because you have few mistakes in new Rectangle2D.Double(xs,xs,xs+10,ys+10);

    1. at starting x and y positions you used xs two times instead of xs and ys
    2. your width and hight isn’t fixed value but related with xs and ys

    Also your loop was increasing xs instead of ys when you ware going to new row.

    Try to replace your paint() method by this:

    public void paint(Graphics g){
    
        super.paint(g);
        System.out.println("________________");
        Graphics2D g2d = (Graphics2D) g.create();
        Figure f = generate();
        int length = f.getX()[0].length;
        for(int j =0; j<f.getX().length; j++){
            System.out.println();
    
            for(int i=0; i<f.getX()[0].length; i++){                      
    
                if (f.getX()[j][i] == 1){
                    Rectangle2D p = new Rectangle2D.Double(xs, ys, 10, 10);
                    g2d.setColor(f.getY());
                    g2d.draw(p);
                    //g2d.drawRect(p.x, p.y, p.width, p.height);    
                    //g2d.fillRect(p.x, p.y, p.width, p.height);                    
                    //System.out.println("widnth: " +p.width + " | height: " + p.height + " end ");
                    System.out.print("*");
                }
                else System.out.print(" ");
                xs+=10;
            }
            xs = 0;
            ys+=10;             
        }
        xs=0;
        ys=0;
        actiondelay();                      
       //g.setColor(Color.white);
       //g.drawRect(45, 95, 55, 105);        
    }
    

    Also javax.swing.Timer(int delay, ActionListener listener) is type of cyclic Thread so you shouldn’t create few instances of it if you want to perform its action once per time set in delay. At your place I would move code from actiondelay to init.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm think something like Facebook apps here. User generated pieces of code that people
I cannot get this to work, here is code that I found in another
Here is my Code that I'm trying to run on Mac OS X: import
Here is the code that doesn't work: Enemy.strength = srand((unsigned)time(NULL)) % 10; Enemy.strength is
Here is my code that adds a line to the grid by pressing the
Here is the code that cause me problem since few hours: TabItem newTab =
Here's my code that is not working: print To: ; my $to=<>; chomp $to;
Here is my code that fails: bool Table::win(const Card &card) { for (int i
Here is the code that I have for my cell of my UITableView: forKeys:[NSArray
I have some code here that uses bitsets to store many 1 bit values

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.