dcsimg
2 Threads calling same paint method results in partially blank screen
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Jos_Nienhuis
Posted On:   Monday, July 21, 2008 12:42 PM

Hi all, I'm having issues with a threading problem. The code I wrote so far is working on the JWTK and also on my test device (Sony Ericsson w810i). However the test device is showing some issues after say 2 minutes of (intensively) running the program. Here's what I'm trying to accomplish: I have a title at the top of the Canvas. If the title is to long it has to scroll horizontally through the screen. Below the title sits a text. The text can be longer then the height of the screen and for this purpose I created a scrollbar. When a user presses down the screen wil move the top line of text of the screen, opposit but same for scrolling upwards. I have a class variable called lineNo that keeps track o   More>>

Hi all,


I'm having issues with a threading problem. The code I wrote so far is working on the JWTK and also on my test device (Sony Ericsson w810i). However the test device is showing some issues after say 2 minutes of (intensively) running the program.



Here's what I'm trying to accomplish:
I have a title at the top of the Canvas. If the title is to long it has to scroll horizontally through the screen.
Below the title sits a text. The text can be longer then the height of the screen and for this purpose I created a scrollbar. When a user presses down the screen wil move the top line of text of the screen, opposit but same for scrolling upwards.
I have a class variable called lineNo that keeps track of the last line painted on the screen. So if the user presses the scroll buttons the paint() method knows where to start. The lineNo is increased in the paint() method (while painting the lines of text).
However the title also utilizes paint() to draw itself on the screen. That's my problem right there.
I inserted some logic to not increase the lineNo variable while the title is painting, but doesn't seem to deal with the bug.



Right the bug, almost forgot:
After some time of navigating through the application and ending up at a textual page like described above, the application seems to only want to draw the title. When this bug occurs it's visible everywhere in the application.
I have a hard time trying to figure out what's happening (why is the text not drawn?). I already figured out that the variable lineNo is correct when the bug occurs. I'm not to sure yet of another variable that decides on which y coordinate the line of text should be drawn, I'll try to debug this one asap.



here's some of the code (the code that's important to this problem)



The runnable method of a class that's responsible for making the title move


			
public void run() {
char firstChar = textAsChar[0];
char[] temp = new char[charLength];
System.arraycopy(textAsChar, 1, temp, 0, charLength - 1);
temp[charLength - 1] = firstChar;
textAsChar = temp;

painting = true;
if(partialRepaint){
view.repaint(0, 0, View.width, View.titleSpacing);
}
else {
view.repaint();
}
}

The paint method (sorry it's kinda bloated atm)
			
public synchronized void paint(Graphics g) {
//Tekenen van achtergrond
g.setColor(Config.detail_bg);
g.fillRect(0, 0, width, height);
g.setColor(Config.detail_text);

if(!Controller.fullScreen) this.removeCommand(bellen);
else commands = new String[]{"Terug"};

int rowY = 0, imageX = width / 2, imageY;
boolean imagePainted = false;

if(scrollText == null || !scrollText.painting){
startLineNo = lineNo;
while (lineNo < amountOfLines) {
g.setColor(0,0,0);
rowY = titleSpacing + ((lineNo - startLineNo) * Controller.defaultFontHeight);

if (rowY + Controller.defaultFontHeight > height - Controller.defaultFontHeight) {
break;
}

//Selectievak tekenen voor telefoonnummers mits dit nodig is
if(detailModel.amountOfNumbers > 0 && !phoneSelectionDrawn){
if(((amountOfLines - detailModel.amountOfNumbers) == lineNo) &&
selectedPhoneNumber == 0){
selectedPhoneNumber = amountOfLines - detailModel.amountOfNumbers;

}
if(selectedPhoneNumber != 0 && lineNo == selectedPhoneNumber){
g.setColor(0, 0, 0);
g.fillRect(0, rowY, width - scrollbarWidth, Controller.defaultFontHeight);
g.setColor(255,255,255);
phoneSelectionDrawn = true;
if(!Controller.fullScreen) this.addCommand(bellen);
else commands = new String[]{"Terug", "Bellen"};
}
}

//Tekenen van inline images
for(int i = 0; i < detailModel.imageLineNo.length; i++){
//Image valt in het zichtbare gedeelte
if(detailModel.imageLineNo[i][0] + detailModel.imageLineNo[i][1] >= startLineNo &&
lineNo <= detailModel.imageLineNo[i][0] + detailModel.imageLineNo[i][1]){
if(!imagePainted){
imageY = titleSpacing + (detailModel.imageLineNo[i][0] + detailModel.imageLineNo[i][1] - lineNo) * Controller.defaultFontHeight;
g.drawImage(detailModel.images[i], imageX, imageY, Graphics.BOTTOM | Graphics.HCENTER);
imagePainted = true;
}
break;
}
}

//Spatie toevoegen aan eerste regel voor uitlijning
String textLine = (lineNo == 0 ? " " : "") + (String) lines.elementAt(lineNo);
g.drawString(textLine, 0, rowY, 0);

lineNo++;
}
}

paintScrollbar(g);
paintNavigation(g, commands);
paintTitle(g);

phoneSelectionDrawn = false;
}


the method that actually paints the title
			
protected void paintTitle(Graphics g){
g.setFont(titleFont);
g.setColor(Config.title_bg);
g.fillRect(0, 0, width, titleSpacing);
g.setColor(Config.title_text); // Oranje

// 25 voor uitlijning / marge (vanaf links)
if(titleFont.stringWidth(title) + 25 >= width){
if(scrollText == null){
scrollText = new ScrollingText(this);
scrollText.init(title, true);
timer.schedule(scrollText, 500, 500);
}
if(!scrollText.text.equals(title)){
scrollText.init(title, true);
}
g.drawString(new String(scrollText.textAsChar), 25, 16 - (titleFont.getHeight() / 2), 0);
scrollText.painting = false;
}
else {
//16 = De helft van de hoogte van de titelbalk
g.drawString(title, 25, 16 - (titleFont.getHeight() / 2), 0);
}

if (Controller.type == '3' || Controller.type == '7') { // type '3' == detail, '7' = image
g.fillRect(0, titleSpacing, width, 2); // border-bottom
}
}

I will greatly appreciate some insights to this problem. Other comments about the code are also welcome.

Thanks for taking a look!    <<Less
About | Sitemap | Contact