不错的渲染例子

表单元格渲染器的使用

使用表格渲染器渲染表格

在使用JTable时,用户往往希望改变它缺省的渲染方式,比如使用间隔色的行,对特定的单元格进行特殊颜色显示等,这对一些可视化编程环境的表格并不是一件容易的事。
在Java Swing编程中我们可以使用DefaultTableCellRenderer的子类渲染表格来达到这个目的,实现和使用它都非常容易。

渲染效果一:


步骤一:实现一个javax.swing.table.DefaultTableCellRenderer的子类

/**
* 间隔色表格渲染类
*/
public class ColorTableCellRenderer extends DefaultTableCellRenderer {
  private static final long serialVersionUID = -3378036327580475639L;

  public Component getTableCellRendererComponent(
               JTable table,
               Object value,
               boolean isSelected,
               boolean hasFocus,
               int row,
               int column) {
   
               // 得到单元格
               Component cell =
                       super.getTableCellRendererComponent(
                               table,
                               value,
                               isSelected,
                               hasFocus,
                               row,
                               column);
               // 进行渲染
               if (hasFocus) {
                 // 如果获得焦点则设置背景色为红色
                   cell.setBackground(Color.red);
                   //cell.setForeground(Color.black);
               } else {
                   if ((row % 2) == 0) {
                     // 偶数行设置为白色
                       cell.setBackground(Color.white);
                   } else {
                     // 奇数行设置为蓝色
                       cell.setBackground(Color.cyan);
                   }
               }
              
               return cell;
       }
}

步骤二:将ColorTableCellRenderer设置为表格的渲染器

 try {
 ColorTableCellRenderer cellRender = new ColorTableCellRenderer();
 table.setDefaultRenderer(Class.forName("java.lang.Object"),
  cellRender);
} catch (Exception e) {
 e.printStackTrace();
}

实现一个将特定单元格设置为红色的表格渲染器

如右,如果想将成员年龄大于37的单元格设置为红色。

AgeTableCellRenderer的代码

public class AgeTableCellRenderer extends DefaultTableCellRenderer {
  private static final long serialVersionUID = -334535475639L;

