I have a software project I'm doing for university, and I just can't figure out how I could get one part of the user-interface to work. The program consists of a server, and several clients that contact the server. The clients are actually people who want to take a break from work, and the server should coordinate that enough people are working all the time.
So the program transpheres a file over the network to the server which updates an int field which tells the client program if he can go on break or not. (if there are too many people on break he ends up in a que). The idea is that an image on the interface refreshes after each connection, and the image that comes up is dependent on the status... (I hope I'm not explaining this in a complicated way) This code is quite messy as It's still under work.
I'm quite aware that I'll probably have to change the code quite alot... Most of the real action is taken care of by the class Moottori. Moottori both loads the images from the disk (and returns them as needed) and takes care of the network connections ect.
During all the java courses I've taken we've used AWT, but for this project I decided to use swing (as awt is quite old). I just can't seem to get my head around all the details (in time atleast).
So the program transpheres a file over the network to the server which updates an int field which tells the client program if he can go on break or not. (if there are too many people on break he ends up in a que). The idea is that an image on the interface refreshes after each connection, and the image that comes up is dependent on the status... (I hope I'm not explaining this in a complicated way) This code is quite messy as It's still under work.
Code:
/**
* The user interface of the program
**/
import javax.swing.*;
import javax.imageio.*;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
public class KLiittyma {
public static void main(String[] args) {
Raamit frame = new Raamit();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
/**
* Frame with a panel
**/
class Raamit extends JFrame {
public Raamit() {
setTitle("Tauko");
setSize(120, 400);
// add the panel
TilannePanel panel = new TilannePanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
/**
* set up the panel
**/
class TilannePanel extends JPanel {
public TilannePanel() {
Moottori yhteys = new Moottori();
// an image that should update when the status changes
jono = new JLabel(yhteys.annaIkoni(), JLabel.CENTER);
otsikko = new JLabel("Jonotilanne");
// an empty gif to give space
gap = new JLabel(yhteys.annaRako());
JButton taukoNappi = new JButton("Tauko");
// add the components
add(jono);
add(otsikko);
add(gap);
add(taukoNappi);
// actions...
ToggleAction taukoToimintaa = new ToggleAction(yhteys, 1);
taukoNappi.addActionListener(taukoToimintaa);
}
// panel components
private JLabel jono;
private JLabel otsikko;
private JLabel gap;
private Image image;
private Image image2;
// reacting to events
private class ToggleAction implements ActionListener {
public ToggleAction(Moottori yhteys, int nappi) {
moottori = yhteys;
pyynto = nappi;
}
// asks for permission to leave for a break or return
public void actionPerformed(ActionEvent event) {
// if status is ready
if (moottori.annaStatus() < 2) {
moottori.siirra(1);
}
// allready on break, return to work
else
moottori.siirra(2);
}
private Moottori moottori;
private int pyynto;
}
}
}
I'm quite aware that I'll probably have to change the code quite alot... Most of the real action is taken care of by the class Moottori. Moottori both loads the images from the disk (and returns them as needed) and takes care of the network connections ect.
During all the java courses I've taken we've used AWT, but for this project I decided to use swing (as awt is quite old). I just can't seem to get my head around all the details (in time atleast).