layout
Hi, this is probably fairly simple but im only just beginning to program graphics in java. What I need is for the setPanel, which holds the setClockButton, to be underneath what the paint method produces. Anyone now how I can achieve this.
import java.awt.*;
import java.awt.event.*;
public class DisplayClockTime extends Panel implements ActionListener
{
private static int r = 65;
private int x = 150;
private int y = 150;
private int minX, minY, hourX, hourY;
private boolean timeSet;
private Label hourLabel = new Label("Hour");
private TextField hourField = new TextField(2);
private Label minsLabel = new Label("Mins");
private TextField minsField = new TextField(2);
private Button setClockButton = new Button("Set clock");
public Panel holdAll = new Panel();
private Panel timePanel = new Panel();
private Panel setPanel = new Panel();
public DisplayClockTime(){
holdAll.setLayout(new BorderLayout());
timePanel.add(hourLabel);
timePanel.add(hourField);
timePanel.add(minsLabel);
timePanel.add(minsField);
setPanel.add(setClockButton);
holdAll.add("North",timePanel);
holdAll.add("South",setPanel);
add(holdAll);
timeSet = false;
hourField.addActionListener(this);
minsField.addActionListener(this);
setClockButton.addActionListener(this);
}
public void paint(Graphics g){
g.drawOval(85,85,2*r,2*r);
if(timeSet == true){
g.setColor(Color.blue);
g.drawLine(x,y,minX,minY);
g.drawLine(x,y,hourX,hourY);
}
}
public void actionPerformed(ActionEvent e){
String hourEntered = hourField.getText();
String minsEntered = minsField.getText();
int minsIn, hourIn;
double theta;
if(e.getSource() == setClockButton){
minsIn = (int)Double.parseDouble(minsEntered);
hourIn = (int)Double.parseDouble(hourEntered);
theta = 2 * Math.PI * minsIn/60;
minX = x + (int)(r * 0.9 * Math.sin(theta));
minY = y - (int)(r * 0.9 * Math.cos(theta));
theta = 2 * Math.PI * (hourIn + minsIn/60.0)/12.0;
hourX = x + (int)(r * 0.65 * Math.sin(theta));
hourY = y - (int)(r * 0.65 * Math.cos(theta));
timeSet = true;
repaint();
}
}
}
|