  public Component getTableCellRendererComponent(
               JTable table,
               Object value,
               boolean isSelected,
               boolean hasFocus,
               int row,
               int column) {
   
               // 得到单元格
               Component cell =
                       super.getTableCellRendererComponent(
                               table,
                               value,
                               isSelected,
                               hasFocus,
                               row,
                               column);

               // 先把所有单元格设置为白色
               cell.setBackground(Color.white);
              
               // 进行渲染
               if (table.getColumnName(column).equals("年龄") ) { // 如果列名等于“年龄”
                 // 取得单元格的文字
                 String strValue=(String)value;
                
                 if(Pattern.matches("//d+", strValue)){
                   if(Integer.parseInt(strValue)>37){
                     // 如果是数字且值大于37,将单元格背景设置为红色
                     cell.setBackground(Color.red);
                   }                  
                 }
               }
              
               return cell;
       }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的使用 Vulkan 渲染 RGBA 图像的例子: ```c++ // 初始化 Vulkan 设备和窗口等对象 ... // 创建 Vulkan 图像对象,并分配内存 VkImageCreateInfo imageCreateInfo = {}; imageCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_CREATE_INFO; imageCreateInfo.imageType = VK_IMAGE_TYPE_2D; imageCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; imageCreateInfo.extent = { width, height, 1 }; imageCreateInfo.mipLevels = 1; imageCreateInfo.arrayLayers = 1; imageCreateInfo.samples = VK_SAMPLE_COUNT_1_BIT; imageCreateInfo.tiling = VK_IMAGE_TILING_OPTIMAL; imageCreateInfo.usage = VK_IMAGE_USAGE_TRANSFER_DST_BIT | VK_IMAGE_USAGE_SAMPLED_BIT; vkCreateImage(device, &imageCreateInfo, nullptr, &image); VkMemoryRequirements memoryRequirements; vkGetImageMemoryRequirements(device, image, &memoryRequirements); VkMemoryAllocateInfo memoryAllocateInfo = {}; memoryAllocateInfo.sType = VK_STRUCTURE_TYPE_MEMORY_ALLOCATE_INFO; memoryAllocateInfo.allocationSize = memoryRequirements.size; memoryAllocateInfo.memoryTypeIndex = findMemoryType(memoryRequirements.memoryTypeBits, VK_MEMORY_PROPERTY_DEVICE_LOCAL_BIT); vkAllocateMemory(device, &memoryAllocateInfo, nullptr, &memory); vkBindImageMemory(device, image, memory, 0); // 将 RGBA 数据写入 Vulkan 图像对象的内存中 void* data; vkMapMemory(device, memory, 0, VK_WHOLE_SIZE, 0, &data); memcpy(data, rgbaData, width * height * 4); vkUnmapMemory(device, memory); // 创建 Vulkan 图片视图对象 VkImageViewCreateInfo imageViewCreateInfo = {}; imageViewCreateInfo.sType = VK_STRUCTURE_TYPE_IMAGE_VIEW_CREATE_INFO; imageViewCreateInfo.image = image; imageViewCreateInfo.viewType = VK_IMAGE_VIEW_TYPE_2D; imageViewCreateInfo.format = VK_FORMAT_R8G8B8A8_UNORM; imageViewCreateInfo.subresourceRange.aspectMask = VK_IMAGE_ASPECT_COLOR_BIT; imageViewCreateInfo.subresourceRange.baseMipLevel = 0; imageViewCreateInfo.subresourceRange.levelCount = 1; imageViewCreateInfo.subresourceRange.baseArrayLayer = 0; imageViewCreateInfo.subresourceRange.layerCount = 1; vkCreateImageView(device, &imageViewCreateInfo, nullptr, &imageView); // 创建 Vulkan 着色器模块对象 VkShaderModuleCreateInfo shaderModuleCreateInfo = {}; shaderModuleCreateInfo.sType = VK_STRUCTURE_TYPE_SHADER_MODULE_CREATE_INFO; shaderModuleCreateInfo.codeSize = sizeof(vertShaderCode); shaderModuleCreateInfo.pCode = vertShaderCode; vkCreateShaderModule(device, &shaderModuleCreateInfo, nullptr, &vertShaderModule); // 创建 Vulkan 渲染管线对象 VkPipelineLayoutCreateInfo pipelineLayoutCreateInfo = {}; pipelineLayoutCreateInfo.sType = VK_STRUCTURE_TYPE_PIPELINE_LAYOUT_CREATE_INFO; vkCreatePipelineLayout(device, &pipelineLayoutCreateInfo, nullptr, &pipelineLayout); VkGraphicsPipelineCreateInfo pipelineCreateInfo = {}; pipelineCreateInfo.sType = VK_STRUCTURE_TYPE_GRAPHICS_PIPELINE_CREATE_INFO; pipelineCreateInfo.stageCount = 2; pipelineCreateInfo.pStages = shaderStages; pipelineCreateInfo.pVertexInputState = &vertexInputStateCreateInfo; pipelineCreateInfo.pInputAssemblyState = &inputAssemblyStateCreateInfo; pipelineCreateInfo.pViewportState = &viewportStateCreateInfo; pipelineCreateInfo.pRasterizationState = &rasterizationStateCreateInfo; pipelineCreateInfo.pMultisampleState = &multisampleStateCreateInfo; pipelineCreateInfo.pDepthStencilState = &depthStencilStateCreateInfo; pipelineCreateInfo.pColorBlendState = &colorBlendStateCreateInfo; pipelineCreateInfo.layout = pipelineLayout; pipelineCreateInfo.renderPass = renderPass; pipelineCreateInfo.subpass = 0; vkCreateGraphicsPipelines(device, VK_NULL_HANDLE, 1, &pipelineCreateInfo, nullptr, &pipeline); // 在渲染时使用 Vulkan 图像视图进行渲染 ... ``` 以上代码仅为演示,部分细节和函数调用可能有所省略或调整。如果需要完整的代码示例,可以参考 Vulkan SDK 中的示例代码,例如 VulkanExample 中的 `texture.cpp` 示例。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值