Results 1 to 3 of 3
Thread: X&Y Coordinate Drawing on jPanel
- 03-26-2008, 08:41 PM #1
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
X&Y Coordinate Drawing on jPanel
Hi Everybody
I have a little problem what I am trying to do is enter the X coordinates into one jTextField and Y coordinate into another jTextfield and when I press GO! jButton it displays it on the jPanel, and then next depending on the location and those two points need to be joined bu a line; for jPanel we can assume it is 250 by 200.
I know how to do GUI and the code is bellow but not this stuff any help would be greatly appreciated.
Java Code:/****************************************************************/ /* XY */ /* */ /****************************************************************/ import java.awt.*; import java.awt.event.*; import javax.swing.*; /** * Summary description for XY * */ public class XY extends JFrame { // Variables declaration private JLabel jLabel1; private JLabel jLabel2; private JTextField jTextField1; private JTextField jTextField2; private JButton jButton1; private JPanel contentPane; //----- private JPanel jPanel1; //----- // End of variables declaration public XY() { super(); initializeComponent(); // // TODO: Add any constructor code after initializeComponent call // this.setVisible(true); } /** * This method is called from within the constructor to initialize the form. * WARNING: Do NOT modify this code. The content of this method is always regenerated * by the Windows Form Designer. Otherwise, retrieving design might not work properly. * Tip: If you must revise this method, please backup this GUI file for JFrameBuilder * to retrieve your design properly in future, before revising this method. */ private void initializeComponent() { jLabel1 = new JLabel(); jLabel2 = new JLabel(); jTextField1 = new JTextField(); jTextField2 = new JTextField(); jButton1 = new JButton(); contentPane = (JPanel)this.getContentPane(); //----- jPanel1 = new JPanel(); //----- // // jLabel1 // jLabel1.setText("X Coordinate"); // // jLabel2 // jLabel2.setText("Y Coordinate"); // // jTextField1 // jTextField1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jTextField1_actionPerformed(e); } }); // // jTextField2 // jTextField2.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jTextField2_actionPerformed(e); } }); // // jButton1 // jButton1.setText("GO!"); jButton1.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { jButton1_actionPerformed(e); } }); jButton1.addMouseListener(new MouseAdapter() { public void mousePressed(MouseEvent e) { jButton1_mousePressed(e); } public void mouseReleased(MouseEvent e) { jButton1_mouseReleased(e); } }); // // contentPane // contentPane.setLayout(null); addComponent(contentPane, jLabel1, 292,251,82,18); addComponent(contentPane, jLabel2, 292,295,84,17); addComponent(contentPane, jTextField1, 355,250,57,22); addComponent(contentPane, jTextField2, 355,292,57,22); addComponent(contentPane, jButton1, 330,332,62,28); addComponent(contentPane, jPanel1, 16,23,266,210); // // jPanel1 // jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); // // XY // this.setTitle("XY - extends JFrame"); this.setLocation(new Point(0, 0)); this.setSize(new Dimension(700, 500)); } /** Add Component Without a Layout Manager (Absolute Positioning) */ private void addComponent(Container container,Component c,int x,int y,int width,int height) { c.setBounds(x,y,width,height); container.add(c); } // // TODO: Add any appropriate code in the following Event Handling Methods // private void jTextField1_actionPerformed(ActionEvent e) { System.out.println("\njTextField1_actionPerformed(ActionEvent e) called."); // TODO: Add any handling code here } private void jTextField2_actionPerformed(ActionEvent e) { System.out.println("\njTextField2_actionPerformed(ActionEvent e) called."); // TODO: Add any handling code here } private void jButton1_actionPerformed(ActionEvent e) { System.out.println("\njButton1_actionPerformed(ActionEvent e) called."); // TODO: Add any handling code here } private void jButton1_mousePressed(MouseEvent e) { System.out.println("\njButton1_mousePressed(MouseEvent e) called."); // TODO: Add any handling code here } private void jButton1_mouseReleased(MouseEvent e) { System.out.println("\njButton1_mouseReleased(MouseEvent e) called."); // TODO: Add any handling code here } // // TODO: Add any method code to meet your needs in the following area // //============================= Testing ================================// //= =// //= The following main method is just for testing this class you built.=// //= After testing,you may simply delete it. =// //======================================================================// public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { System.out.println("Failed loading L&F: "); System.out.println(ex); } new XY(); } //= End of Testing = }
- 03-27-2008, 04:24 AM #2
Java Code:import java.awt.*; import java.awt.event.*; import javax.swing.*; public class XYRx extends JFrame { // Variables declaration private JTextField jTextField1; private JTextField jTextField2; //----- private JPanel jPanel1; //----- int x; int y; // End of variables declaration public XYRx() { super(); initializeComponent(); // // TODO: Add any constructor code after initializeComponent call // this.setVisible(true); } private void initializeComponent() { JLabel jLabel1 = new JLabel(); JLabel jLabel2 = new JLabel(); jTextField1 = new JTextField(); jTextField2 = new JTextField(); JButton jButton1 = new JButton(); Container contentPane = (JPanel)this.getContentPane(); //----- jPanel1 = new JPanel() { protected void paintComponent(Graphics g) { super.paintComponent(g); g.setColor(Color.red); g.fillOval(x-2, y-2, 4, 4); } }; //----- // // jLabel1 // jLabel1.setText("X Coordinate"); // // jLabel2 // jLabel2.setText("Y Coordinate"); // // jTextField1 // jTextField1.addActionListener(al); // // jTextField2 // jTextField2.addActionListener(al); // // jButton1 // jButton1.setText("GO!"); jButton1.addActionListener(al); jPanel1.addMouseListener(ma); // // contentPane // contentPane.setLayout(null); addComponent(contentPane, jLabel1, 292,251,82,18); addComponent(contentPane, jLabel2, 292,295,84,17); addComponent(contentPane, jTextField1, 355,250,57,22); addComponent(contentPane, jTextField2, 355,292,57,22); addComponent(contentPane, jButton1, 330,332,62,28); addComponent(contentPane, jPanel1, 16,23,266,210); // // jPanel1 // jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5)); jPanel1.setBorder(BorderFactory.createLineBorder(Color.blue)); // // XY // this.setTitle("XY - extends JFrame"); this.setLocation(new Point(0, 0)); this.setSize(new Dimension(700, 500)); } /** Add Component Without a Layout Manager (Absolute Positioning) */ private void addComponent(Container container,Component c, int x,int y,int width,int height) { c.setBounds(x,y,width,height); container.add(c); } // // TODO: Add any appropriate code in the following Event Handling Methods // private ActionListener al = new ActionListener() { public void actionPerformed(ActionEvent e) { x = Integer.parseInt(jTextField1.getText()); y = Integer.parseInt(jTextField2.getText()); jPanel1.repaint(); } }; private MouseListener ma = new MouseAdapter() { public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); jPanel1.repaint(); } }; public static void main(String[] args) { JFrame.setDefaultLookAndFeelDecorated(true); JDialog.setDefaultLookAndFeelDecorated(true); try { UIManager.setLookAndFeel("com.sun.java" + ".swing.plaf.windows.WindowsLookAndFeel"); } catch (Exception ex) { System.out.println("Failed loading L&F: "); System.out.println(ex); } new XYRx(); } }
- 03-27-2008, 10:47 AM #3
Member
- Join Date
- Mar 2008
- Posts
- 32
- Rep Power
- 0
Similar Threads
-
drawing window
By BlitzA in forum New To JavaReplies: 1Last Post: 01-15-2009, 12:55 PM -
set coordinate to an image
By nuur in forum AWT / SwingReplies: 2Last Post: 04-01-2008, 08:08 AM -
Drawing on aJPanel
By Djangolo in forum AWT / SwingReplies: 1Last Post: 02-17-2008, 01:01 AM -
New: Want to understand Drawing...
By diRisig in forum New To JavaReplies: 1Last Post: 02-05-2008, 08:13 AM -
Drawing outside paintComponent()
By DarkSide1 in forum Java 2DReplies: 2Last Post: 11-08-2007, 10:36 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks