Study/Java

Graphics을 이용한 Rack View 만들기

momong'-' 2020. 6. 19. 09:07

Main Method가 있는 Main 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.awt.Dimension;
import java.awt.Rectangle;
import java.awt.Toolkit;
 
import javax.swing.UIManager;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class Main {
    private static Logger LOGGER = LogManager.getLogger(Main.class);
    
    public static void main(String[] args) {
        try {
            // Look and Feel UIManager를 통해 ui 쉽게 변경 가능
            UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel");
        }
        catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
        
        TestFrame frame = new TestFrame();
        
        // 화면의 전체 크기
        Dimension scrDim = Toolkit.getDefaultToolkit().getScreenSize();
 
        int xPos = (scrDim.width - TestFrame.WIDTH) / 2;
        int yPos = (scrDim.height - TestFrame.HEIGHT) / 2;
 
        // 1)과 2)를 동시에 처리
        frame.setBounds(new Rectangle(xPos, yPos, TestFrame.WIDTH, TestFrame.HEIGHT));
        // 3) 프레임 표시
        frame.setVisible(true);
    }
}
cs

Frame을 만드는 TestFrame 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
 
import javax.swing.JFrame;
import javax.swing.JOptionPane;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
public class TestFrame extends JFrame {
    private static Logger LOGGER = LogManager.getLogger(TestFrame.class);
    
    public static final int WIDTH     = 870;
    public static final int HEIGHT     = 635;
    
    private TestPanel testPanel = new TestPanel();
    
    public TestFrame() {
        try {
            initComponent();
        }
        catch (Exception ex) {
            LOGGER.error(ex.getMessage(), ex);
        }
    }
    
    private void initComponent() throws Exception {
        this.setTitle("Rack View");
        // 사용자가이 프레임에서 "닫기"를 시작할 때 기본적으로 수행되는 작업을 설정
        this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
        
        this.setContentPane(testPanel);
        
        // 윈도우 이벤트
        this.addWindowListener(new TestFrame_this_WindowAdapter(this));
    }
    
    void windowClosing(WindowEvent e) {
        LOGGER.debug("종료 버튼이 선택되었습니다.");
        
        int result = JOptionPane.showConfirmDialog(TestFrame.this
                "정말로 종료하시겠습니까?""EXIT", JOptionPane.OK_CANCEL_OPTION);
        
        if ( result == JOptionPane.OK_OPTION ) {
            System.exit(0);
        }
    }
    
}
 
class TestFrame_this_WindowAdapter extends WindowAdapter {
    private TestFrame adaptee;
    
    public TestFrame_this_WindowAdapter(TestFrame adaptee) {
        this.adaptee = adaptee;
    }
    @Override
    public void windowClosing(WindowEvent e) {
        adaptee.windowClosing(e);
    }
}
cs

Panel 생성하는 TestPanel 클래스 - Rack View 생성

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;
 
import javax.swing.JPanel;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import common.BoardVo;
import common.constants.BoardType;
 
public class TestPanel extends JPanel {
    private static Logger LOGGER = LogManager.getLogger(TestPanel.class);
 
    public static final int TEXT_WIDTH        = 30;
    public static final int TEXT_HEIGHT        = 18;
    
    public final int BOARD_START_X            = 27;
    public final int BOARD_START_Y_TOP        = 26;
    public final int BOARD_START_Y_BOTTOM     = 307;
    
    public TestPanel() {
        try {
            initComponent();
            initData();
        }
        catch ( Exception e ) {
            LOGGER.debug(e.getMessage(), e);
        }
    }
 
    private void initComponent() {
        this.setLayout(null);
    }
 
 
    private void initData() {
        // 연동에 의해 board 정보 조회
        List<BoardVo> boardList = getBoardData();
        
        for (BoardVo oneBoardVo : boardList) {
            int boardId = oneBoardVo.getBoardId();
            
            BoardPanel boardPanel = new BoardPanel(oneBoardVo);
 
            this.add(boardPanel, null);
            
            boardPanel.repaint();
            
            if ( boardId < 20 ) {
                boardPanel.setBounds(BOARD_START_X + boardId * boardPanel.BOARD_WIDTH,
                        BOARD_START_Y_TOP,
                        boardPanel.getBoardWidth(),
                        boardPanel.getBoardHeight());
            }
            else {
                boardPanel.setBounds(BOARD_START_X + ((boardId % 20+ 2* boardPanel.BOARD_WIDTH,
                        BOARD_START_Y_BOTTOM,
                        boardPanel.getBoardWidth(),
                        boardPanel.getBoardHeight());
            }
            LOGGER.debug("--- TestPanel에 {} boardPanel 추가", boardId);
        }
    }
 
    private List<BoardVo> getBoardData() {
        List<BoardVo> boardList = new ArrayList<>();
 
        for ( int i = 0; i < 37; i++ ) {        
            BoardVo boardVo = new BoardVo();
            
            // 0, 1번 MPU 2 ~ 17번, 20 ~ 35번 SALC, 18번 36번 SRGU
            if ( i < 2 ) {
                boardVo.setBoardType(BoardType.MPU);
                boardVo.setBoardName("MPU" + i);
                boardVo.setSeverity((int) (Math.random()*4));
                boardVo.setBoardId(i);
            }
            else if ( i == 18 || i == 36 ) {
                boardVo.setBoardType(BoardType.SRGU);
                boardVo.setBoardName("SRGU" + i);
                boardVo.setSeverity((int) (Math.random()*4));
                boardVo.setBoardId(i);
            }
            else {
                boardVo.setBoardType(BoardType.SALC);
                boardVo.setBoardName("SALC" + i);
                boardVo.setSeverity((int) (Math.random()*4));
                boardVo.setBoardId(i);
            }
            boardList.add(boardVo);
        }
        
        return boardList;
    }
 
    @Override
    protected void paintComponent(Graphics g) {
        LOGGER.debug("paint_Background_Component");
        
        g.drawImage(ImageFactory.backgroundImageIcon.getImage(),
                00this);
    }
}
cs