본문 바로가기
인턴

[인턴 OJT 11일차] Swing 아날로그 시계 구현, JTree 주소록

by 리잼 2022. 12. 30.
반응형

 

아날로그 시계

 

private Timer timer;
int sec, min, hour, secX, secY, minX, minY, hourX, hourY, oX, oY, r;
 

변수 선언시 전역변수를 남발하지 않는다. 확실하게 선언하고 사용할 수 있도록함

개발실무에서 전역변수를 남발하면 코드가 꼬일 수 있고, 유지보수가 어려워짐.

	private void initTimer() {
		timer = new Timer();
		timer.scheduleAtFixedRate(new TimerTask() {
			public void run() {
				initData(); // 시간정보를 가져온다
				ClockPanel.this.repaint(); // 계속 다시 그려준다
			}
		}, 0, 1000L);
	}
 

run() 무한으로 돌면서 시간정보를 가져온다.

repain()에서 시간정보를 가지고 화면에 출력해준다.

		cal = Calendar.getInstance();
		sec = cal.get(Calendar.SECOND);
		min = cal.get(Calendar.MINUTE);
		hour = cal.get(Calendar.HOUR);
		
		LOGGER.debug(String.format("%d 시 %d분 %d초", hour, min, sec));
		
		sec = sec * 6 - 90; //초를 구하는 공식
		// 초침
		secX = (int) (130 * Math.cos((sec * Math.PI / 180)) + oX);
		secY = (int) (130 * Math.sin((sec * Math.PI / 180)) + oY);
		// 분침
		min = min * 6 + (sec / 60 - 90); //분을 구하는 공식
		minX = (int) (100 * Math.cos((min * Math.PI / 180)) + oX);
		minY = (int) (100 * Math.sin((min * Math.PI / 180)) + oY);
		// 시침
		hour = hour * 6 + (min / 2 - 90); //시를 구하는 공식
		hourX = (int) (80 * Math.cos((hour * Math.PI / 180)) + oX);
		hourY = (int) (80 * Math.sin((hour * Math.PI / 180)) + oY);
 

공식은 사용할 때 이유를 확실히 알고 사용할 수 있도록 한다.

 


JTree 주소록

 

프레임 설정

	private GridBagLayout gridBagLayout = new GridBagLayout();

	
	// 프레임 설정
	private JPanel jPanel_Main = new JPanel();
	private JPanel jPanel_Top = new JPanel();
	private JPanel jPanel_Bottom = new JPanel();
	private JTree jTree_Result = new JTree();
	
	// 라벨설정
	private JLabel jLabel_group_id = new JLabel("그룹 ID");
	private JLabel jLabel_group_name = new JLabel("그룹명");
	private JLabel jLabel_parent_group_id = new JLabel("상위 그룹명");
	// 텍스트 박스 설정
	private JTextField jTextField_group_id = new JTextField();
	private JTextField jTextField_group_name = new JTextField();
	private JTextField jTextField_parent_group_id = new JTextField();
	// 버튼 설정
	private JButton jButton_Save = new JButton("저장");
	private JButton jButton_Update = new JButton("수정");
	private JButton jButton_Delete = new JButton("삭제");
	
	private JScrollPane jScrollPane_Tree = new JScrollPane();
	
	private DefaultMutableTreeNode rootNode = new DefaultMutableTreeNode("root");
	private DefaultTreeModel treeModel = new DefaultTreeModel(rootNode);
	
	private GroupInterface addressBookIf;
 
	private void initData() {
		
		List<GroupVo> groupList = addressBookIf.selectGroup(null);
		Vector<DefaultMutableTreeNode> parent = new Vector<>();
		
		for (int i = 0; i < groupList.size(); i++) {
			GroupVo oneGroup = groupList.get(i);
			
			DefaultMutableTreeNode oneNode = new DefaultMutableTreeNode();
			oneNode.setUserObject(oneGroup);
		
			if (oneGroup.getParent_group_id() == 0) { // Parent_group_id가 0을 부모노드로 설정
				parent.add(oneNode);
			}
			else {
				for (int son = 0; son < parent.size(); son++) {
					if (((GroupVo) parent.get(son).getUserObject()).getGroup_id() == oneGroup.getParent_group_id()) {
						parent.get(son).add(oneNode);
						break;
					}
				}
			}
		}
		for (DefaultMutableTreeNode par : parent) {
			rootNode.add(par);
		}
		treeModel.reload();
		
	}
 

 

