View Single Post
  #9 (permalink)  
Old 03-14-2008, 07:52 AM
Preethi Preethi is offline
Member
 
Join Date: Jan 2008
Posts: 83
Preethi is on a distinguished road
Code:
package saxExamples; import javax.swing.*; import java.awt.*; import java.awt.BorderLayout; import java.awt.Color; import java.awt.Container; import java.awt.Dimension; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.Rectangle; import java.awt.RenderingHints; import java.awt.event.*; import java.awt.geom.*; import java.io.FileNotFoundException; import java.io.IOException; import java.sql.Time; import java.text.*; import java.util.ArrayList; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.Iterator; import java.util.Map; import javax.swing.*; import org.xml.sax.*; import org.xml.sax.helpers.*; public class mousedrag extends javax.swing.JApplet { @Override public void init() { Container cont = getContentPane(); cont.setLayout(new BorderLayout()); setPreferredSize(new Dimension(300, 300)); JScrollPane scroll = new JScrollPane(); getContentPane().add(scroll, BorderLayout.CENTER); scroll.getViewport().add(new Imagepanel()); } private class Imagepanel extends JPanel implements MouseListener,MouseMotionListener { public static final long serialVersionUID = 0; private int border = 2; boolean selected= false; boolean selected1= false; /*Iterator for drawing the resource tree(for the tag<res_mseg>)*/ private Iterator<DataObject> it; /*Iteartor for the work order attribute in the tag <rev_mseg>*/ private Iterator<DrawingData> it_wrk_drawing; /*Iterator for the attribute wo_from_date in the tag <rev_mseg>*/ private Iterator<FromDateData> it_frmdate; /*Iterator for the attribute wo_to_date in the tag <rev_mseg>*/ private Iterator<ToDateData> it_todate; protected ArrayList<DataObject> res_mseg; protected ArrayList<DrawingData> rev_list; protected ArrayList<FromDateData> frm_list; protected ArrayList<WrkorderData> wrk ; ArrayList<ToDateData> todate_list; ArrayList<FromTime>fromtime_list; ArrayList<ToTime> totime_list; ArrayList<FromDateData> frmdate_list = new ArrayList<FromDateData>(); boolean drawpanel = true; boolean drawresource = true; boolean drawcalender = true; boolean canDrag = false; public Date date1; ArrayList str_wrk = new ArrayList(); ArrayList <Date>str_frmdate ; ArrayList <Date>str_todate = new ArrayList<Date>(); ArrayList <Date>str_frmtime = new ArrayList<Date>(); ArrayList <Date>str_totime = new ArrayList<Date>(); ArrayList resno_list = new ArrayList (); int noday; int no_of_days; Date fromDate; Graphics g; MouseEvent e; int rect_x; int rect_y; int rect_w; int rect_ht; int x,y,w,ht; //Rectangle r = new Rectangle(); int r_x=100; int r_y =100; int r_w =15; int r_ht = 10; Rectangle r1 = new Rectangle(r_x,r_y,r_w,r_ht); ArrayList<Integer> x_cord_list = new ArrayList<Integer>(); ArrayList<Integer> y_cord_list = new ArrayList<Integer>(); ArrayList<Integer> width_list = new ArrayList<Integer> (); ArrayList<Rectangle> rectangles = new ArrayList<Rectangle> (); ArrayList<Rectangle> rectangle = new ArrayList<Rectangle> (); Rectangle clickedRectangle = null; Rectangle r = new Rectangle(rect_x,rect_y,rect_w,rect_ht); Rectangle [] panel; int x_Pos,y_Pos; Rectangle selectedRect; Point offset = new Point(); boolean dragging = false; Rectangle[] rects; public Imagepanel() { panel(); addMouseMotionListener(this); addMouseListener(this); } public void panel() { Graphics g1 = this.g; int dimension =0 ; setPreferredSize(new Dimension(33060,33060)); Imagepanel imagepanel ; setBorder(BorderFactory.createLineBorder(Color.BLACK, border)); } @Override public void paint(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setColor(getBackground()); g2.fillRect(0, 0, getWidth(), getHeight()); g2.setColor(getForeground()); if(drawpanel){drawPanel(g2);} if(drawresource){drawResource(g2);} if(drawcalender) { drawCalendar(g2); } g2.setPaint(new Color(0,157,250)); System.out.println(rectangles.size()+"SIZE"); rects = rectangles.toArray(new Rectangle[rectangles.size()]); for(Rectangle ret : rects) { g2.draw(ret); } } public void drawPanel(Graphics g){ int margin = 10; int imgborder = 10; int imgline1 = 300; int titlebarx = 300; int titlebary = 50; Graphics2D g2 = (Graphics2D) g; g2.drawLine(imgline1, 0, imgline1, getHeight()); g2.setPaint(new Color(0, 128, 192)); g2.fillRect(imgborder, imgborder, 290, 45); g2.setPaint(Color.WHITE); g2.drawLine(margin, margin, titlebarx, titlebary); g2.drawString("RESOURCE", 40, 40); g2.drawString("TIME", 200, 25); } public void drawResource(Graphics g) { int x_axis= 50; int y_axis = 80; Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); XMLReading xmlreadingObj = new XMLReading(); try { xmlreadingObj.fileParsing("C:/Documents and Settings/8563/TEST/src/Gantt.xml"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (SAXException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } it = xmlreadingObj.getres_list().iterator(); it_wrk_drawing = xmlreadingObj.getrev__wrkOrder_list().iterator(); it_frmdate = xmlreadingObj.getrev_frmdate_list().iterator(); it_todate = xmlreadingObj.getrev_todate_list().iterator(); int size = xmlreadingObj.getres_list().size(); g2.setPaint(new Color(0, 128, 192)); int y_rect = 72; int tree_rect_width = 13; int tree_rect_height = 10; int panel_line_x = 300; for (int len=0;len<size;len++) { while(it.hasNext()) { Font font_resourcetrees = new Font("Ariel",Font.PLAIN,9); g2.setFont(font_resourcetrees); g2.fillRect(30,y_rect,tree_rect_width,tree_rect_height); g2.drawLine(panel_line_x,y_axis+14,getWidth(),y_axis+14); g2.drawString(""+it.next(),x_axis,y_axis); /*line for seperating the resource*/ y_axis += 28; y_rect += 28; } } } /*This method is for drawing the calender in the right top of the panel*/ public Date drawCalendar( Graphics g) { Graphics2D g2 = (Graphics2D)g; int no_ofday = this.no_of_days; long from; long to; int n = 168; int x = 300; int x1_cal = 300; int x3 = x; int width1 = 180; int daydx = (width1 / 2); int dayx = (daydx + x); int f2 = x + n; String date[] = {"0", "4", "8", "12", "16", "20", "24"}; int total_no_days = date_difference(g2); long days =(long) total_no_days; no_ofday = (int) days ; Date [] arraydate = drawwrk(g2); g2.setPaint(Color.BLACK); Calendar c = Calendar.getInstance(); // current date* Calendar c1 = Calendar.getInstance(); Date local_fromDate = arraydate[0]; Date toDate = arraydate[arraydate.length-1]; c.setTime(local_fromDate); for (int i = 0; i < no_ofday; i++) { SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd"); SimpleDateFormat sdf1 = new SimpleDateFormat("EEE"); // use the pattern: day_of_month String str = sdf.format(c.getTime()); // fromat the date to string String str3 = sdf1.format(c1.getTime()); for (int j = 0; j < 6; j++) { Font font1 = new Font("Ariel", Font.PLAIN, 9); g2.setFont(font1); g2.drawLine(x1_cal, 50, x1_cal, 55); g2.drawString(date[j], x1_cal, 45); x1_cal += 30; } g2.drawRect(x, 10, width1, 45); g2.drawString("24", f2, 45); f2 += width1; x += width1; g2.drawString(str, dayx-10, 35); g2.drawString(str3,dayx-5,20); dayx += 180; int nextdate = c.get(Calendar.DATE) + 1; c.set(Calendar.DATE, nextdate); int nextdate1 = c1.get(Calendar.DAY_OF_WEEK)+1; c1.set(Calendar.DAY_OF_WEEK,nextdate1); } return local_fromDate; } /*This method make the datas in the list in a order. It computes the date and time from the list.. And it return the start date and end date & this was used in calender method*/ public Date [] drawwrk(Graphics g) { Graphics2D g2 = (Graphics2D)g; XMLReading xmlreadingObj = new XMLReading(); try { xmlreadingObj.fileParsing("C:/Documents and Settings/8563/TEST/src/Gantt.xml"); } catch (FileNotFoundException ex) { ex.printStackTrace(); } catch (SAXException ex) { ex.printStackTrace(); } catch (IOException ex) { ex.printStackTrace(); } res_mseg = xmlreadingObj.res_list; Object [] array = res_mseg.toArray(); rev_list = xmlreadingObj.rev__wrkOrder_list; Object [] array1 = rev_list.toArray(); String str1 = new String(); String str2 = new String(); wrk = xmlreadingObj.wrkorder_list; frm_list = xmlreadingObj.rev_frmdate_list; todate_list = xmlreadingObj.rev_todate_list; fromtime_list = xmlreadingObj.rev_frmtime_list; totime_list = xmlreadingObj.rev_totime_list; Date frmdate = new Date(); Date todate = new Date(); Date frm_time = null; Date to_time = null; Time frmTime = null; Time toTime = null; Date startdate= null; SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat sdf_time = new SimpleDateFormat("HH:mm:ss"); int delay =(1000*60*60*24); Calendar c2 = Calendar.getInstance(); Calendar c3 = Calendar.getInstance(); int ftime = 0; int ttime = 0; int dif = 0; int x_1 = 0; int fromdate =0; int to_date =0; int size = res_mseg.size(); int size1 = rev_list.size(); ArrayList res_list = new ArrayList(); String strtdate = frm_list.get(0).toString(); String [] str_array = new String[size]; String [] str1_array = new String[size1]; double x_time; double y_time; double x1 ; double x2; int y_1 =80; int y_axis = 74; int x_cord = this.rect_x; int width = this.rect_w; str_frmdate = new ArrayList<Date>(); for(int len = 0; len < array.length; len++) { int i =0; int j =0; str1 = array[len].toString(); str_array [i] = str1; res_list.add(str1); for(int len1 = 0; len1 < array1.length; len1++) { str2 = array1[len1].toString(); str1_array [j] = str2; if(str1.compareToIgnoreCase(str2) == 0 ) { resno_list.add(str2); str_wrk.add(wrk.get(len1)); String str_date = frm_list.get(len1).toString(); String date_todate = todate_list.get(len1).toString(); String totime = totime_list.get(len1).toString(); String frmtime = fromtime_list.get(len1).toString(); try { frmdate = sdf.parse(str_date); todate = sdf.parse(date_todate); frm_time = sdf_time.parse(frmtime); to_time = sdf_time.parse(totime); } catch (ParseException ex) { ex.printStackTrace(); } c2.setTime(frm_time); c3.setTime(to_time); ftime = c2.get(Calendar.HOUR_OF_DAY); ttime = c3.get(Calendar.HOUR_OF_DAY); int time_diff = ttime - ftime; fromdate = Math.round((frmdate.getTime())/delay); to_date = Math.round((todate.getTime())/delay); str_frmtime.add(frm_time); str_totime.add(to_time); str_frmdate.add(frmdate); str_todate.add(todate); long datediff =((( todate.getTime())/delay)-((difference().getTime())/delay)); long convr_datediff =(datediff*180)+300; /*calculating x-co_ordinate*/ double hour = ftime*7.5; double x_hr = hour +convr_datediff; int conversion = (int)Math.round(x_hr); x_cord = conversion; /*calculating width*/ double x_2 = ttime *7.5; double timediff = x_2 - hour; int width_hr =(int) Math.round(timediff); width =width_hr; //int width =width_hr; try { x_cord_list.add(x_cord); y_cord_list.add(y_axis); width_list.add(width); rectangles.add(new Rectangle(x_cord,y_axis,width,15)); } catch(Exception e2) { System.out.print("EXCEPTION"); } } } y_axis += 28; } Date [] sorted_frmDate = (Date [])str_frmdate.toArray(new Date[str_frmdate.size()]); Date [] sorted_toDate = (Date [])str_todate.toArray(new Date[str_todate.size()]); java.util.Arrays.sort(sorted_frmDate); java.util.Arrays.sort(sorted_toDate); startdate = sorted_frmDate[0]; Date enddate = sorted_toDate[sorted_toDate.length-1]; return new Date [] {startdate,enddate}; } public int date_difference(Graphics g) { int noofdays= this.no_of_days; Date [] date = null; Graphics2D g2 = (Graphics2D)g; long difference = 0; try { date = drawwrk(g2); } catch(NullPointerException nullptr_e) { } Date startDate = date[0]; Date endDate = date[date.length-1]; Calendar c = Calendar.getInstance(); Calendar c1 = Calendar.getInstance(); c.setTime(startDate); c1.setTime(endDate); difference = (c1.getTime().getTime() - c.getTime().getTime()); long total_no_days = Math.round((difference)/(1000*60*60*24)); no_of_days = (int)total_no_days+1; return this.no_of_days; } public Date difference() { Date dt = str_frmdate.get(0); return dt; } public ArrayList getfrmDate() { return str_frmdate; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { Point p = e.getPoint(); for(int j = 0; j < rects.length; j++) { if(rects[j].contains(p)) { selectedRect = rects[j]; offset.x = p.x - rects[j].x; offset.y = p.y - rects[j].y; dragging = true; break; } } } public void mouseReleased(MouseEvent e) { rectangles.clear(); dragging = false; } public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseDragged(MouseEvent e) { Point me_p = e.getPoint(); if(dragging == true) { for(int x = 0, y = rects.length; x < y; x++) { selectedRect.setLocation(me_p.x - offset.x,me_p.y - offset.y); this.repaint(); } } } public void actionPerformed(MouseEvent e) {} public void mouseMoved(MouseEvent e) {} } }
Reply With Quote