-
Graphics을 이용한 Rack View 만들기Study/Java 2020. 6. 19. 09:07
Main Method가 있는 Main 클래스
1234567891011121314151617181920212223242526272829303132333435import 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 클래스
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061import 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;}@Overridepublic void windowClosing(WindowEvent e) {adaptee.windowClosing(e);}}cs
Panel 생성하는 TestPanel 클래스 - Rack View 생성
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105import 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번 SRGUif ( 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;}@Overrideprotected void paintComponent(Graphics g) {LOGGER.debug("paint_Background_Component");g.drawImage(ImageFactory.backgroundImageIcon.getImage(),0, 0, this);}}cs
'Study > Java' 카테고리의 다른 글
Http -> Https TLS/SSL 적용하기 (0) 2020.06.30 [CSS] CSS 적용 우선순위 (0) 2020.06.30 [Class] Thread (0) 2020.06.18 [Class] Graphics/Graphics2D (0) 2020.06.18 RMI 채팅프로그램(Swing) (0) 2020.06.17