Ahoj, mam nasledujici kod:
// ####### Okno.java
package gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class Okno extends JFrame {
private JPanel contentPane;
private JPanel platno;
private DrawImage di;
private int x, y;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Okno frame = new Okno();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public Okno() {
setResizable(false);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
platno = new JPanel();
platno.setLayout(new BorderLayout());
this.getContentPane().add(platno, BorderLayout.CENTER);
di = new DrawImage();
di.setBackground(Color.BLACK);
//Ta barva se nikdy nenastavi :-(
platno.add(di, BorderLayout.CENTER);
//di.kresliCaru(0, 0, 20, 20, Color.RED);
/*
* Na tom to vzdycky spadne :-(
*/
}
}
// ####### DrawImage.java
package gui;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JPanel;
public class DrawImage extends JPanel {
private Graphics2D g;
@Override
public void paintComponent(Graphics g1D) {
g = (Graphics2D) g1D;
g.setColor(Color.RED);
g.drawOval(0, 0, 50, 100);
g.drawLine(10, 20, 50, 100);
}
public void kresliCaru(int x1, int y1, int x2, int y2, Color barva) {
g.setColor(barva);
g.drawLine(x1, y1, x2, y2);
g.drawLine(10, 20, 50, 100);
}
}
Nikdy se mi nedari zmenit barvu pozadi. A kdyz zavolam metodu pro kresleni usecky, tak program vzdy pri kompilaci spadne ("di.kresliCaru(0, 0, 20, 20, Color.RED);").
Jinak program normalne funguje: http://www.imagehosting.cz/?…
Muzete mi prosim poradit, kde delam chybu?