|
|
|||||||||
|
|||||||||
|
|||||||||
| |
|||
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Newbie Question Regarding Layouts
Hi. I am brand new to Java. This is a very basic question.
I have been playing around with layouts. I started off with the FlowLayout. Basically, I wanted a layout with buttons, text fields and few regular shapes. I was able to come with the previous two, but when i draw shapes, the buttons and text boxes dissappear. I am using the paint(Graphics g) to draw these shapes. Is there a possibility to show both controls and graphics on the container?. If not, is it also possible to display the buttons on one window and the shapes on another?. Thanks in advance! |
|
#2
|
|||
|
|||
|
no one?
![]() |
|
#3
|
|||
|
|||
|
I'm guessing you probably forgot to overide paincomponent.
Here's a piece of one of my codes, not the best solution, but I hope it helps. This is after creating texfields and buttons: It does it all on the same window without you needing to specify that. Code:
public void paintComponent(Graphics g)
{
super.paintComponent (g);
X=-500; //X=-500 is Xmin
g.setColor (Color.white); //sets color of graphs background
g.fillRect (0,55,size,size); //draws the graph area background
g.setColor (Color.black); //sets color of axis
g.drawLine (size/2,55,size/2,size+55); //draws vertical axis
g.drawLine (0,((size+50)/2)+100,size,((size+50)/2)+100); //draws horizontal axis
while (X<500) //X=500 is Xmax
{
Y = A*Math.pow(X,3) + B*Math.pow(X,2) + C*X + D; //AX^3 + BX^2 + CX + D
g.setColor (Color.red); //sets color of the graph
int Y2 = (int)Y;
int X2 = (int)X; //X and Y must be integers to fit in drawline command.
X=X+0.01; //
//System.out.println (X2+" , "+Y2); //may be used to optionally dysplay values for X and Y
g.drawLine ((Y2+(size/2)),(X2+((size+50)/2)+100),(Y2+(size/2)),X2+((size+50)/2)+100);
//draws the graph
}
}
private class PlotterAdapter implements ActionListener
{
public void actionPerformed (ActionEvent event)
{
String text1 = input1.getText(); //gets a string from user
String text2 = input2.getText(); //
String text3 = input3.getText(); //
String text4 = input4.getText(); //gets a string from user
A = Double.parseDouble (text1); //converts string to Double
B = Double.parseDouble (text2); //
C = Double.parseDouble (text3); //
D = Double.parseDouble (text4); //converts string to Double
repaint(); //redraws the graph when button is pressed
}
}
oh and don't forget the repaint at the very bottom, really important. |
|
#4
|
|||
|
|||
|
can you post your code??? so that we can understand what is excatly your problem... as you are a newbie.. you might be doing some simple things wrong... there is nothing that stops you from dispalying graphics along with GUI... but put the graphics in a separate panel and attach it to the Frame's content pane...
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > Java Development > Newbie Question Regarding Layouts |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|