1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 package ohos.devtools.views.charts.tooltip; 17 18 import javax.swing.JComponent; 19 import java.awt.Color; 20 import java.awt.Dimension; 21 import java.awt.Graphics; 22 23 /** 24 * Legend color block in tooltip 25 * 26 * @since 2021/5/19 16:39 27 */ 28 public class TooltipColorRect extends JComponent { 29 /** 30 * Color block size 31 */ 32 private static final int SIZE = 15; 33 34 /** 35 * Color 36 */ 37 private final Color color; 38 39 /** 40 * Constructor 41 * 42 * @param color Color 43 */ TooltipColorRect(Color color)44 public TooltipColorRect(Color color) { 45 this.color = color; 46 this.setOpaque(false); 47 fillColor(); 48 } 49 50 /** 51 * Fill color 52 */ fillColor()53 private void fillColor() { 54 this.setPreferredSize(new Dimension(SIZE, SIZE)); 55 super.repaint(); 56 super.validate(); 57 } 58 59 /** 60 * Paint component 61 * 62 * @param graphics graphics 63 */ 64 @Override paintComponent(Graphics graphics)65 protected void paintComponent(Graphics graphics) { 66 super.paintComponent(graphics); 67 graphics.setColor(color); 68 graphics.fillRect(0, 0, SIZE, SIZE); 69 } 70 } 71