PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : AWT: Component ignoriert paint(Graphics g)...



Lin728
18-07-2003, 15:09
Hi there!

Ich schreibe gerade ein paar leightweight-Komponenten für AWT und habe folgendes Problem:

Ich hab meine Komponente von Component abgeleitet, und paint(Graphics g) überschrieben. Leider wird paint nie aufgerufen, was mach ich falsch?
Bei JComponent ging es doch immer so, was ist jetzt anders?

danke schonmal im Vorraus,

bischi
18-07-2003, 16:32
Paint wird nur dann aufgerufen, wenn die Komponente neu gezeichnet werden muss (Programm start, überlappende Fenster,...).

Inwiefern wird Paint nie aufgerufen? Was willst du genau machen? (Animation?)

Poste doch mal n Bisserl Code

MfG Bischi

Lin728
18-07-2003, 16:52
Hi Bischi!


Wie du siehst ist das einzige was ich machen will, ein lightweight awt-widget zu erstellen...
Und die paint-methode wird nicht einmal aufgerufen, nicht einmal wenn die Komponente erzeugt wird ;-(



import java.awt.*;

public class TableZell extends Component
{
Dimension prefferedSize = new Dimension(30,30);


public Dimension getMinimumSize()
{
return prefferedSize;
}

public Dimension getSize()
{
return prefferedSize;
}

public void paint(Graphics g)
{
System.out.println("HalloPaint");
g.setColor(Color.red);
g.fill3DRect(1,1,10,10,true);
g.drawString("Hallo!",2,2);
g.drawLine(1,1,9,9);
}

public Dimension getPreferredSize()
{
return prefferedSize;
}

public void setSize(Dimension size)
{
this.prefferedSize = size;
}

public void setSize(int x, int y)
{
this.prefferedSize = new Dimension(x,y);
}
}

anda_skoa
18-07-2003, 17:24
Wie sieht aus mit update oder paintAll?

Ciao,
_

bischi
18-07-2003, 17:31
Du hast den Konstruktor vergessen!!!

public TableZell()
{
//ev. hier Paint(), weiss nicht, musste ausprobieren
}

MfG Bischi

Lin728
18-07-2003, 17:57
Hi again!

Nein, es reicht angeblich laut Sun, dass man paint überschreibt, das wird dann automatisch aufgerufen.

Der Konstruktor ist egal, auch ein super() hilft nicht weiter. Paint gehört nicht in den Konstruktor...

Hier ist ein Beispiel von Sun, das funktioniert bei mir auch, nur kann ich keine relevanten Unterschiede erkennen:



import java.lang.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
* RoundButton - a class that produces a lightweight button.
*/
public class RoundButton extends Component {

String label; // The Button's text
protected boolean pressed = false; // true if the button is detented.

/**
* Constructs a RoundButton with the specified label.
* @param label the label of the button
*/
public RoundButton(String label) {
this.label = label;
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}

/**
* paints the RoundButton
*/
public void paint(Graphics g) {
int s = Math.min(getSize().width - 1, getSize().height - 1);

// paint the interior of the button
if(pressed) {
g.setColor(getBackground().darker().darker());
} else {
g.setColor(getBackground());
}
g.fillArc(0, 0, s, s, 0, 360);

// draw the perimeter of the button
g.setColor(getBackground().darker().darker().darke r());
g.drawArc(0, 0, s, s, 0, 360);

// draw the label centered in the button
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
g.setColor(getForeground());
g.drawString(label,
s/2 - fm.stringWidth(label)/2,
s/2 + fm.getMaxDescent());
}
}

/**
* The preferred size of the button.
*/
public Dimension getPreferredSize() {
Font f = getFont();
if(f != null) {
FontMetrics fm = getFontMetrics(getFont());
int max = Math.max(fm.stringWidth(label) + 40, fm.getHeight() + 40);
return new Dimension(max, max);
} else {
return new Dimension(100, 100);
}
}

/**
* The minimum size of the button.
*/
public Dimension getMinimumSize() {
return new Dimension(100, 100);
}

/**
* Paints the button and distribute an action event to all listeners.
*/
public void processMouseEvent(MouseEvent e) {
Graphics g;
switch(e.getID()) {
case MouseEvent.MOUSE_PRESSED:
// render myself inverted....
pressed = true;

// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the Gauge example).
repaint();
break;
case MouseEvent.MOUSE_RELEASED:
// render myself normal again
if(pressed == true) {
pressed = false;
// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the Gauge example).
repaint();
}
break;
case MouseEvent.MOUSE_ENTERED:
break;
case MouseEvent.MOUSE_EXITED:
if(pressed == true) {
// Cancel! Don't send action event.
pressed = false;

// Repaint might flicker a bit. To avoid this, you can use
// double buffering (see the DoubleBufferPanel example above).
repaint();

// Note: for a more complete button implementation,
// you wouldn't want to cancel at this point, but
// rather detect when the mouse re-entered, and
// re-highlight the button. There are a few state
// issues that that you need to handle, which we leave
// this an an excercise for the reader (I always
// wanted to say that!)
}
break;
}
super.processMouseEvent(e);
}
}

bischi
18-07-2003, 20:11
Also, wenn ich deinen Beispielcode mit folgendem Programm aufrufe (den ersten den du gepostet hast, den du selber geschrieben hast), so erscheint dein Dialogelment bei mir halb ausserhalb des Fensters (oben links), aber Paint() wird aufgerufen!



import java.awt.*;
import javax.swing.*;
import java.awt.event.*;

public class Test
extends JFrame
{
Container cp;

public Test()
{
super("Spiel");
this.setSize(809,829);
this.setLocation(5,5);
this.setResizable(false);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);

cp = getContentPane();
cp.setLayout(new BorderLayout());

TableZell tz = new TableZell();
cp.add(tz, BorderLayout.CENTER);

this.setVisible(true);
}


public static void main(String[] args)
{
new Test();
}
}


Vielleicht hast du das setVisible(true) am schluss vergessen.

MfG Bischi

Lin728
20-07-2003, 14:34
Nein, auch an setVisible hatte ich gedacht. Ich habe einfach ein neues File im JBuilder erstellt und den alten Inhalt reinkopiert, jetzt funktionierts?

bischi
20-07-2003, 14:41
Na, halt nicht JBuilder benutzen ;) .

Ich habe solche Probleme weniger, da ich SCITE (super Editor, einfach Googeln) verwende.

MfG Bischi