G
Guest
Gast
Hallo, ich habe gestern erstmals versucht eine Netzwerkapplikation zu erstellen und da kamen gleich einige Fragen auf. Zu allererst einmal der Code:
Nun habe ich folgende Frage: Wieso wird das geladene Bild nicht im Panel gezeichnet bzw. was mache ich falsch? Desweiteren ist mir bis heute irgendwie nie richtig klar geworden, was es nun für einen Unterschied macht ob ich bei einem JPanel die paintComponents oder paintComponent Methode überschreibe (habe vorher awt grafikoberflächen erstellt). Warum sollte man oder warum sollte man nicht die eine oder andere Methode überschreiben und wo liegen die Unterschiede? JPanel erbt ja beide und müsste deshalb mit beiden Methode neu zeichnen können.
Vielen Dank
Code:
import java.io.*;
import java.net.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import javax.swing.*;
import javax.swing.event.*;
public class NetTest extends JFrame implements ActionListener{
int frameWidth, frameHeight;
URL url;
URLConnection urlConnect;
Toolkit kit;
Image img;
JLabel urlLabel;
JTextField urlText;
ImagePanel imagePanel;
JButton okButton;
public NetTest(String title, int frameWidth, int frameHeight){
super(title);
setDefaultCloseOperation(EXIT_ON_CLOSE);
this.frameWidth = frameWidth;
this.frameHeight = frameHeight;
Container cp = this.getContentPane();
cp.setLayout(new GridBagLayout());
cp.setPreferredSize(new Dimension(frameWidth, frameHeight));
kit = Toolkit.getDefaultToolkit();
this.setLocation(kit.getScreenSize().width/2-frameWidth/2,
kit.getScreenSize().height/2-frameHeight/2);
GridBagConstraints con = new GridBagConstraints();
urlLabel = new JLabel("URL:");
urlText = new JTextField(9);
imagePanel = new ImagePanel(img);
imagePanel.setBorder(BorderFactory.createCompoundBorder(
BorderFactory.createLineBorder(Color.black),
BorderFactory.createEmptyBorder(2,2,2,2)));
okButton = new JButton("OK");
okButton.addActionListener(this);
con.gridx = 0; con.gridy = 0;
con.gridwidth = 1; con.gridheight = 1;
con.weightx = 1; con.weighty = 1;
con.fill = GridBagConstraints.BOTH;
con.anchor = GridBagConstraints.CENTER;
con.insets = new Insets(1,1,1,1);
con.ipadx = 0; con.ipady = 0;
cp.add(urlLabel, con);
con.gridx = 1; con.gridy = 0;
con.gridwidth = 3; con.gridheight = 1;
con.weightx = 1; con.weighty = 1;
con.fill = GridBagConstraints.BOTH;
con.anchor = GridBagConstraints.CENTER;
con.insets = new Insets(1,1,1,1);
con.ipadx = 0; con.ipady = 0;
cp.add(urlText, con);
con.gridx = 4; con.gridy = 0;
con.gridwidth = 1; con.gridheight = 1;
con.weightx = 1; con.weighty = 1;
con.fill = GridBagConstraints.BOTH;
con.anchor = GridBagConstraints.CENTER;
con.insets = new Insets(1,1,1,1);
con.ipadx = 0; con.ipady = 0;
cp.add(okButton, con);
con.gridx = 0; con.gridy = 1;
con.gridwidth = 5; con.gridheight = 1;
con.weightx = 1; con.weighty = 100;
con.fill = GridBagConstraints.BOTH;
con.anchor = GridBagConstraints.CENTER;
con.insets = new Insets(1,1,1,1);
con.ipadx = 0; con.ipady = 0;
cp.add(imagePanel, con);
pack();
setVisible(true);
}
public void actionPerformed(ActionEvent evt){
String cmd = evt.getActionCommand();
if(cmd.equals("OK")){
try{
url = new URL(urlText.getText());
Object obj = url.getContent();
if(obj instanceof ImageProducer){
img = kit.createImage((ImageProducer)obj);
imagePanel.repaint();
}
}
catch(MalformedURLException exc){
System.err.println(exc);
}
catch(IOException exc){
System.err.println(exc);
}
}
}
public static void main(String[] args){
NetTest net = new NetTest("Pic Loader", 200, 300);
}
}
Code:
import java.awt.*;
import javax.swing.*;
public class ImagePanel extends JPanel{
Image img;
public ImagePanel(Image img){
super();
this.img = img;
}
public void paintComponent(Graphics g){
super.paintComponent(g);
g.drawImage(img, 0, 0, this);
}
}
Nun habe ich folgende Frage: Wieso wird das geladene Bild nicht im Panel gezeichnet bzw. was mache ich falsch? Desweiteren ist mir bis heute irgendwie nie richtig klar geworden, was es nun für einen Unterschied macht ob ich bei einem JPanel die paintComponents oder paintComponent Methode überschreibe (habe vorher awt grafikoberflächen erstellt). Warum sollte man oder warum sollte man nicht die eine oder andere Methode überschreiben und wo liegen die Unterschiede? JPanel erbt ja beide und müsste deshalb mit beiden Methode neu zeichnen können.
Vielen Dank