Help with rectangles and JTextArea
Hello. I need help with two aspects of my current program.
I insert rectangles (with Shapes) in a JPanel and link them with lines. These lines join the center of both rectangles...
How can I do to have the rectangles as Opaque, i mean, just over the lines that appear in the border, not in the center...
One more question. I have to insert notes in the panel, and i am trying to do it with JTextArea with border, but i need to move it with de mouse and i cant. how can i do?
Thaks a lot.
1) The order that you draw the objects to the screen can help you! Have a list of rectangleObjects. Draw all the lines to the center points, then after drawing lines use a drawRect followed by a fillRect.. as follows:
public void paint( Graphics g )
{
// Draw lines
for ( Object obj : rectangles )
{
g.setColor( LineColor );
rectangleObject R= (rectangleObject)obj;
R.drawLine( R.getX(), R.getY(), R.getNextX(), R.getNextY() );
}
// Draw rectangles
for ( Object obj : rectangles )
{
rectangleObject R= (rectangleObject)obj;
// Draw outline
g.setColor(RectColor );
g.drawRect( R.getX() - R.getWidth(), R.getY() - R.getHeight(), R.getWidth(), R.getHeight() );
// Fill the inside with BG color
g.setColor(BackGroundColor );
g.drawRect( R.getX() - R.getWidth() + 1, R.getY() - R.getHeight() + 1, R.getWidth() - 2, R.getHeight() - 2 );
}
}
This code only works if the background color is a constant... if not, just replace the "Fill the inside with BG color" portion with a bufferedImage that copies a texture from the background and pastes it inside the rectangle.
2) Make your own "noteOnJPanel" class... Take an input from a JTextArea / JTextField and then create a notOnJPanel object that you send the text in the field to the constructor. Then just make you noteOnJPanel class have a drag function, so you can drag it around...
Hello again and thaks a lot for your help. The problem with rectangles has been solved.
In the other hand, i dont understand what you mean with the "noteOnJPanel" class. How do i insert the text from the JTextArea to the noteOnJPanle object?
Thanks again for your atention.
Santi.
Ok... To extract text from a textfield/area you just use getText()...
// Main drawing application
...
public void createNote()
{
// create a new noteOnJPanel object with text
noteOnJPanel NOTE = new noteOnJPanel( textFieldObject.getText() );
//Clear the text in the current field
textFieldObject.setText( "" );
//Add the not to a list for functionality...
ListOfNotes.add( NOTE );
}
...
//class definition of notOnJpanel
public class noteOnJPanel
{
String note;
public notOnJPanel ( String n )
{
note = n;
}
// all the other functions you need
...
...
...
}
Hello again. What i dont understand is which kind of component is noteOnJPanel. From what you wrote i understand that is a JPanel. I know how to get the text from a JTextArea with getText(). OK, once i have it in a String, how do i print the text? how do i put a text into a JPanel?
Thanks again.
you're joking!
That was definitely "Googleable".
But don't fear, I'll tell you anyway... :)
noteOnJPanel is not even an "extends" of JPanel... its just a class with a paint function inside it... To draw that text you want somewhere on the screen use the : "drawString(x,y,z)" function
public void paint( Graphics g )
{
g.drawString( textOfNote, xposition, yposition );
}
Thanks a lot.The problem was that i thought your class noteOnJPanel was a JPanel, and i was trying to insert a text into using some property of this JPanel.I have used before the paint function, so NOW ( :-) ) I understand what you mean.Thanks again. Have a nice day.