dcsimg
Enhancing jpanel1 so acts like Cordinate Demo
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   deniz_tezer
Posted On:   Friday, April 22, 2005 09:39 AM

I need help again mainly ,I am trying enhance jPanel1 so it behaves as CoordinatesDemo(same functionality ) firstly , I try to draw grid on jPanel1 so I add additional codes to below source code but it did not work ... if you show me how draw grids on jPanel1 and then how make it as CoordinatesDemo (I apologize of my bad english and poor java knowledge ....I hope I told my problem properly) I ll be very glad . I am giving the source codes of Sentinel (contains jPanel1 ) and the CoordinatesDemo below . My Best wishes Deniz Tezer /****************************************************************/ /* Sentinel */ /* */ /*   More>>

I need help again

mainly ,I am trying enhance jPanel1 so it behaves as CoordinatesDemo(same functionality )

firstly , I try to draw grid on jPanel1 so I add additional codes to below source code but it did not work ...

if you show me how draw grids on jPanel1 and then how make it as CoordinatesDemo (I apologize of my bad english and poor java knowledge ....I hope I told my problem properly) I ll be very glad . I am giving the source codes of Sentinel (contains jPanel1 ) and the CoordinatesDemo below .

My Best wishes

Deniz Tezer



			



/****************************************************************/
/* Sentinel */
/* */
/****************************************************************/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/**
* Summary description for Sentinel
*
*/
public class Sentinel extends JFrame
{
// Variables declaration
private JSeparator jSeparator1;
private JButton jButton1;
private JButton jButton2;
private JButton jButton3;
private JPanel contentPane;

private JPanel jPanel1;


//ADDTONAL CODE
Graphics g;
//-----


// End of variables declaration


public Sentinel()
{
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()
{
jSeparator1 = new JSeparator();
jButton1 = new JButton();
jButton2 = new JButton();
jButton3 = new JButton();
contentPane = (JPanel)this.getContentPane();
//-----
jPanel1 = new JPanel();
//-----

//
// jSeparator1
//
//
// jButton1
//
jButton1.setText("jButton1");
jButton1.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton1_actionPerformed(e);
}

});
//
// jButton2
//
jButton2.setBackground(new Color(242, 29, 29));
jButton2.setIcon(new ImageIcon("C:\Documents and Settings\DEN\Desktop\project\cube_molecule_preferences.JPG"));
jButton2.setToolTipText("PROJECT");
jButton2.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton2_actionPerformed(e);
}

});
//
// jButton3
//
jButton3.setBackground(new Color(22, 112, 235));
jButton3.setIcon(new ImageIcon("C:\Documents and Settings\DEN\Desktop\project\navigate_plus.JPG"));
jButton3.setToolTipText("Help");
jButton3.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent e)
{
jButton3_actionPerformed(e);
}

});
//
// contentPane
//
contentPane.setLayout(null);
contentPane.setBorder(BorderFactory.createEtchedBorder());
contentPane.setBackground(new Color(183, 201, 197));
addComponent(contentPane, jSeparator1, 19,83,741,19);
addComponent(contentPane, jButton1, 33,28,83,24);
addComponent(contentPane, jButton2, 370,18,60,58);
addComponent(contentPane, jButton3, 692,13,60,57);
addComponent(contentPane, jPanel1, 24,105,743,478);
//
// jPanel1
//
jPanel1.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
jPanel1.setBorder(BorderFactory.createRaisedBevelBorder());
jPanel1.setBackground(new Color(243, 232, 249));



//ADDITIONAL CODE

g=jPanel1.getGraphics();
g.fillRect(0, 0, jPanel1.getWidth(), jPanel1.getHeight());
Insets insets = jPanel1.getInsets();
int gridSpace=20;
int firstX = insets.left;
int firstY = insets.top;
int lastX = jPanel1.getWidth()-2*insets.right;
int lastY = jPanel1.getHeight() - 2* insets.bottom; //Draw vertical lines.
int x = firstX;

while (x < lastX) { g.drawLine(x, firstY, x, lastY);

x += gridSpace; } //Draw horizontal lines.

int y = firstY;
while (y < lastY) { g.drawLine(firstX, y, lastX, y); y += gridSpace; }

//END OF ADDITIONAL CODE




//
// Sentinel
//
this.setTitle("Sentinel - extends JFrame");
this.setLocation(new Point(0, -3));
this.setSize(new Dimension(841, 634));
}

