[Java] Graphics: Het maken van grote counters

Pagina: 1
Acties:

Onderwerpen


Acties:
  • 0 Henk 'm!

  • Snow_King
  • Registratie: April 2001
  • Laatst online: 17:59

Snow_King

Konijn is stoer!

Topicstarter
Hi,

Ik ben bezig een .Net applicatie om te schrijven naar Java toe ( de beweegredenen doen er even niet toe ).

Deze .Net is door een programmeur snel in elkaar gezet als proof of concept en ik probeer dit nu in Java te herschrijven.

Bij de GUI loop ik nu echter even vast.

Afbeeldingslocatie: http://zooi.widodh.nl/got/screenshot_vlaggen_programma_small.jpg

In .Net is het erg makkelijk om zulke "counters" te maken, in Java lijkt dit niet echt makkelijk.

Ik ben al aan de slag gegaan met het Swing component "JFormattedTextField", maar dat lijkt hem ook niet te worden.

Ik sta nu op het punt dit te gaan tekenen met de old-skool java.awt.Graphics library, maar dat is ook niet echt wenselijk.

Iemand een idee?

Acties:
  • 0 Henk 'm!

  • sig69
  • Registratie: Mei 2002
  • Nu online
Heb je het lettertype van die counters? Of worden ze custom getekend? Als je het lettertype heb kan je toch gewoon wat labels neerzetten met een grote font size?

Roomba E5 te koop


Acties:
  • 0 Henk 'm!

  • bat266
  • Registratie: Februari 2004
  • Laatst online: 09:29
een andere oplossing kan zijn plaatjes van 0-9 neer te zetten afhankelijk van het gewenste getal, is iets makkelijker dan AWT-graphics imho

Better to remain silent and be thought a fool then to speak out and remove all doubt.


Acties:
  • 0 Henk 'm!

  • krvabo
  • Registratie: Januari 2003
  • Laatst online: 13:39

krvabo

MATERIALISE!

Gaat het om de layout, of gaat het om de functionaliteit?

Anders kun je nog eens kijken naar Apache Commons Lang die een stopwatchfunctionaliteit heeft.. Doc
public String toSplitString()

Gets a summary of the split time that the stopwatch recorded as a string.

The format used is ISO8601-like, hours:minutes:seconds.milliseconds.

Pong is probably the best designed shooter in the world.
It's the only one that is made so that if you camp, you die.


Acties:
  • 0 Henk 'm!

  • Snow_King
  • Registratie: April 2001
  • Laatst online: 17:59

Snow_King

Konijn is stoer!

Topicstarter
Het is puur functioneel, ik wil die tellers laten lopen, maar er is een andere thread die de gametijd en scores bij houdt.

Maar aan het lettertype kan ik wel komen. Ook is het plaatjes idee geen slechte.

Acties:
  • 0 Henk 'm!

  • Snow_King
  • Registratie: April 2001
  • Laatst online: 17:59

Snow_King

Konijn is stoer!

Topicstarter
Uiteindelijk was het JLabel de optie, hier een stukje code voor iemand die dat interessant vindt.

code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    JLabel redScore = new JLabel("13:21");
    JLabel blueScore = new JLabel("22:41");
    
    blueScore.setForeground(Color.BLUE);
    redScore.setForeground(Color.RED);

    try {
        Font ttf = Font.createFont(Font.TRUETYPE_FONT, new BufferedInputStream(new FileInputStream("resource/fonts/SFDigitalReadout-Heavy.ttf"))).deriveFont(Font.TRUETYPE_FONT, 50);
        blueScore.setFont(ttf);
        redScore.setFont(ttf);
    } catch (Exception e) {
        Logger.msg("Error", "Error loading font: " + e.getMessage());
    }

    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(blueScore);
    cp.add(redScore);

Acties:
  • 0 Henk 'm!

  • Cobalt
  • Registratie: Januari 2004
  • Laatst online: 08-10 18:51
Je zou de Java GUI ook in JavaFX kunnen bouwen.

Een voorbeeld van een stopwatch met een strakke GUI. http://javafx.com/samples/StopWatch/index.html

Een code achter van een counter zou er als volgt uit kunnen zien.
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package form1;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.VBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.transition.ParallelTransition;

def font: Font = Font {
    name: "SFDigitalReadout-Heavy"
    size: 50
}

var minutes: Integer = 0;
var seconds: Integer = 0;
var timeString: String = bind "{%02d minutes}:{%02d seconds}";

var counterLabel = Label {
    text:  bind "{timeString}"
    font: font
    textFill: Color.RED
}

var transition: ParallelTransition = ParallelTransition {
    node: counterLabel
    content: [
        Timeline {
            repeatCount: 3600
            keyFrames:[
                KeyFrame {
                    time: 1s 
                    action: function() {
                        if(seconds < 60) {
                            seconds++ 
                        } else {
                            seconds = 0;
                            minutes++; 
                        }
                    }
                }
            ]
        }
    ]
}
                        
