⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jsfcanvas.java

📁 这是一个俄罗斯方块的JAVA实现
💻 JAVA
字号:
package com.sunfruit.draw.swing;

import com.sunfruit.draw.framework.JSFComponentface;
import java.awt.Graphics;
import java.util.ArrayList;
import java.awt.Shape;
import java.awt.Graphics2D;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;
import java.awt.Rectangle;
import com.sunfruit.draw.framework.JSFCanvasface;
import java.awt.image.BufferedImage;
import java.awt.RenderingHints;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2007</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
public class JSFCanvas extends java.awt.Canvas implements KeyListener, JSFCanvasface {

  private ArrayList arrayList=new ArrayList();

  public JSFComponentface jsfComponentface_=null;

  private int[][] drawRect=null;

  private int space=20;

  public Rectangle rectangle=new Rectangle(0,0,180,360);

  private boolean[][] reseau=new boolean[18][9];

  public JSFCanvas() {
    this.setBackground(new Color(255,255,255));
    initEvent();

    for(int i=0;i<reseau.length;i++)
    {
      for(int j=0;j<reseau[i].length;j++)
      {
        reseau[i][j]=false;
      }
    }
  }

  public void init()
  {
    drawRect=null;
    jsfComponentface_=null;
    for(int i=0;i<reseau.length;i++)
    {
      for(int j=0;j<reseau[i].length;j++)
      {
        reseau[i][j]=false;
      }
    }

  }
  /**
   * 添加组件
   * @param jsfComponentface JSFComponentface
   */
  public void addComponent(JSFComponentface jsfComponentface)
  {
    arrayList.add(jsfComponentface);
  }

  private void initEvent()
  {
    this.addKeyListener(this);
  }

  /**
   * 绘制屏幕
   * @param g Graphics
   */
  public void paint(Graphics g)
  {
    //Graphics2D g2d = (Graphics2D) g;
    //建立双缓冲
    BufferedImage bufferedImage=new BufferedImage(rectangle.width,rectangle.height,BufferedImage.TYPE_INT_ARGB);

    Graphics2D g2d = (Graphics2D)bufferedImage.getGraphics();
    g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    BasicStroke s = new BasicStroke(1);
    g2d.setStroke(s);

    drawLines(g2d);
//    //显示选择框
    for(int i=0;i<arrayList.size();i++)
    {
      JSFComponentface jsfComponentface=(JSFComponentface)arrayList.get(i);
      int[][] rects=jsfComponentface.getRects();
      for(int j=0;j<rects.length;j++)
      {
        if(rects[j]!=null)
        {
          int x1 = rects[j][0];
          int y1 = rects[j][1];
          Rectangle rectangle = new Rectangle(x1 * space, y1 * space, space,
                                              space);
          g2d.setColor(jsfComponentface.getFillColor());
          g2d.fill(rectangle);
          g2d.setColor(jsfComponentface.getFrameColor());
          g2d.draw(rectangle);
        }
      }

    }

    if(jsfComponentface_!=null)
    {
      int[][] rects=jsfComponentface_.getRects();
      for(int j=0;j<rects.length;j++)
      {
        if(rects[j]!=null)
        {
          int x1 = rects[j][0];
          int y1 = rects[j][1];
          Rectangle rectangle = new Rectangle(x1 * space, y1 * space, space,
                                              space);
          g2d.setColor(jsfComponentface_.getFillColor());
          g2d.fill(rectangle);
          g2d.setColor(jsfComponentface_.getFrameColor());
          g2d.draw(rectangle);
        }
      }

    }

    g2d.setColor(Color.black);
    g2d.draw(rectangle);

    //将双缓冲绘制到屏幕上
    //super.paint(g);
    if(drawRect!=null)
    {
      for(int j=0;j<drawRect.length;j++)
      {
        int x1=drawRect[j][0];
        int y1=drawRect[j][1];
        Rectangle rectangle=new Rectangle(x1*space,y1*space,space,space);
        g.clearRect(rectangle.x,rectangle.y,space,space);
      }
    }

    if(jsfComponentface_!=null)
    {
      int[][] rects=jsfComponentface_.getRects();
      drawRect = new int[rects.length][2];
      for(int i=0;i<rects.length;i++)
      {
        if(rects[i]!=null)
        {
          drawRect[i][0] = rects[i][0];
          drawRect[i][1] = rects[i][1];
        }
      }
    }
    Graphics2D g2d1 = (Graphics2D) g;
    g2d1.drawImage(bufferedImage,0,0,null);
  }

  /**
   * 屏幕网格
   * @param g2d Graphics2D
   */
  private void drawLines(Graphics2D g2d)
  {
    //画横线
    g2d.setColor(new Color(240,240,240));
    int y=rectangle.y+rectangle.height;
    for(int i=0;i<y;i+=space)
    {
      g2d.drawLine((int)rectangle.getMinX(),i,(int)rectangle.getMaxX(),i);
    }
    //画竖线
    int x=rectangle.x+rectangle.width;
    for(int i=0;i<x;i+=space)
    {
      g2d.drawLine(i,(int)rectangle.getMinY(),i,(int)rectangle.getMaxY());
    }

  }

  /**
   * 添加组件
   * @param e Shape
   */
  public void addShape(JSFComponentface e)
  {
    arrayList.add(e);
  }

  public void setArrayList(ArrayList arrayList)
  {
    this.arrayList=arrayList;
  }


  public void removeShape(JSFComponentface e)
  {
    arrayList.remove(e);
  }

  public void removeAll()
  {
    arrayList.clear();
    jsfComponentface_=null;
  }
  /**
   * 删除
   * @param i int
   */
  public void removeShape(int i)
  {
    arrayList.remove(i);
  }

  /**
   * Invoked when a key has been typed.
   *
   * @param e KeyEvent
   * @todo Implement this java.awt.event.KeyListener method
   */
  public void keyTyped(KeyEvent e) {
  }

  /**
   * Invoked when a key has been pressed.
   *
   * @param e KeyEvent
   * @todo Implement this java.awt.event.KeyListener method
   */
  public void keyPressed(KeyEvent e) {
    int code=e.getKeyCode();
    if(!(code>=37 && code<=40)) return;
    int state=jsfComponentface_.getState();
    if(code==38) state--;
    if(state==-1) state=3;

    if(code==37)
      jsfComponentface_.moveLeft();

    if(code==39)
      jsfComponentface_.moveRight();

    if(code==40)
    {
      jsfComponentface_.moveBottom();
    }
    jsfComponentface_.setState(state);
  }

  /**
   * Invoked when a key has been released.
   *
   * @param e KeyEvent
   * @todo Implement this java.awt.event.KeyListener method
   */
  public void keyReleased(KeyEvent e) {
  }

  /**
   * getComponent
   *
   * @return ArrayList
   * @todo Implement this com.sunfruit.draw.framework.JSFCanvasface method
   */
  public JSFComponentface[] getComponents() {
    JSFComponentface[] jsfComponentfaces=(JSFComponentface[])arrayList.toArray(new JSFComponentface[0]);
    return jsfComponentfaces;
  }

  public boolean[][] getReseau() {
    return reseau;
  }

  public JSFComponentface getJSFComponentface_()
  {
    return jsfComponentface_;
  }

  public void setJSFComponentface_(JSFComponentface jsfComponentface)
  {
    this.jsfComponentface_=jsfComponentface;
  }

  public int getSpace() {
    return space;
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -