Witam,
mam problem z narysowaniem obrazka na panelu. Oto moj kod:
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import java.awt.Rectangle;
import javax.swing.JTextPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;
public class ramka extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel jContentPane = null;
private JButton jButton = null;
private Url nowy; // @jve:decl-index=0:
private JTable jTable = null;
private Image wykres; // @jve:decl-index=0:
Object[][] data = {
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
{"", "","", ""},
};
private JButton getJButton() {
if (jButton == null) {
jButton = new JButton();
jButton.setBounds(new Rectangle(551, 22, 117, 49));
jButton.setText("Sciagnij");
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
data = nowy.WIG20;
nowy.run();
repaint();
Graphics g = jContentPane.getGraphics();
g.drawImage(wykres, 400, 400, jContentPane);
g.drawLine(400, 400, 500, 500);
}
});
}
return jButton;
}
private JTable getJTable() {
if (jTable == null) {
jTable = new JTable(new MyTableModel());
jTable.setBounds(new Rectangle(28, 24, 400, 320));
}
return jTable;
}
public static void main(String[] args) {
// TODO Auto-generated method stub
SwingUtilities.invokeLater(new Runnable() {
public void run() {
ramka thisClass = new ramka();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public ramka() {
super();
initialize();
}
private void initialize() {
this.setSize(700, 400);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
nowy = new Url();
wykres = nowy.wykres;
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new JPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButton(), null);
jContentPane.add(getJTable(), null);
}
return jContentPane;
}
class MyTableModel extends AbstractTableModel{
private static final long serialVersionUID = 1L;
String[] columnNames = {"Nazwa",
"[godz.]",
"Bieżący",
"Zm. %",};
MyTableModel(){
super();
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return data.length;
}
public String getColumnName(int col) {
return columnNames[col];
}
public Object getValueAt(int row, int col) {
return data[row][col];
}
public Class<?> getColumnClass(int c) {
return getValueAt(0, c).getClass();
}
public boolean isCellEditable(int row, int col) {
if (col < 1) {
return false;
} else {
return true;
}
}
public void setValueAt(Object value, int row, int col) {
if(data[row][0] != ""){
data[row][col] = (String)value;
fireTableCellUpdated(row, col);
}
}
}
}
import java.net.*;
import java.awt.Image;
import java.io.*;
import javax.imageio.ImageIO;
public class Url {
String[] WIG20goly = new String[20];
String[][] WIG20 = new String[20][4];
Image wykres;
public void run() {
try {
URL u = new URL("http://www.bankier.pl/inwestowanie/profile/quote.html?symbol=WIG20");
InputStream is = u.openStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String theLine;
int i=0;
while ((theLine = br.readLine()) != null) {
if(theLine.contains("<tr align=center><td align=left><a class=articleTitleLink href=")) {
WIG20goly[i] = theLine;
i++;
}
}
WIG20goly[0] = WIG20goly[0].substring(5);
obrobka();
URL uu = new URL("http://em.bankier.pl/Portal/Data/Charts/WIG20_S1Y.gif");
wykres = ImageIO.read(uu);
}
catch (MalformedURLException ex) {
System.err.println(ex);
}
catch (IOException ex) {
System.err.println(ex);
}
}
public void obrobka(){
wyciagnijNazwe();
wyciagnijDate();
wyciagnijNotowanie();
wyciagnijZmiane();
}
public void wyciagnijNazwe(){
for(int i = 0; i < 20; i++){
WIG20goly[i] = WIG20goly[i].substring(105);
WIG20[i][0] = WIG20goly[i].substring(0,WIG20goly[i].indexOf('&'));
}
}
public void wyciagnijDate(){
for(int i = 0; i < 20; i++){
WIG20[i][1] = WIG20goly[i].substring(WIG20goly[i].indexOf('['),WIG20goly[i].indexOf(']')+1);
}
}
public void wyciagnijNotowanie(){
for(int i = 0; i < 20; i++){
WIG20goly[i] = WIG20goly[i].substring(WIG20goly[i].indexOf("<td class="));
WIG20goly[i] = WIG20goly[i].substring(WIG20goly[i].indexOf(">")+1);
WIG20[i][2] = WIG20goly[i].substring(0,WIG20goly[i].indexOf('<'));
}
}
public void wyciagnijZmiane(){
for(int i = 0; i < 20; i++){
WIG20goly[i] = WIG20goly[i].substring(WIG20goly[i].indexOf("<td class="));
WIG20goly[i] = WIG20goly[i].substring(WIG20goly[i].indexOf(">")+1);
WIG20[i][3] = WIG20goly[i].substring(0,WIG20goly[i].indexOf('<'));
}
}
}
Druga czesc jest malo wazna, chodzi mi o to aby po wcisnieciu przycisku program wymalowywal wykres.
Metoda która powinna to robic ma nastepujaca postać:
jButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
data = nowy.WIG20;
nowy.run();
repaint();
Graphics g = jContentPane.getGraphics();
g.drawImage(wykres, 400, 400, jContentPane);
g.drawLine(400, 400, 500, 500);
}
});
Guzik uzupełnia tabelke a nastepnie odrysowuje wykres i rysuje linie (testowo), przez ułamek sekundy widze linie a obrazka wcale.
Prosze o pomoc.