You are not logged in.

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

1

Thursday, August 22nd 2013, 12:47am

Gradient Jlabel - How do I get text to appear?

Hey there,

I've got a ClassModule named 'GradientLabel' with SuperClass javax/swing/JLabel:

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Option Explicit

'override
'SuperClass: javax/swing/JLabel

Dim gradientPaint As java#awt#GradientPaint = New GradientPaint(0, 70, color.gray, 0, 0, color.white, False)

Public Sub paintComponent(g As Graphics)
'Public Sub paint(g As Graphics)
   On Error Goto LoadErr
   
   Debug.Print Me.getText()
   Dim g2 As Graphics2D = Cast(g, Graphics2D)
   g2.setPaint(gradientPaint)
   g2.fillRect(1, 0, Base.getWidth(), Base.getHeight())
   Me.paintComponent(g)
'   Me.paint(g)
   Exit Sub
   LoadErr:
   Stop
End Sub


calling it from Form1:

Jabaco Source

1
2
3
4
5
6
7
8
9
Public Sub Form_Load()
   Dim Label1 As New GradientLabel
   With Label1
      .setText("Hey there,")
      .setForeground(color.black)
      .setBounds(10,25,120,30)
   End With
   Me.add Label1
End Sub


It paints the gradient fine but lacks the Caption/Text that I set!
I suspect that the text is painted over by g2 since the debug.print shows it is there.
So I figured:

Jabaco Source

1
Me.paintComponent(arg2)


would draw the original content, but it throws an Error and loops on...

Any thoughts whats happening here or how to get a gradient background for a Label and still being able to show text and icons above it?


Dani

theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

2

Thursday, August 22nd 2013, 1:27am

You have to uncomment the line with paint() and comment the line with paintComponent().

Then it works fine.


Greatings
theuserbl

This post has been edited 1 times, last edit by "theuserbl" (Aug 22nd 2013, 1:36am)


theuserbl

Intermediate

Posts: 436

Date of registration: Dec 20th 2008

  • Send private message

3

Thursday, August 22nd 2013, 1:48am

Ok, there existing two alternatives.


With paint():

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
Option Explicit

'override
'SuperClass: javax/swing/JLabel

Dim gradientPaint As java#awt#GradientPaint = New GradientPaint(0, 70, color.gray, 0, 0, color.white, False)

'Public Sub paintComponent(g As Graphics)
Public Sub paint(g As Graphics)
   On Error Goto LoadErr
   
   Debug.Print Me.getText()
   Dim g2 As Graphics2D = Cast(g, Graphics2D)
   g2.setPaint(gradientPaint)
   g2.fillRect(1, 0, Base.getWidth(), Base.getHeight())
   Me.paintComponent(g)
'   Me.paint(g)
   Exit Sub
   LoadErr:
'   Stop
End Sub




With paintComponent():

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Option Explicit

'override
'SuperClass: javax/swing/JLabel

Dim gradientPaint As java#awt#GradientPaint = New GradientPaint(0, 70, color.gray, 0, 0, color.white, False)

Public Sub paintComponent(g As Graphics)
'Public Sub paint(g As Graphics)
   On Error Goto LoadErr
   
   ''Debug.Print Me.getText()
   Dim g2 As Graphics2D = Cast(g, Graphics2D)
   g2.setPaint(gradientPaint)
   g2.fillRect(1, 0, Base.getWidth(), Base.getHeight())
   g2.setPaint(Color.black)
   g2.drawString(Me.getText,0,20)
   Me.paintComponent(g)
 'Me.paint(g)
  Exit Sub
  LoadErr:
  ' Stop
End Sub


paintComponent paints the component. So in case of JLabel, the text, for example. So it have to be redrawn with drawString().
The current drawString() is not correct, because the position of the text is defined as the left button point of the String. But you need the point on the top. I have only write "20" for this example. You have to calculate the number with font metrics.

Greatings
theuserbl

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

4

Thursday, August 22nd 2013, 10:22am

Thanks theuserbl,

very strange...

Quoted

You have to uncomment the line with paint() and comment the line with paintComponent().

Then it works fine.


Flüchtigkeitsfehler...
I was playing around with all the different combinations last night (there are not so many :whistling: )

Quoted

'Public Sub paintComponent(g As Graphics)
Public Sub paint(g As Graphics)
...
Me.paintComponent(g)
' Me.paint(g)



I must have missed the one working combination !! :rolleyes:

The rest I got figured out myself...


Dani

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

5

Thursday, August 22nd 2013, 11:03am

Still,

If you outcomment the 'On Error' statement

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Option Explicit

'override
'SuperClass: javax/swing/JLabel

Dim gradientPaint As java#awt#GradientPaint = New GradientPaint(0, 70, color.gray, 0, 0, color.white, False)

Public Sub paintComponent(g As Graphics)
'   On Error Goto LoadErr

   Dim g2 As Graphics2D = Cast(g, Graphics2D)
   g2.setPaint(gradientPaint)
   g2.fillRect(1, 0, Base.getWidth(), Base.getHeight())
   g2.setPaint(Color.black)
   g2.drawString(Me.getText,0,20)
   Me.paintComponent(g)
  Exit Sub
  LoadErr:
'   Stop
End Sub


'Me.paintComponent(g)' throws an Error

Quoted

Exception In thread "AWT-EventQueue-0" java.lang.StackOverflowError
at java.util.HashMap.hash(Unknown Source)
at java.util.HashMap.put(Unknown Source)
at java.awt.RenderingHints.put(Unknown Source)
at sun.java2d.SunGraphics2D.makeHints(Unknown Source)
at sun.java2d.SunGraphics2D.getRenderingHints(Unknown Source)
at sun.java2d.pipe.AlphaPaintPipe.startSequence(Unknown Source)
at sun.java2d.pipe.SpanShapeRenderer$Composite.startSequence(Unknown Source)
at sun.java2d.pipe.SpanShapeRenderer.renderRect(Unknown Source)
at sun.java2d.pipe.SpanShapeRenderer.fill(Unknown Source)
at sun.java2d.pipe.PixelToShapeConverter.fillRect(Unknown Source)
at sun.java2d.SunGraphics2D.fillRect(Unknown Source)
at GradientLabel.paintComponent(GradientLabel.jsrc:64)
at GradientLabel.paintComponent(GradientLabel.jsrc:65)


and when you keep the 'On Error', 'Me.paintComponent(g)' does not make any visual difference for the output!
From what I read it is supposed to paint the original paint of the component over the g2 graphics...

Dani

Dani

Intermediate

  • "Dani" started this thread

Posts: 325

Date of registration: Nov 19th 2009

Location: GERMANY

  • Send private message

6

Thursday, August 22nd 2013, 11:14am

Hey there theuserbl,

You got me on the right path ... thanks again :)


FINAL VERSION - ' Me.setOpaque(False) ' makes all the difference !

Jabaco Source

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Option Explicit

'override
'SuperClass: javax/swing/JLabel

Dim gradientPaint As java#awt#GradientPaint = New GradientPaint(0, 70, color.gray, 0, 0, color.white, False)

Public Sub paint(g As Graphics)
   Me.setOpaque(False)
   Dim g2 As Graphics2D = Cast(g, Graphics2D)
   g2.setPaint(gradientPaint)
   g2.fillRect(1, 0, Base.getWidth(), Base.getHeight())
   Me.paintComponent(g)
End Sub


It keeps the original value for the caption/text now


Dani

This post has been edited 2 times, last edit by "Dani" (Aug 22nd 2013, 11:22am)


Rate this thread
WoltLab Burning Board