Stage {
     title : "Form1"
     scene: Scene {
        content: [
            VBox {
                content: [
                    counterLabel,
                    Button {
                        text: bind if(transition.running) "Stop" else "Start" 
                        action: function(){
                            if(transition.running) transition.stop() else transition.play()
                        }
                    }
                ]
            }
        ]
     }
}

[ Voor 22% gewijzigd door Cobalt op 08-02-2010 23:40 ]


Acties:
  • 0 Henk 'm!

  • Snow_King
  • Registratie: April 2001
  • Laatst online: 17:59

Snow_King

Konijn is stoer!

Topicstarter
Dat ziet er erg goed uit! Overigens ga ik het project Open Source maken, dus zodra ik het geheel draaiend heb, dan zal ik hier nog wel een linkje plaatsen naar het project.

Acties:
  • 0 Henk 'm!

  • Cobalt
  • Registratie: Januari 2004
  • Laatst online: 08-10 18:51
Ik heb net even snel, mijn vorige code sampel aangepast. Nu is het wat netter met aparte klasse voor Counter, het kan nog wat netter, ik heb het ook maar even snel in elkaar gezet.

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package form1;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Node;
import javafx.scene.Group;
import javafx.scene.layout.VBox;
import javafx.scene.layout.HBox;
import javafx.scene.layout.LayoutInfo;
import javafx.geometry.BoundingBox;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.Control;
import javafx.scene.control.Skin;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Text;
import javafx.scene.text.Font;
import javafx.scene.paint.Color;
import javafx.animation.Timeline;
import javafx.animation.KeyFrame;
import javafx.animation.transition.ParallelTransition;

// custom font is gedefinieerd in META-INF/fonts.mf en de font zit in de folder /fonts
def font: Font = Font {
    name: "SFDigitalReadout-Heavy"
    size: 50
}

class Counter extends Control {
    
    override public-read protected var layoutBounds = BoundingBox {
        height: 70
        width: 220
    }
    
    public var minutes: Integer = 0;
    public var seconds: Integer = 0;
    
    public var label: String;
    
    // deze methode wordt aangeroepen bij het aanmaken van het object
    override public function create(): Node {
        if(skin == null)
           skin =  CounterSkin{}
        super.create();
    }
    
    public function isRunning(): Boolean {
        return (skin as CounterSkin).transition.running;
    }
    
    public function play(): Void {
        (skin as CounterSkin).transition.play();
    }
    
    public function stop(): Void {
        (skin as CounterSkin).transition.stop();
    }
    
    public function reset(): Void {
        seconds = 0;
        minutes = 0;
    }
       
}

class CounterSkin extends Skin {
    
    var timeString: String = bind "{%02d (control as Counter).minutes}:{%02d (control as Counter).seconds}";
    
   // bind kan je vergelijke met een Observer pattern, als timeString veranderd dan wordt de text van redScore 
   //geupdate. bind ... with inverse is ook mogelijk in sommige gevallen dan wordt timeString ook geupdate als 
   //de text van redScore gewijzigd wordt.
    var redScore: Label = Label {
        text:  bind "{timeString}"
        font: font
        textFill: Color.RED
    }
    
    public var transition: ParallelTransition = ParallelTransition {
        node: redScore
        content: [
            Timeline {
                repeatCount: 3600
                keyFrames:[
                    KeyFrame {
                        time: 1s 
                        action: function() {
                            if(seconds < 60) {
                                seconds++ 
                            } else {
                                seconds = 0;
                                minutes++; 
                            }
                        }
                    }
                ]
            }
        ]
    }
    // draw de counter
    init {
        node = Group {
            content: [
                Text{ x: 20 y: 15 content: "Totals" fill: Color.BLUE}
                Rectangle{x:10 y: 10 height: 50 width: 200 strokeWidth: 1 stroke: Color.LIGHTGREY fill: Color.TRANSPARENT}
                HBox {
                    layoutX: 20
                    layoutY: 20
                    content: [
                        redScore
                    ]
                }
            ]
        }
    }
        
    override function contains(localX: Number, localY: Number): Boolean {
        node.contains(localX, localY);
    }
    
    override function intersects(localX: Number, localY: Number,
        localWidth: Number, localHeight: Number): Boolean {
        node.intersects(localX, localY, localWidth, localHeight);
    }
}


// instantieren van een object Counter. Counter{} is aanroepen van de default constructor.
var counter: Counter = Counter{};

                    
Stage {
     title : "Form1"
     width: 250
     height: 150
     scene: Scene {
        content: [
            Group {
                content: [
                    VBox {
                        content: [
                            counter,
                            Button {
                        text: bind if(counter.isRunning()) "Stop" else "Start" 
                                action: function(){
                                    if(counter.isRunning()) counter.stop() else {counter.reset(); counter.play();}
                                }
                                layoutInfo: LayoutInfo {
                                    width: 100
                                    height:40   
                                }
                            }
                        ]
                    }
                ]   
            }   
         ]
    }
}
Pagina: 1