/** 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 jButton1_actionPerformed(ActionEvent e)
{
System.out.println("
jButton1_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jButton2_actionPerformed(ActionEvent e)
{
System.out.println("
jButton2_actionPerformed(ActionEvent e) called.");
// TODO: Add any handling code here

}

private void jButton3_actionPerformed(ActionEvent e)
{
System.out.println("
jButton3_actionPerformed(ActionEvent 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 Sentinel();
}
//= End of Testing =


}




// CoordinatesDemo



/* CoordinatesDemo.java is a 1.4 application that requires no other files.
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import javax.swing.event.MouseInputListener;
/* * This displays a framed area. As the user moves the cursor * over the area,
a label displays the cursor's location. When * the user clicks, the area displays a 7x7 dot at the click * location. */
public class CoordinatesDemo {
private JLabel label;
private Point clickPoint, cursorPoint;
private void buildUI(Container container) {
container.setLayout(new BoxLayout(container, BoxLayout.PAGE_AXIS));
CoordinateArea coordinateArea = new CoordinateArea(this); container.add(coordinateArea);
label = new JLabel();
resetLabel();
container.add(label); //Align the left edges of the components.
coordinateArea.setAlignmentX(Component.LEFT_ALIGNMENT); label.setAlignmentX(Component.LEFT_ALIGNMENT); //redundant
}
public void updateCursorLocation(int x, int y) {
if (x < 0 || y < 0) { cursorPoint = null; updateLabel();
return;
}
if (cursorPoint == null) {
cursorPoint = new Point();
}
cursorPoint.x = x;
cursorPoint.y = y;
updateLabel();
}
public void updateClickPoint(Point p) {
clickPoint = p;
updateLabel();
}
public void resetLabel() {
cursorPoint = null;
updateLabel();
}
protected void updateLabel() {
String text = "";
if ((clickPoint == null) && (cursorPoint == null)) {
text = "Click or move the cursor within the framed area.";
}
else {
if (clickPoint != null) {
text += "The last click was at (" + clickPoint.x + ", " + clickPoint.y + "). ";
}
if (cursorPoint != null) {
text += "The cursor is at (" + cursorPoint.x + ", " + cursorPoint.y + "). ";
}
}
label.setText(text);
}
/** * Create the GUI and show it. For thread safety, * this method should be invoked from the * event-dispatching thread. */
private static void createAndShowGUI() { //Make sure we have nice window decorations.

JFrame.setDefaultLookAndFeelDecorated(true); //Create and set up the window.
JFrame frame = new JFrame("CoordinatesDemo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Set up the content pane.
CoordinatesDemo controller = new CoordinatesDemo();
controller.buildUI(frame.getContentPane()); //Display the window.
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(
new Runnable() {
public void run() {
createAndShowGUI();
}
}
);
}
public static class CoordinateArea extends JComponent implements MouseInputListener {
Point point = null;
CoordinatesDemo controller;
Dimension preferredSize = new Dimension(400,400);
Color gridColor;
public CoordinateArea(CoordinatesDemo controller) {
this.controller = controller; //Add a border of 5 pixels at the left and bottom, //and 1 pixel at the top and right.
setBorder(BorderFactory.createMatteBorder(1,5,5,1, Color.RED));
addMouseListener(this); addMouseMotionListener(this); setBackground(Color.WHITE);
setOpaque(true);
}
public Dimension getPreferredSize() {
return preferredSize;
}
protected void paintComponent(Graphics g) {
//Paint background if we're opaque.
if (isOpaque()) {
g.setColor(getBackground());
g.fillRect(0, 0, getWidth(), getHeight()); } //Paint 20x20 grid.
g.setColor(Color.blue);
drawGrid(g, 40); //If user has chosen a point, paint a small dot on top.
if (point != null) { g.setColor(getForeground());
g.fillRect(point.x - 3, point.y - 3, 7, 7); }
} //Draws a 20x20 grid using the current color.
private void drawGrid(Graphics g, int gridSpace) {
Insets insets = getInsets();
int firstX = insets.left;
int firstY = insets.top;
int lastX = getWidth()-2*insets.right;
int lastY = getHeight() - 2* insets.bottom; //Draw vertical lines.
int x = firstX;
while (x < lastX) { g.drawLine(x, firstY, x, lastY);
x += gridSpace; } //Draw horizontal lines.
int y = firstY;
while (y < lastY) { g.drawLine(firstX, y, lastX, y); y += gridSpace; }
} //Methods required by the MouseInputListener interface.
public void mouseClicked(MouseEvent e) {
int x = e.getX();
int y = e.getY();
if (point == null) { point = new Point(x, y);
}
else { point.x = x; point.y = y; }
controller.updateClickPoint(point);
repaint(); }
public void mouseMoved(MouseEvent e) {
controller.updateCursorLocation(e.getX(), e.getY()); }
public void mouseExited(MouseEvent e) { controller.resetLabel(); }
public void mouseReleased(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mousePressed(MouseEvent e) { }
public void mouseDragged(MouseEvent e) { } } }

   <<Less
About | Sitemap | Contact