|
|
Welcome to the Java Forums.
You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:
- have access to post topics
- communicate privately with other members (PM)
- not see advertisements between posts
- have the possibility to earn one of our surprises if you are an active member
- access many other special features that will be introduced later.
Registration is fast, simple and absolutely free so please, join our community today!
If you have any problems with the registration process or your account login, please contact us.
|
|

03-26-2008, 09:41 PM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 32
|
|
|
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.
/****************************************************************/
/* 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, 05:24 AM
|
|
Senior Member
|
|
Join Date: Jul 2007
Posts: 1,141
|
|
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, 11:47 AM
|
|
Member
|
|
Join Date: Mar 2008
Posts: 32
|
|
|
Thanks a lot dude I would never get this in 10 000 years.
Last edited by BHCluster : 03-27-2008 at 12:20 PM.
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|