może na poczatku dam kod
import javax.swing.SwingUtilities;
import java.awt.BorderLayout;
import javax.swing.JPanel;
import javax.swing.JFrame;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.GridLayout;
import javax.swing.JButton;
import java.awt.Rectangle;
import java.awt.Dimension;
public class MFrame extends JFrame {
private int wlkRamki = 500;
private static final long serialVersionUID = 1L;
private MPanel jContentPane = null;
private JButton jButtonStart = null;
private JButton jButtonStop = null;
/**
* This method initializes jButtonStart
*
* @return javax.swing.JButton
*/
private JButton getJButtonStart() {
if (jButtonStart == null) {
jButtonStart = new JButton();
jButtonStart.setBounds(new Rectangle(450, 20, 50, 50));
jButtonStart.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
jContentPane.stol.run();
}
});
}
return jButtonStart;
}
/**
* This method initializes jButtonStop
*
* @return javax.swing.JButton
*/
private JButton getJButtonStop() {
if (jButtonStop == null) {
jButtonStop = new JButton();
jButtonStop.setBounds(new Rectangle(520, 20, 50, 50));
jButtonStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent e) {
}
});
}
return jButtonStop;
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
MFrame thisClass = new MFrame();
thisClass.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
thisClass.setVisible(true);
}
});
}
public MFrame() {
super();
initialize();
}
private void initialize() {
this.setSize(609, 628);
this.setContentPane(getJContentPane());
this.setTitle("JFrame");
}
private JPanel getJContentPane() {
if (jContentPane == null) {
jContentPane = new MPanel();
jContentPane.setLayout(null);
jContentPane.add(getJButtonStart(), null);
jContentPane.add(getJButtonStop(), null);
}
return jContentPane;
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
public class MPanel extends JPanel{
Graphics G;
Stol stol;
public MPanel(){
stol = new Stol();
}
protected void paintComponent(Graphics g) {
G = g;
stol.rozdajWidelce(g);
System.out.println("widelce rozdane");
}
}
import java.awt.Graphics;
import javax.swing.JPanel;
class Stol extends Thread{
final static int X = 300;
final static int Y = 320;
Widelec[] widelce;
Gosc[] goscie;
Graphics G;
public Stol(){
widelce = new Widelec[17];
goscie = new Gosc[9];
for(int i = 1;i<17;i++){
widelce[i] = new Widelec();
}
for(int i = 1;i<9;i++){
goscie[i] = new Gosc();
}
}
void rozdajWidelce(Graphics g){
G = g;
ulozWidelce(g);
przydzielWidelce();
}
private void ulozWidelce(Graphics g){
widelce[7].setWidelec(g, 300, 50, 550, 483);
widelce[4].setWidelec(g, 550, 483, 50, 483);
widelce[1].setWidelec(g, 50, 483, 300, 50);
widelce[12].setWidelec(g, X, Y-50, X+75, Y+80);
widelce[10].setWidelec(g, X+75, Y+80, X-80, Y+80);
widelce[11].setWidelec(g, X-80, Y+80, X, Y-50);
widelce[16].setWidelec(g, X, Y-50, X, Y+30);
widelce[14].setWidelec(g, X, Y+30, X-80, Y+80);
widelce[15].setWidelec(g, X, Y+30, X+75, Y+80);
widelce[5].setWidelec(g, X, Y-50, 300, 50);
widelce[9].setWidelec(g, X+75, Y+80, 550, 483);
widelce[3].setWidelec(g, X-80, Y+80, 50, 483);
widelce[6].setWidelec(g, 340, 200, 300, 50);
widelce[8].setWidelec(g, 340, 200, 550, 483);
widelce[2].setWidelec(g, 340, 200, 50, 483);
widelce[13].setWidelec(g, 340, 200, X, Y+30);
}
private void przydzielWidelce(){
goscie[1].dajWidelce(widelce[1], widelce[2], widelce[3], widelce[4]);
goscie[2].dajWidelce(widelce[1], widelce[5], widelce[6], widelce[7]);
goscie[3].dajWidelce(widelce[7], widelce[8], widelce[9], widelce[4]);
goscie[4].dajWidelce(widelce[3], widelce[11], widelce[14], widelce[10]);
goscie[5].dajWidelce(widelce[10], widelce[15], widelce[12], widelce[9]);
goscie[6].dajWidelce(widelce[16], widelce[11], widelce[12], widelce[5]);
goscie[7].dajWidelce(widelce[13], widelce[14], widelce[15], widelce[16]);
goscie[8].dajWidelce(widelce[2], widelce[13], widelce[6], widelce[8]);
}
public void run(){
goscie[1].run();
}
}
public class Gosc extends Thread{
int nr;
Widelec w1,w2,w3,w4;
public void dajWidelce(Widelec w1, Widelec w2, Widelec w3, Widelec w4){
this.w1 = w1;
this.w2 = w2;
this.w3 = w3;
this.w4 = w4;
}
public void run() {
w1.use();
}
}
import java.awt.Color;
import java.awt.Graphics;
class Widelec {
int x1,y1,x2,y2;
Graphics g;
public void setWidelec(Graphics g, int x1, int y1, int x2, int y2){
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
this.g = g;
initialize();
}
public void initialize(){
g.drawLine(x1, y1, x2, y2);
}
public void use(){
g.setColor(new Color(255,0,0));
g.drawLine(x1, y1, x2, y2);
}
public void putBack(){
g.setColor(new Color(255,255,255));
g.drawLine(x1, y1, x2, y2);
}
}
a moje pytanie jest nastepujace:
Dlaczego podczas wcisniecia jButtonStart w JFrame program wchodzi do funkcji
public void use(){
g.setColor(new Color(255,0,0));
g.drawLine(x1, y1, x2, y2);
}
ale polecienie g.drawLine(x1, y1, x2, y2); nie rysuje czerwonej linii.
pozdrawiam