图层的渲染
学习于bilibili 尚学堂官方
1. 创建一个测试类
在test包中创建TestRenderLayer类
用来测试图层渲染
package com.zy.test;
/*
测试渲染一个图层的数据
*/
import com.zy.model.Brand;
import com.zy.model.Cell;
import com.zy.model.Layer;
import com.zy.util.LayerUtil;
import com.zy.view.Start;
import javax.swing.*;
public class TestRenderLayer extends JFrame {
public Layer layer = LayerUtil.build(6,6);//36个单元格的图层
public TestRenderLayer(){
//初始化窗口
init();
//渲染图层
Cell[][] cells = layer.getCells();
for (int row = 0; row < cells.length; row++) {
for (int col = 0; col < cells[row].length; col++) {
Brand brands1 = cells[row][col].getBrand();
this.getContentPane().add(brands1);
}
System.out.println();
}
//自动刷新
autoRefresh();
}
private void init(){
this.setTitle("易-java-羊了个羊");//设置弹出游戏窗口的标题
this.setSize(450,800);//设置窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭的同时也推出java进程
this.setLocationRelativeTo(null);//居中
this.setVisible(true);//让窗口显示 默认隐藏
}
//线程
private void autoRefresh(){
JFrame start = this;
new Thread(new Runnable() {
@Override
public void run() {
//用来刷新
while (true){
start.repaint();//调用的还是当前窗口的repaint
try {
Thread.sleep(40);
start.setVisible(true);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
public static void main(String[] args) {
new TestRenderLayer();
}
}
此时默认坐标是(0,0),运行只能看见一张牌
2. 坐标
代码修改为
package com.zy.test;
/*
测试渲染一个图层的数据
*/
import com.zy.model.Brand;
import com.zy.model.Cell;
import com.zy.model.Layer;
import com.zy.util.LayerUtil;
import com.zy.view.Start;
import javax.swing.*;
public class TestRenderLayer extends JFrame {
public Layer layer = LayerUtil.build(6,6);//36个单元格的图层
public TestRenderLayer(){
//初始化窗口
init();
//渲染图层
//默认坐标是(0,0)
//布局方式 默认swing 添加组件 提供了多种布局方式 网格 流
Cell[][] cells = layer.getCells();
for (int row = 0; row < cells.length; row++) {
for (int col = 0; col < cells[row].length; col++) {
Brand brands1 = cells[row][col].getBrand();
int x = col * 50;//0 50 100 150 ...
int y = row * 50;//0 50 100 ...
brands1.setBounds(x,y,50,50);
this.getContentPane().add(brands1);
}
System.out.println();
}
//自动刷新
autoRefresh();
}
private void init(){
this.setTitle("易-java-羊了个羊");//设置弹出游戏窗口的标题
this.setSize(450,800);//设置窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭的同时也推出java进程
this.setLocationRelativeTo(null);//居中
//设置绝对布局
this.setLayout(null);
this.setBounds(0,0,450,800);
this.setVisible(true);//让窗口显示 默认隐藏
}
//线程
private void autoRefresh(){
JFrame start = this;
new Thread(new Runnable() {
@Override
public void run() {
//用来刷新
while (true){
start.repaint();//调用的还是当前窗口的repaint
try {
Thread.sleep(40);
start.setVisible(true);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
public static void main(String[] args) {
new TestRenderLayer();
}
}
3. 代码分装
package com.zy.test;
/*
测试渲染一个图层的数据
*/
import com.zy.model.Brand;
import com.zy.model.Cell;
import com.zy.model.Layer;
import com.zy.util.LayerUtil;
import com.zy.view.Start;
import javax.swing.*;
public class TestRenderLayer extends JFrame {
public Layer layer = LayerUtil.build(6,6);//36个单元格的图层
public TestRenderLayer(){
//初始化窗口
init();
//渲染图层
renderLayer();
//自动刷新
autoRefresh();
}
private void renderLayer(){
//渲染图层
//默认坐标是(0,0)
//布局方式 默认swing 添加组件 提供了多种布局方式 网格 流
Cell[][] cells = layer.getCells();
for (int row = 0; row < cells.length; row++) {
for (int col = 0; col < cells[row].length; col++) {
Brand brands1 = cells[row][col].getBrand();
int x = col * 50;//0 50 100 150 ...
int y = row * 50;//0 50 100 ...
brands1.setBounds(x,y,50,50);
this.getContentPane().add(brands1);
}
System.out.println();
}
}
private void init(){
this.setTitle("易-java-羊了个羊");//设置弹出游戏窗口的标题
this.setSize(450,800);//设置窗口大小
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//关闭的同时也推出java进程
this.setLocationRelativeTo(null);//居中
//设置绝对布局
this.setLayout(null);
this.setBounds(0,0,450,800);
this.setVisible(true);//让窗口显示 默认隐藏
}
//线程
private void autoRefresh(){
JFrame start = this;
new Thread(new Runnable() {
@Override
public void run() {
//用来刷新
while (true){
start.repaint();//调用的还是当前窗口的repaint
try {
Thread.sleep(40);
start.setVisible(true);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
}
}).start();
}
public static void main(String[] args) {
new TestRenderLayer();
}
}