// draw.java make draw.class then java draw // /gui/proj5/draw converted to Java (work in progress) import java.awt.*; import java.awt.event.*; import java.text.*; import java.io.*; import java.util.*; import javax.swing.*; import javax.swing.filechooser.*; import java.awt.image.*; import javax.imageio.*; public class draw extends JFrame { int fwidth=1000; // drawing frame width int fheight=800; // drawing frame height int xmid = fwidth/2; // center of user draw area at scale = 1.0 int ymid = fheight/2; // center of user draw area at scale = 1.0 String filename = new String("draw.draw"); JMenu nameMenu; // variables "local" to draw (was draw_gui.h) int startX, startY, lastX, lastY, midX, midY, dr, dx, dy; boolean tracking = false; boolean filled_flag = false; int fillback = Names.LINECOLOR; // FILLCOLOR, BACKCOLOR, LINECOLOR int action = Names.LINE; // SELECT, LINE, ELLIPSE, ... int obj_kind = 0; // int bound_xmin, bound_ymin, bound_xmax, bound_ymax; int now_button = 0; int xoff_at = 0; int yoff_at = 0; double scale = 1.0; int xg0, yg0, xg1, yg1, xgr; // xg = ((x - xmid)/scale) + xoff int x0, y0, x1, y1; // x = ((xg - xoff)*scale) + xmid int x2, y2, xg2, yg2; // for 3 point symbols int width, height; // width = gwidth*scale int radius; // radius = gradius*scale int font_size = 12; int pen_size = 1; boolean snap = true; // grid stuff int grid_spacing = 20; boolean grid_on = true; int frame_xmin, frame_ymin, frame_xmax, frame_ymax, frame_auto; int sel_area_xmin, sel_area_ymin, sel_area_xmax, sel_area_ymax; Frame_data frame_read = new Frame_data(); // LinkedList display_list; LinkedList display_list = new LinkedList(); Color linecolor = Color.black; Color fillcolor = Color.green; Color backcolor = Color.white; Color LLGRAYC = new Color(210, 210, 210); JButton whiteB, blackB, redB, greenB, blueB, orangeB, yellowB, violetB, grayB, ltgrayB, llgrayB; JButton frameB, selectB, selectareaB, textB, lineB, ellipseB, circleB, rectangleB, rectanglecB, polygonB, linecolorB, fillcolorB, backcolorB, filledB, unfilledB, zoompB, zoommB, zoompctB; JButton vmuxB, hmuxB, aluB, andB, orB, xorB, driverB, compdotB, arrowB, arc3B, dotB, hresistB, vresistB, hcapaciB, vcapaciB, hinductB, vinductB; boolean debug = true; boolean first = true; JPanel buttonscol, buttons1, buttons2; JPanel otherpanel, buttons3, dp; // drawing panel inside scroll pane JScrollPane sp; public draw() { setTitle("draw"); setSize(fwidth+250,fheight+100); // can be changed by user setBackground(Color.white); setForeground(Color.black); // LinkedList display_list = new LinkedList(); frame_xmin = 0; frame_xmax = fwidth; frame_ymin = 0; frame_ymax = fheight; frame_auto = 1; frame_read.xmin = frame_xmin; frame_read.ymin = frame_ymin; frame_read.xmax = frame_xmax; frame_read.ymax = frame_ymax; frame_read.fwidth = fwidth; frame_read.fheight = fheight; frame_read.xmid = xmid; frame_read.ymid = ymid; // create menu bar and attach it to AwtMenuTest window JMenuBar bar = new JMenuBar(); setJMenuBar(bar); // set up file menu JMenu fileMenu = new JMenu("File"); bar.add(fileMenu); // add File menu to menu bar // set up File items JMenuItem clearItem = new JMenuItem("Clear"); fileMenu.add(clearItem); JMenuItem openItem = new JMenuItem("Open .draw"); fileMenu.add(openItem); JMenuItem newItem = new JMenuItem("New file name"); fileMenu.add(newItem); JMenuItem saveItem = new JMenuItem("Save .draw"); fileMenu.add(saveItem); JMenuItem savegItem = new JMenuItem("Save .png"); fileMenu.add(savegItem); JMenuItem quitItem = new JMenuItem("Quit"); fileMenu.add(quitItem); // set up edit menu JMenu editMenu = new JMenu("Edit"); bar.add(editMenu); // add Edit menu to menu bar // set up Edit items JMenuItem selectItem = new JMenuItem("Select"); editMenu.add(selectItem); JMenuItem deselectItem = new JMenuItem("Deselect all"); editMenu.add(deselectItem); JMenuItem deleteItem = new JMenuItem("Delete"); editMenu.add(deleteItem); JMenuItem cutItem = new JMenuItem("Cut"); editMenu.add(cutItem); JMenuItem copyItem = new JMenuItem("Copy"); editMenu.add(copyItem); JMenuItem pasteItem = new JMenuItem("Paste"); editMenu.add(pasteItem); // set up Arrange menu JMenu arrangeMenu = new JMenu("Arrange"); bar.add( arrangeMenu ); // add Arrange menu to menu bar // create Arrange items JMenuItem groupItem = new JMenuItem("Group"); arrangeMenu.add(groupItem); JMenuItem ungroupItem = new JMenuItem("Ungroup"); arrangeMenu.add(ungroupItem); JMenuItem movefItem = new JMenuItem("Move to Front"); arrangeMenu.add(movefItem); JMenuItem movebItem = new JMenuItem("Move to Back"); arrangeMenu.add(movebItem); // create Font submenu JMenu fontMenu = new JMenu("Font size"); bar.add( arrangeMenu ); // add Font menu to menu bar // create Font items JMenuItem cour12Item = new JMenuItem("Courier 12"); fontMenu.add(cour12Item); JMenuItem cour14Item = new JMenuItem("Courier 14"); fontMenu.add(cour12Item); JMenuItem cour18Item = new JMenuItem("Courier 18"); fontMenu.add(cour12Item); JMenuItem cour24Item = new JMenuItem("Courier 24"); fontMenu.add(cour12Item); // create Grid submenu JMenu gridMenu = new JMenu("Grid"); bar.add( gridMenu ); // add Grid menu to menu bar // create Grid items JMenuItem gridoffItem = new JMenuItem("grid off"); gridMenu.add(gridoffItem); JMenuItem gridonItem = new JMenuItem("display grid"); gridMenu.add(gridonItem); JMenuItem gridsnapItem = new JMenuItem("snap to grid"); gridMenu.add(gridsnapItem); JMenuItem gridnosnapItem = new JMenuItem("no snap"); gridMenu.add(gridnosnapItem); JMenuItem spacing5Item = new JMenuItem("spacing 5"); gridMenu.add(spacing5Item); JMenuItem spacing10Item = new JMenuItem("spacing 10"); gridMenu.add(spacing10Item); JMenuItem spacing15Item = new JMenuItem("spacing 15"); gridMenu.add(spacing15Item); JMenuItem spacing20Item = new JMenuItem("spacing 20"); gridMenu.add(spacing20Item); JMenuItem spacing30Item = new JMenuItem("spacing 30"); gridMenu.add(spacing30Item); JMenuItem spacing40Item = new JMenuItem("spacing 40"); gridMenu.add(spacing40Item); // create Pen submenu JMenu penMenu = new JMenu("Pen"); bar.add( penMenu ); // add Pen menu to menu bar // create Pen items JMenuItem pen1Item = new JMenuItem("1"); penMenu.add(pen1Item); JMenuItem pen2Item = new JMenuItem("2"); penMenu.add(pen2Item); JMenuItem pen3Item = new JMenuItem("3"); penMenu.add(pen3Item); JMenuItem pen4Item = new JMenuItem("4"); penMenu.add(pen4Item); // set up Help menu JMenu helpMenu = new JMenu("Help"); bar.add(helpMenu); // add help menu to menu bar // set up Help items JMenuItem helpItem = new JMenuItem("Basic help"); helpMenu.add(helpItem); JMenuItem aboutItem = new JMenuItem("About"); helpMenu.add(aboutItem); // set up Name menu nameMenu = new JMenu("name="+filename); bar.add(nameMenu); // add name menu to menu bar // set up layout // this.getContentPane().setLayout(new BoxLayout(this, BoxLayout.X_AXIS)); buttonscol = new JPanel(); // two columns of buttons buttonscol.setLayout(new BoxLayout(buttonscol, BoxLayout.X_AXIS)); otherpanel = new JPanel(); // color buttons and draw area otherpanel.setLayout(new BoxLayout(otherpanel, BoxLayout.Y_AXIS)); // set up column 1 buttons buttons1 = new JPanel(); buttons1.setLayout(new BoxLayout(buttons1, BoxLayout.Y_AXIS)); frameB = new JButton("frame"); buttons1.add(frameB); selectB = new JButton("select"); buttons1.add(selectB); selectareaB = new JButton("select area"); buttons1.add(selectareaB); textB = new JButton("text"); buttons1.add(textB); lineB = new JButton("line"); buttons1.add(lineB); ellipseB = new JButton("ellipse"); buttons1.add(ellipseB); circleB = new JButton("circle"); buttons1.add(circleB); rectangleB = new JButton("rectangle"); buttons1.add(rectangleB); rectanglecB = new JButton("rectanglec"); buttons1.add(rectanglecB); polygonB = new JButton("polygon"); buttons1.add(polygonB); linecolorB = new JButton("line color"); linecolorB.setBackground(linecolor); linecolorB.setForeground(Color.WHITE); buttons1.add(linecolorB); fillcolorB = new JButton("fill color"); fillcolorB.setBackground(fillcolor); buttons1.add(fillcolorB); backcolorB = new JButton("back color"); backcolorB.setBackground(backcolor); buttons1.add(backcolorB); filledB = new JButton("filled"); buttons1.add(filledB); unfilledB = new JButton("unfilled"); buttons1.add(unfilledB); zoompB = new JButton("zoom +"); buttons1.add(zoompB); zoommB = new JButton("zoom -"); buttons1.add(zoommB); zoompctB = new JButton("zoom 0%"); buttons1.add(zoompctB); buttonscol.add(buttons1); // set up column 2 buttons buttons2 = new JPanel(); buttons2.setLayout(new BoxLayout(buttons2, BoxLayout.Y_AXIS)); vmuxB = new JButton("V mux"); buttons2.add(vmuxB); hmuxB = new JButton("H mux"); buttons2.add(hmuxB); aluB = new JButton("alu"); buttons2.add(aluB); andB = new JButton("and"); buttons2.add(andB); orB = new JButton("or"); buttons2.add(orB); xorB = new JButton("xor"); buttons2.add(xorB); driverB = new JButton("driver"); buttons2.add(driverB); compdotB = new JButton("comp dot"); buttons2.add(compdotB); arrowB = new JButton("arrow <"); buttons2.add(arrowB); arc3B = new JButton("3pt arc"); buttons2.add(arc3B); dotB = new JButton("dot"); buttons2.add(dotB); hresistB = new JButton("H resists"); buttons2.add(hresistB); vresistB = new JButton("V resists"); buttons2.add(vresistB); hcapaciB = new JButton("H capaci"); buttons2.add(hcapaciB); vcapaciB = new JButton("V capaci"); buttons2.add(vcapaciB); hinductB = new JButton("H induct"); buttons2.add(hinductB); vinductB = new JButton("V induct"); buttons2.add(vinductB); buttonscol.add(buttons2); this.getContentPane().add(buttonscol, BorderLayout.WEST); // set up row 3 buttons buttons3 = new JPanel(); buttons3.setLayout(new BoxLayout(buttons3, BoxLayout.X_AXIS)); whiteB = new JButton("white"); buttons3.add(whiteB); blackB = new JButton("black"); blackB.setBackground(Color.BLACK); blackB.setForeground(Color.WHITE); buttons3.add(blackB); redB = new JButton("red"); redB.setBackground(Color.RED); buttons3.add(redB); greenB = new JButton("green"); greenB.setBackground(Color.GREEN); buttons3.add(greenB); blueB = new JButton("blue"); blueB.setBackground(Color.BLUE); buttons3.add(blueB); orangeB = new JButton("orange"); orangeB.setBackground(Color.ORANGE); buttons3.add(orangeB); violetB = new JButton("violet"); violetB.setBackground(Color.magenta); buttons3.add(violetB); yellowB = new JButton("yellow"); yellowB.setBackground(Color.YELLOW); buttons3.add(yellowB); grayB = new JButton("gray"); grayB.setBackground(Color.GRAY); buttons3.add(grayB); ltgrayB = new JButton("lt gray"); ltgrayB.setBackground(Color.lightGray); buttons3.add(ltgrayB); llgrayB = new JButton("ll gray"); llgrayB.setBackground(LLGRAYC); buttons3.add(llgrayB); otherpanel.add(buttons3); // set up drawing area dp = new MyDrawingPane(); dp.setBackground(Color.white); dp.addMouseListener(new mousePressHandler()); dp.addMouseListener(new mouseReleaseHandler()); dp.addMouseMotionListener(new mouseMotionHandler()); // put the drawing area in a scroll pane sp = new JScrollPane(dp); sp.setPreferredSize(new Dimension(1000,800)); sp.setHorizontalScrollBarPolicy( ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS); sp.setVerticalScrollBarPolicy( ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS); otherpanel.add(sp); this.getContentPane().add(otherpanel); // set up Button 1 callbacks, Listeners frameB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doframeB();}}); selectB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doselectB();}}); selectareaB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doselectareaB();}}); textB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dotextB();}}); lineB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dolineB();}}); ellipseB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doellipseB();}}); circleB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docircleB();}}); rectangleB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dorectangleB();}}); rectanglecB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dorectanglecB();}}); polygonB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopolygonB();}}); linecolorB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dolinecolorB();}}); fillcolorB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dofillcolorB();}}); backcolorB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dobackcolorB();}}); filledB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dofilledB();}}); unfilledB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dounfilledB();}}); zoompB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dozoompB();}}); zoommB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dozoommB();}}); // set up Button 2 callbacks, Listeners vmuxB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dovmuxB();}}); hmuxB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dohmuxB();}}); aluB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doaluB();}}); andB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doandB();}}); orB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doorB();}}); xorB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doxorB();}}); driverB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dodriverB();}}); compdotB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docompdotB();}}); arrowB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doarrowB();}}); arc3B.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doarc3B();}}); dotB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dodotB();}}); hresistB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dohresistB();}}); vresistB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dovresistB();}}); hcapaciB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dohcapaciB();}}); vcapaciB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dovcapaciB();}}); hinductB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dohinductB();}}); vinductB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dovinductB();}}); whiteB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dowhiteB();}}); blackB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doblackB();}}); redB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doredB();}}); greenB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogreenB();}}); blueB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doblueB();}}); orangeB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doorangeB();}}); violetB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dovioletB();}}); yellowB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doyellowB();}}); grayB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dograyB();}}); ltgrayB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doltgrayB();}}); llgrayB.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dollgrayB();}}); // set up Menu callbacks, Listeners clearItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doclearItem();}}); openItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doopenItem();}}); newItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { String name = JOptionPane.showInputDialog("enter base file name"); if(name==null) { System.out.println("enter base file name cancelled"); } else { System.out.println("Name is "+name); filename = name+".draw"; System.out.println("png name="+filename.substring( 0,filename.length()-4)+"png"); nameMenu.setText("name="+filename); } }}); saveItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dosaveItem();}}); savegItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dosavegItem();}}); quitItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { System.exit(0);}}); selectItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doselectItem();}}); deselectItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dodeselectItem();}}); deleteItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dodeleteItem();}}); cutItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docutItem();}}); copyItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docopyItem();}}); pasteItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopasteItem();}}); groupItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogroupItem();}}); ungroupItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { doungroupItem();}}); movefItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { domovefItem();}}); movebItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { domovebItem();}}); cour12Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docour12Item();}}); cour14Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docour14Item();}}); cour18Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docour18Item();}}); cour24Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { docour24Item();}}); gridoffItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogridoffItem();}}); gridonItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogridonItem();}}); gridsnapItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogridsnapItem();}}); gridnosnapItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dogridnosnapItem();}}); spacing5Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing5Item();}}); spacing10Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing10Item();}}); spacing15Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing15Item();}}); spacing20Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing20Item();}}); spacing30Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing30Item();}}); spacing40Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dospacing40Item();}}); pen1Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopen1Item();}}); pen2Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopen2Item();}}); pen3Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopen3Item();}}); pen4Item.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { dopen4Item();}}); helpItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { displayHelp("DrawHelp.txt");}}); aboutItem.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent event) { displayHelp("DrawAbout.txt");}}); addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }); setVisible(true); } // end constructor draw class mousePressHandler extends MouseAdapter { public void mousePressed (MouseEvent e) { int xp, yp, xg, yg, b; // fwidth = dp.getSize().width; // fheight = dp.getSize().height; System.out.println("size fwidth="+fwidth+", fheight="+fheight); xmid = fwidth/2; ymid = fheight/2; xp = e.getX(); yp = e.getY(); b = e.getButton(); now_button = b; if(grid_on) { xg = (int)((xp-xmid)/scale+xoff_at); // bring to centered yg = (int)((yp-ymid)/scale+yoff_at); if(xg>0) xg = ((xg+grid_spacing/2)/grid_spacing)*grid_spacing; if(xg<0) xg = ((xg-grid_spacing/2)/grid_spacing)*grid_spacing; if(yg>0) yg = ((yg+grid_spacing/2)/grid_spacing)*grid_spacing; if(yg<0) yg = ((yg-grid_spacing/2)/grid_spacing)*grid_spacing; xp = (int)((xg-xoff_at)*scale+xmid); yp = (int)((yg-yoff_at)*scale+ymid); //System.out.println("xp_grid="+xp+", yp_grid="+yp); } startX = xp; startY = yp; if(b==3) // right button, move { //requestFocus(); //System.out.println("right xp="+xp+", yp="+yp+", b="+b); //debug print //dp.repaint(); tracking = true; // global startX, startY } else // left button { //requestFocus(); //System.out.println("left xp="+xp+", yp="+yp+", b="+b); //debug print //dp.repaint(); tracking = true; // global startX, startY } } // end mousePressed } // end class mousePressHandler class mouseReleaseHandler extends MouseAdapter { public void mouseReleased (MouseEvent e) { int xp, yp, xg, yg, b; // fwidth = dp.getSize().width; // fheight = dp.getSize().height; //System.out.println("fwidth="+fwidth+", fheight="+fheight); xmid = fwidth/2; ymid = fheight/2; xp = e.getX(); yp = e.getY(); b = e.getButton(); if(grid_on) { xg = (int)((xp-xmid)/scale+xoff_at); // bring to centered yg = (int)((yp-ymid)/scale+yoff_at); if(xg>0) xg = ((xg+grid_spacing/2)/grid_spacing)*grid_spacing; if(xg<0) xg = ((xg-grid_spacing/2)/grid_spacing)*grid_spacing; if(yg>0) yg = ((yg+grid_spacing/2)/grid_spacing)*grid_spacing; if(yg<0) yg = ((yg-grid_spacing/2)/grid_spacing)*grid_spacing; xp = (int)((xg-xoff_at)*scale+xmid); yp = (int)((yg-yoff_at)*scale+ymid); System.out.println("xp_grid="+xp+", yp_grid="+yp); } lastX = xp; lastY = yp; if(b==3) // right button { //requestFocus(); //System.out.println("right up xp="+xp+", yp="+yp+", b="+b); // debug //dp.repaint(); tracking = false; stopMotion(); // now_button global now_button = 0; } else // left button { //requestFocus(); //System.out.println("left up xp="+xp+", yp="+yp+", b="+b); // debug //dp.repaint(); tracking = false; stopMotion(); // now_button global now_button = 0; } } // end mouseReleased } // end class mouseReleaseHandler class mouseMotionHandler extends MouseMotionAdapter { public void mouseDragged (MouseEvent e) { int xp, yp, xg, yg; // fwidth = dp.getSize().width; // fheight = dp.getSize().height; //System.out.println("fwidth="+fwidth+", fheight="+fheight); xmid = fwidth/2; ymid = fheight/2; xp = e.getX(); yp = e.getY(); if(grid_on) { xg = (int)((xp-xmid)/scale+xoff_at); // bring to centered yg = (int)((yp-ymid)/scale+yoff_at); if(xg>0) xg = ((xg+grid_spacing/2)/grid_spacing)*grid_spacing; if(xg<0) xg = ((xg-grid_spacing/2)/grid_spacing)*grid_spacing; if(yg>0) yg = ((yg+grid_spacing/2)/grid_spacing)*grid_spacing; if(yg<0) yg = ((yg-grid_spacing/2)/grid_spacing)*grid_spacing; xp = (int)((xg-xoff_at)*scale+xmid); yp = (int)((yg-yoff_at)*scale+ymid); //System.out.println("xp_grid="+xp+", yp_grid="+yp); } if(tracking && (lastX != xp || lastY != yp)) { lastX = xp; lastY = yp; requestFocus(); dp.repaint(); } } // end mouseMotion } // end class mouseMotionHandler class MyDrawingPane extends JPanel { MyDrawingPane(){} // default, just to have "paint" dependent public void paintComponent(Graphics g) { g.clearRect(0, 0, 1000, 1000); // cover non-erase bug g.setColor(Color.black); if(first) { frameB.setSelected(false); first = false; } doselected(); if(grid_on) { g.setColor(Color.black); for(int i=0; i