[java] g = eenPanel.getGraphics (= null)

Pagina: 1
Acties:
  • 46 views sinds 30-01-2008

  • dexter07051982
  • Registratie: November 2001
  • Laatst online: 11-07 22:38
waarom tekent de applet bij het opstarten geen dots (g=nul in testPanel class na panel.getGraphics()) en bij het klikken op draw wel??

voor verduidelijking kijk even naar de onderstaande code

De testPanel class
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import java.awt.*;
import javax.swing.*;

public class testPanel
{
  private static final int LEN = 200;

  private JPanel panel;
  private int nDots;
  private int firstRed = 0;

  public testPanel(JPanel canv, int dots)
  {
    panel = canv;
    nDots = dots;
  }


  public void draw()
  {
    Graphics g = panel.getGraphics();
    System.out.println(panel.getGraphics());

    if(g!=null)
    {
    for(int nDrawn = 0; nDrawn < nDots; nDrawn++)
    {
      int x = (int)(Math.random()*LEN);
      int y = (int)(Math.random()*LEN);
      g.fillOval(x,y,3,3);

      if(Math.random() < 0.001)
      {
        g.setColor(Color.red);
        firstRed = nDrawn;
      }
    }

    }
  }
}

De Applet class
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class testApplet extends JApplet implements ActionListener
{
  private final int NDOTS = 10000;
  private JPanel test = new JPanel();
  private testPanel panel;
  private Button draw = new Button("Draw");

  public void init()
  {
    this.getContentPane().setLayout(null);
    draw.setBounds(210,10,100,20);
    draw.addActionListener(this);
    this.getContentPane().add(draw);
  //  test.setBorder(BorderFactory.createTitledBorder("Drawing panel"));
    test.setBounds(10,10,200,200);
    panel = new testPanel(test,NDOTS);
    panel.draw();

    this.getContentPane().add(test);
  }

  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()== draw)
    {
    panel = new testPanel(test, NDOTS);
    panel.draw();
    }
  }
}

voorbeeld uit JAVA, JAVA, JAVA
kan alleen niet vinden waarom hij wel tekent na klikken op draw en niet bij opstarten.

  • whoami
  • Registratie: December 2000
  • Laatst online: 21:13
Waarom heeft D2k uw eerdere post gesloten:
[topic=528619]

Debug eens, wij gaan niet voor u gaan debuggen.

https://fgheysels.github.io/


Dit topic is gesloten.