Im trying to make a client server application. When i transfer jpg
files from server to client to view the files in a window the repaint
() method doesn't work. I get different files from the server but i
will see only the first file in my window(JFrame). So what's wrong
with my repaint() method or do i forget something?
Thanks for your support.
Code below:
<Client Code>
import java.net.*;
import java.io.*;
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
import java.util.*;
public class DiasClient extends JFrame
{
private JLabel label;
private JTextField hostveld;
private JList dirveld;
private DefaultListModel listModel;
private JButton knop;
private JPanel west, oost, westnoord, westzuid;
static BufferedReader stdin,in;
static PrintWriter out;
private Object test;
String shost, doorgeef;
int ipoort;
File[] groep;
public JPlaatje toonPlaatje;
BufferedInputStream fileIn;
BufferedOutputStream fileOut;
Socket client;
int data;
byte[]buffer;
public DiasClient(String host, int poort) {
shost = host;
ipoort = poort;
getContentPane().setLayout(new BorderLayout());
west = new JPanel();
west.setLayout(new BorderLayout());
westnoord = new JPanel();
westnoord.setLayout(new FlowLayout());
westzuid = new JPanel();
westzuid.setLayout(new BorderLayout());
oost = new JPanel();
label = new JLabel("Server (serverpoort):");
hostveld = new JTextField(20);
knop = new JButton(" Maak verbinding ");
knop.addActionListener(
new ActionListener()
{
public void actionPerformed
(ActionEvent a)
{
try {
client = new Socket
(shost, ipoort);
System.out.println
("Verbonden met de DiasServer");
in = new
BufferedReader(
new
InputStreamReader(client.getInputStream(), "latin1"));
// PrintWriter met
autoflush
out = new
PrintWriter(
new
OutputStreamWriter(client.getOutputStream(), "latin1"), true);
fileIn = new
BufferedInputStream(client.getInputStream());
fileOut = new
BufferedOutputStream(new FileOutputStream(new File("test.jpg")));
verbinden2(doorgeef);
}
catch(IOException ex)
{}
}
}
);
listModel = new DefaultListModel();
verbinden();
dirveld = new JList(listModel);
dirveld.setVisibleRowCount( 20 );
dirveld.setFixedCellWidth( 100 );
dirveld.setFixedCellHeight( 20 );
dirveld.setSelectionMode
(ListSelectionModel.SINGLE_SELECTION);
dirveld.addListSelectionListener(
new ListSelectionListener()
{
public void valueChanged(ListSelectionEvent
e)
{
test = dirveld.getSelectedValue();
doorgeef = ""+test;
}
}
);
westnoord.add(label);
westzuid.add(hostveld, BorderLayout.NORTH);
westzuid.add(knop, BorderLayout.CENTER);
westzuid.add(new JScrollPane(dirveld), BorderLayout.SOUTH);
west.add(westnoord, BorderLayout.NORTH);
west.add(westzuid, BorderLayout.CENTER);
getContentPane().add(west, BorderLayout.WEST);
getContentPane().add(oost, BorderLayout.EAST);
hostveld.setText(""+host + ":" + poort);
setSize(700,500);
//setLocation(150,150);
setVisible(true);
}
public static void main(String[] args) throws
UnknownHostException, IOException
{
if (args.length !=2)
throw new IllegalArgumentException("Syntaxis: java
DiasClient <host> <poort>");
//Maak verbinding met de server
String host = args[0];
int poort = Integer.parseInt(args[1]);
Socket client = new Socket(host, poort);
System.out.println("Verbonden met de DiasServer");
in = new BufferedReader(
new InputStreamReader
(client.getInputStream(), "latin1"));
// PrintWriter met autoflush
out = new PrintWriter(
new OutputStreamWriter
(client.getOutputStream(), "latin1"), true);
DiasClient venster = new DiasClient(host, poort);
venster.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void verbinden() {
try
{
String invoer;
out.println("lijst");
while((invoer = in.readLine()) != null)
listModel.addElement(invoer);
out.close();
}
catch (Exception ex)
{}
}
public void verbinden2(String s) {
int invoer;
int teller = 0;
File[] tester=null;
out.println("dir");
out.println(s);
try
{
while((data = fileIn.read()) >= 0)
{
fileOut.write(data);
}
fileOut.flush();
fileOut.close();
toonPlaatje = new JPlaatje(new File("test.jpg"));
toonPlaatje.setSize(400, 400);
getContentPane().add(toonPlaatje);
repaint();
}
catch (Exception ex)
{System.out.println(ex);}
}
}
<additional code JPlaatje>
import javax.swing.*;
public class JPlaatje extends JPanel
{
private ImageIcon plaatje;
public JPlaatje(File bestand) throws IOException
{
if (!bestand.isFile())
{
throw new IOException("Incorrecte bestandsnaam");
}
try
{
String bestandsnaam = bestand.getCanonicalPath();
plaatje = new ImageIcon(bestandsnaam);
System.out.println(bestand.length());
}
catch (Exception ex)
{
throw new IOException("IOException: " + ex);
}
}
public JPlaatje(byte[] buffer)
{
plaatje = new ImageIcon(buffer);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
g.drawImage(plaatje.getImage(), 0, 0, this);
}
}