Schönen Vormittag,
ich habe ein JAVA-Programm, das in einem Canvas entweder Daten die per drawString "gemalt" worden sind oder ein animated gif - das Programm reagiert auf Tastatureingaben (z.B. wenn PF12 gedrückt wird, werden die drawString-Befehle ausgeführt, bei PF1-PF8 wird ein animated GIF geladen und angezeigt).
Jetzt habe ich das Problem, dass wenn die Gifs einmal geladen wurden offensichtlich im SPeicher bleiben und immer wieder ein repaint ausführen, was auf Dauer zu Überlagerungen und dann zu einem Flackern der Anzeige führt.
Ich bräuchte jetzt einen Weg, dass die geladenen Gifs, sofern sie nicht angezeigt werden, kein repaint ausführen - hat jemand eine Idee?
vielen Dank im voraus
Programm:
ich habe ein JAVA-Programm, das in einem Canvas entweder Daten die per drawString "gemalt" worden sind oder ein animated gif - das Programm reagiert auf Tastatureingaben (z.B. wenn PF12 gedrückt wird, werden die drawString-Befehle ausgeführt, bei PF1-PF8 wird ein animated GIF geladen und angezeigt).
Jetzt habe ich das Problem, dass wenn die Gifs einmal geladen wurden offensichtlich im SPeicher bleiben und immer wieder ein repaint ausführen, was auf Dauer zu Überlagerungen und dann zu einem Flackern der Anzeige führt.
Ich bräuchte jetzt einen Weg, dass die geladenen Gifs, sofern sie nicht angezeigt werden, kein repaint ausführen - hat jemand eine Idee?
vielen Dank im voraus
Programm:
Code:
import java.awt.BorderLayout;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DecimalFormat;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
/*
* Created on 26.04.2004
*
* To change the template for this generated file go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
/**
* @author Klausi
*
* To change the template for this generated type comment go to
* Window&Preferences&Java&Code Generation&Code and Comments
*/
public class Scoreboard50 extends JFrame
implements ActionListener, KeyListener
{
public static String HomeTeam = "Hometeam";
public static String GuestTeam = "Guestteam";
public static String remdist = "10";
public static String togo = "10";
public static int HPosition = 15;
public static int GPosition = 115;
public static int yPosition = 20;
public static int timeouth = 3;
public static int timeoutg = 3;
public static int scoreh = 0;
public static int scoreg = 0;
public static int ballon = 20;
public static int remdown = 1;
public static int qrt = 1;
public static int PlayedMin = 12;
public static int PlayedSec = 0;
public static boolean runClock = false;
public static JButton stopstart = new JButton("clock");
public static JButton setClock = new JButton("set time");
public static JButton downmarker = new JButton("to go");
public static JButton downcount = new JButton("down");
public static JButton ballison = new JButton("ball on");
public static JButton addhome = new JButton("Score h");
public static JButton addguest = new JButton("Score g");
public static JButton changeqrt = new JButton("chg. qrt.");
public static JButton timeh = new JButton("to h");
public static JButton timeg = new JButton("to g");
public static JButton opener = new JButton("opener");
public static JButton touchdown = new JButton("touchdown");
public static JTextField clockmin = new JTextField("00");
public static JTextField clocksec = new JTextField("00");
public static JTextField ballonc = new JTextField("00");
public static JTextField addpt = new JTextField("1");
public static JTextField down = new JTextField("00");
public static boolean isChanged = true;
public static boolean isOther = false;
public static boolean isTouchdown = false;
public static myCanvas area = new myCanvas();
public static int drawwhat = 1;
public Scoreboard50()
{
setTitle ("Raiders Scoreboard");
Container comp = getContentPane();
// comp.addKeyListener(this);
comp.setFocusable(true);
comp.addKeyListener(this);
JPanel Header = new JPanel();
Header.setFocusable(true);
Header.addKeyListener(this);
JPanel Footer = new JPanel();
Footer.addKeyListener(this);
Footer.setFocusable(true);
Header.setSize(300,15);
Header.setLayout(new GridLayout(1,1));
Footer.setSize(100,350);
Footer.setLayout(new GridLayout(4,8));
// Buttons/Textfelder werden definiert
stopstart.setFont(new Font ("Arial",Font.PLAIN,10));
setClock.setFont(new Font ("Arial",Font.PLAIN,10));
downmarker.setFont(new Font ("Arial",Font.PLAIN,10));
downcount.setFont(new Font ("Arial",Font.PLAIN,10));
ballison.setFont(new Font ("Arial",Font.PLAIN,10));
addhome.setFont(new Font ("Arial",Font.PLAIN,10));
addguest.setFont(new Font ("Arial",Font.PLAIN,10));
changeqrt.setFont(new Font ("Arial",Font.PLAIN,10));
timeh.setFont(new Font ("Arial",Font.PLAIN,10));
timeg.setFont(new Font ("Arial",Font.PLAIN,10));
opener.setFont(new Font ("Arial",Font.PLAIN,10));
touchdown.setFont(new Font ("Arial",Font.PLAIN,10));
setClock.addActionListener(this);
setClock.addKeyListener(this);
clockmin.setEditable(true);
clockmin.addActionListener(this);
clockmin.addKeyListener(this);
clocksec.setEditable(true);
clocksec.addActionListener(this);
clocksec.addKeyListener(this);
stopstart.addActionListener(this);
stopstart.addKeyListener(this);
downcount.addActionListener(this);
downcount.addKeyListener(this);
down.setEditable(true);
down.addActionListener(this);
down.addKeyListener(this);
downmarker.addActionListener(this);
downmarker.addKeyListener(this);
ballonc.setEditable(true);
ballonc.addActionListener(this);
ballonc.addKeyListener(this);
ballison.addActionListener(this);
ballison.addKeyListener(this);
addhome.addActionListener(this);
addhome.addKeyListener(this);
addpt.setEditable(true);
addpt.addKeyListener(this);
addpt.addActionListener(this);
addguest.addActionListener(this);
addguest.addKeyListener(this);
changeqrt.addActionListener(this);
changeqrt.addKeyListener(this);
timeh.addActionListener(this);
timeh.addKeyListener(this);
timeg.addActionListener(this);
timeg.addKeyListener(this);
// row 1
Header.add(setClock);
Header.add(clockmin);
Header.add(clocksec);
Header.add(stopstart);
// row 2
Footer.add(downcount);
Footer.add(down);
Footer.add(downmarker);
Footer.add(new JLabel(" "));
// row 3
Footer.add(ballonc);
Footer.add(ballison);
Footer.add(new JLabel(" "));
Footer.add(new JLabel(" "));
// row 4
Footer.add(addhome);
Footer.add(addpt);
Footer.add(addguest);
Footer.add(new JLabel(" "));
// row 4
Footer.add(changeqrt);
Footer.add(timeh);
Footer.add(timeg);
Footer.add(new JLabel(" "));
// row 5
// Footer.add(opener);
// Footer.add(touchdown);
// Footer.add(new JLabel(" "));
// Footer.add(new JLabel(" "));
// Footer.add(new JLabel(" "));
// clockCanvas drawCanvas = new clockCanvas();
area.setSize(400,300);
area.setTy(yPosition);
comp.add(Header, BorderLayout.NORTH);
comp.add(area, BorderLayout.CENTER);
comp.add(Footer, BorderLayout.EAST);
setSize(640,480);
setVisible(true);
counter zaehlen = new counter();
zaehlen.start();
}
public void actionPerformed(ActionEvent e)
{Object event = e.getSource();
if (e.getActionCommand() == "clock")
{if (runClock)
{runClock = false;
}
else {runClock = true;
}
}
if (e.getActionCommand() == "set time")
{PlayedMin = Integer.parseInt(clockmin.getText());
PlayedSec = Integer.parseInt(clocksec.getText());
area.setPlayedMin(PlayedMin);
area.setPlayedSec(PlayedSec);
area.repaint();
}
if (e.getActionCommand() == "ball on")
{ballon = Integer.parseInt(ballonc.getText());
area.setBallOn(ballon);
area.repaint();
}
if (e.getActionCommand() == "Score h")
{System.out.println(addpt.getText());
scoreh += Integer.parseInt(addpt.getText());
area.setScoreH(scoreh);
area.repaint();
}
if (e.getActionCommand() == "Score g")
{System.out.println(addpt.getText());
scoreg += Integer.parseInt(addpt.getText());
area.setScoreG(scoreg);
area.repaint();
}
if (e.getActionCommand() == "down")
{if (remdown == 4)
{remdown = 1;
}
else {remdown += 1;
}
togo = Integer.toString(remdown)+" and "+remdist;
area.setRemDown(remdown);
area.repaint();
}
if (e.getActionCommand() == "to go")
{remdist = down.getText();
togo = Integer.toString(remdown)+" and "+remdist;
area.setRemDist(remdist);
area.repaint();
}
if (e.getActionCommand() == "to h")
{if (timeouth == 0)
{timeouth = 3;
}
else {timeouth -= 1;
}
area.setTimeOutH(timeouth);
area.repaint();
}
if (e.getActionCommand() == "to g")
{if (timeoutg == 0)
{timeoutg = 3;
}
else {timeoutg -= 1;
}
area.setTimeOutG(timeoutg);
area.repaint();
}
if (e.getActionCommand() == "chg. qrt.")
{if (qrt == 4)
{qrt = 1;
}
else {qrt += 1;
}
area.setQrt(qrt);
area.repaint();
}
}
public static void main(String[] args)
{
JFrame f = new Scoreboard50();
HomeTeam = args[0];
HPosition = Integer.parseInt(args[1]);
GuestTeam = args[2];
GPosition = Integer.parseInt(args[3]);
area.setHomeTeam(HomeTeam);
area.setGuestTeam(GuestTeam);
area.setHTx(HPosition);
area.setGTx(GPosition);
area.setQrt(1);
area.setBallOn(20);
area.setRemDown(1);
area.setRemDist("10");
area.setPlayedMin(12);
// yPosition = Integer.parseInt(args[4]);
f.setSize(640,480);
f.setVisible(true);
f.setTitle("Raiders Scoreboard");
}
class counter extends Thread
{
public void run()
{while (true)
{if (runClock)
{try {sleep(1000);
if (PlayedSec==0)
{ PlayedMin -= 1;
PlayedSec = 59;
}
else
{ PlayedSec -= 1;
}
if (PlayedMin < 0)
{ PlayedMin = 0;
PlayedSec = 0;
area.setPlayedMin(PlayedMin);
area.setPlayedSec(PlayedSec);
runClock = false;
}
area.setPlayedMin(PlayedMin);
area.setPlayedSec(PlayedSec);
area.repaint();
}
catch (InterruptedException e) {}
}
}
}
}
public void keyPressed(KeyEvent arg0)
{if (arg0.getKeyCode() == 112)
{drawwhat = 2;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 113)
{drawwhat = 3;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 114)
{drawwhat = 4;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 115)
{drawwhat = 5;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 116)
{drawwhat = 6;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 117)
{drawwhat = 7;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 118)
{drawwhat = 8;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 119)
{drawwhat = 9;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 123)
{drawwhat = 1;
area.setdrawwhat(drawwhat);
area.repaint();
}
if (arg0.getKeyCode() == 17)
{if(runClock)
{runClock = false;
}
else
{runClock = true;
}
area.repaint();
}
}
public void keyReleased(KeyEvent arg0)
{
}
public void keyTyped(KeyEvent arg0)
{
}
}
class myCanvas extends Canvas
{
private static int PlayedMin, PlayedSec, GTx, HTx, Ty = 0;
private static int ScoreH, ScoreG = 0;
private static int TimeOutH = 3, TimeOutG = 3;
private static int RemDown, BallOn, Qrt = 0;
private static int drawwhat = 1;
private static String RemDist = "";
private static String HomeTeam, GuestTeam = "";
private String ImageNo = "NULL";
private String ImagePath = "c:\\Raiders\\picnull.gif";
private static Image homeim;
MediaTracker mt = new MediaTracker(this);
public myCanvas ()
{
super();
setSize(350,400);
setBackground(Color.black);
}
public void paint (Graphics g)
{
System.out.println(drawwhat);
if (drawwhat==1)
{DecimalFormat FormDecimal = new DecimalFormat("00;00");
// set the font for the remarks
Font remark = new Font("Arial",Font.PLAIN, 12);
Font detail = new Font("Arial",Font.BOLD, 24);
Font detair = new Font("Arial",Font.BOLD, 26);
Font detaiu = new Font("Arial",Font.BOLD, 32);
Font names = new Font("Arial",Font.BOLD, 15);
//Block mit weisser Schrift
g.setColor(Color.WHITE);
// draw the names
g.setFont(names);
g.drawString (HomeTeam, HTx, Ty);
g.drawString (GuestTeam,GTx, Ty);
g.setFont(detaiu);
g.drawString (FormDecimal.format(PlayedMin)+":"+FormDecimal.format(PlayedSec),92,90);
g.setColor(Color.YELLOW);
// draw the Timeouts
switch (TimeOutH)
{case 3: g.fillOval(17,33,6,6);
g.fillOval(17,43,6,6);
g.fillOval(17,53,6,6);
break;
case 2: g.fillOval(17,33,6,6);
g.fillOval(17,43,6,6);
break;
case 1: g.fillOval(17,33,6,6);
break;
case 0: break;
}
switch (TimeOutG)
{case 3: g.fillOval(233,33,6,6);
g.fillOval(233,43,6,6);
g.fillOval(233,53,6,6);
break;
case 2: g.fillOval(233,33,6,6);
g.fillOval(233,43,6,6);
break;
case 1: g.fillOval(233,33,6,6);
break;
case 0: break;
}
// set the font for the details
g.setFont(detair);
// draw the result
g.drawString (FormDecimal.format(ScoreH),60,58);
g.drawString (FormDecimal.format(ScoreG),175,58);
//draw the game-data
g.setFont(detail);
g.drawString (Integer.toString(RemDown), 125, 122);
g.drawString (RemDist, 189, 122);
g.drawString (FormDecimal.format(BallOn), 40, 122);
g.drawString (Integer.toString(Qrt), 50, 89);
g.setFont(remark);
g.drawString ("Down", 115, 133);
g.drawString ("to go", 190, 133);
g.drawString ("Ball on", 35, 133);
g.drawString ("Qtr.",25, 86);
}
else
{int picnum = drawwhat - 1;
String ImageNo = Integer.toString(picnum);
String ImagePath = "C:\\raiders\\pic0" + ImageNo + ".gif";
homeim = Toolkit.getDefaultToolkit().getImage(ImagePath);
mt.addImage(homeim, 0);
try {
//Warten, bis das Image vollständig geladen ist,
mt.waitForAll();
} catch (InterruptedException e) {
//nothing
}
g.drawImage(homeim,10,10,getBackground(),this);
}
}
public void setBallOn(int ballOn)
{BallOn = ballOn;
}
public void setGTx(int tx)
{GTx = tx;
}
public void setGuestTeam(String guestTeam)
{GuestTeam = guestTeam;
}
public void setHomeTeam(String homeTeam)
{HomeTeam = homeTeam;
}
public void setHTx(int tx)
{HTx = tx;
}
public void setPlayedMin(int playedMin)
{PlayedMin = playedMin;
}
public void setPlayedSec(int playedSec)
{PlayedSec = playedSec;
}
public void setQrt(int qrt)
{Qrt = qrt;
}
public void setRemDist(String remDist)
{RemDist = remDist;
}
public void setRemDown(int remDown)
{RemDown = remDown;
}
public void setScoreG(int scoreG)
{ScoreG = scoreG;
}
public void setScoreH(int scoreH)
{ScoreH = scoreH;
}
public void setTimeOutG(int timeOutG)
{TimeOutG = timeOutG;
}
public void setTimeOutH(int timeOutH)
{TimeOutH = timeOutH;
}
public void setTy(int ty)
{Ty = ty;
}
public void setdrawwhat(int drawing)
{drawwhat = drawing;
}
}