groupImpl

package com.barunsw.ojt.jmlee.day10;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class GroupImpl implements GroupInterface {
   	
	private static final Logger LOGGER = LoggerFactory.getLogger(GroupImpl.class);
	
	private static final SqlSessionFactory sqlSessionFactory = SqlSessionFactoryManager.getSqlSessionFactory();

	@Override
	public List<GroupVo> selectGroup(GroupVo paramData) {
		
		List<GroupVo> groupBook = new ArrayList<>();
		
		try (SqlSession session = sqlSessionFactory.openSession()) {
			GroupInterface mapper = session.getMapper(GroupInterface.class);
			
			groupBook = mapper.selectGroup(new GroupVo());
			
			for (GroupVo s : groupBook) {
				LOGGER.debug(s.toString());
			}
		} catch (Exception ex) {
			LOGGER.debug(ex.getMessage(), ex);
		}
		return groupBook;
	}

	@Override
	public int insertGroup(GroupVo paramData) throws Exception {
		// TODO Auto-generated method stub
		try (SqlSession session = sqlSessionFactory.openSession()) {
			System.out.println("insert 호출");
			GroupInterface mapper = session.getMapper(GroupInterface.class);
			
			mapper.insertGroup(paramData);
			
			session.commit();
			System.out.println("insert end");	
		}
		catch (Exception ex) {
			LOGGER.debug(ex.getMessage(), ex);
		}
		
		
		return 0;
	}

	@Override
	public int updateGroup(GroupVo paramData) throws Exception {
		try (SqlSession session = sqlSessionFactory.openSession()) {
			LOGGER.debug("update 호출");
			GroupInterface mapper = session.getMapper(GroupInterface.class);
			
			mapper.updateGroup(paramData);
			session.commit();
			LOGGER.debug("update end");	
		}		
		catch (Exception ex) {
			LOGGER.debug(ex.getMessage(), ex);
		}
		return 0;
	}

	@Override
	public int deleteGroup(GroupVo paramData) throws Exception {
		try (SqlSession session = sqlSessionFactory.openSession()) {
			LOGGER.debug("Delete 호출");
			GroupInterface mapper = session.getMapper(GroupInterface.class);
			
			mapper.deleteGroup(paramData);
			session.commit();
			LOGGER.debug("Delete end");	
		}				
		catch (Exception ex) {
			LOGGER.debug(ex.getMessage(), ex);
		}
		return 0;
	}

	@Override
	public GroupVo selectOneGroup(GroupVo paramData) {
		GroupVo groupList = new GroupVo();
		try (SqlSession session = sqlSessionFactory.openSession()) {
			GroupInterface mapper = session.getMapper(GroupInterface.class);
			groupList = mapper.selectOneGroup(paramData);
		}
		return null;
	}

}
 

DB 테이블이 바뀌어서 전부 변경

 

GroupDao.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
  PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
  "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.barunsw.ojt.jmlee.day10.GroupInterface">
  <select id="selectGroup" parameterType="com.barunsw.ojt.jmlee.day10.GroupVo" 
  	resultType="com.barunsw.ojt.jmlee.day10.GroupVo">
    SELECT * FROM tb_group order by parent_group_id asc
  </select>
  
  <insert id="insertGroup" parameterType="com.barunsw.ojt.jmlee.day10.GroupVo">
  	INSERT INTO tb_group
  	(GROIP_ID, GROUP_NAME, PARENT_GROUP_ID) 
  	VALUES (#{group_id}, #{group_name}, #{parent_group_id})
  </insert>
  
  <update id="updateGroup" parameterType="com.barunsw.ojt.jmlee.day10.GroupVo">
  	UPDATE tb_group
  	SET GROUP_NAME = #{group_name}, PARENT_GROUP_ID=#{parent_group_id}
    WHERE GROUP_ID=#{group_id}
  </update>
  
  <delete id="deleteGroup" parameterType="com.barunsw.ojt.jmlee.day10.GroupVo">
  	DELETE FROM tb_group
  	WHERE GROIP_ID = #{group_id}
  </delete>
  
    <select id="selectOneGroup" parameterType="com.barunsw.ojt.jmlee.day10.GroupVo" 
  	resultType="com.barunsw.ojt.jmlee.day10.GroupVo">
    SELECT * FROM tb_group WHERE GROUP_NAME = #{group_name}
  </select>
  
</mapper>
 

마찬가지로 xml파일들도 올바르게 맵핑해준다

 

 

반응형