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.trace.fragment.graph; 17 18 import ohos.devtools.views.trace.fragment.AbstractDataFragment; 19 import ohos.devtools.views.trace.fragment.ruler.AbstractNode; 20 21 import javax.swing.JComponent; 22 import java.awt.Graphics2D; 23 import java.awt.Rectangle; 24 import java.awt.event.MouseEvent; 25 26 /** 27 * Data line graphics 28 * 29 * @since 2021/04/22 12:25 30 */ 31 public abstract class AbstractGraph extends AbstractNode { 32 /** 33 * fragment 34 */ 35 public AbstractDataFragment fragment; 36 37 /** 38 * root 39 */ 40 public JComponent root; 41 42 /** 43 * rect 44 */ 45 public Rectangle rect = new Rectangle(0, 0, 0, 0); 46 47 /** 48 * Control the focus blur event is triggered only once, the focus blur is initiated by mouseMove 49 */ 50 public boolean flagFocus; 51 52 /** 53 * Drawing method 54 * 55 * @param graphics graphics 56 */ draw(Graphics2D graphics)57 public abstract void draw(Graphics2D graphics); 58 59 /** 60 * Get focus event 61 * 62 * @param event event 63 */ onFocus(MouseEvent event)64 public abstract void onFocus(MouseEvent event); 65 66 /** 67 * Loss of focus event 68 * 69 * @param event event 70 */ onBlur(MouseEvent event)71 public abstract void onBlur(MouseEvent event); 72 73 /** 74 * Click event 75 * 76 * @param event event 77 */ onClick(MouseEvent event)78 public abstract void onClick(MouseEvent event); 79 80 /** 81 * Mouse movement event 82 * 83 * @param event event 84 */ onMouseMove(MouseEvent event)85 public abstract void onMouseMove(MouseEvent event); 86 87 /** 88 * Whether the mouse is inside the graphics area 89 * 90 * @param event event 91 * @return boolean boolean 92 */ edgeInspect(final MouseEvent event)93 public boolean edgeInspect(final MouseEvent event) { 94 return rect.contains(event.getPoint()); 95 } 96 97 /** 98 * Redraw the current graph 99 */ repaint()100 public void repaint() { 101 if (root != null) { 102 root.repaint(rect); 103 } 104 } 105 106 /** 107 * Set rect object 108 * 109 * @param xAxis x coordinate 110 * @param yAxis y coordinate 111 * @param width width 112 * @param height height 113 */ setRect(final double xAxis, final double yAxis, final double width, final double height)114 public void setRect(final double xAxis, final double yAxis, final double width, final double height) { 115 if (rect == null) { 116 rect = new Rectangle(); 117 } 118 rect.setRect(xAxis, yAxis, width, height); 119 } 120 } 121