Как убрать мигание компонентов фрейма при его перерисовке? - Java
Формулировка задачи:
Добрый день, ув. программисты
Прошу помочь: увлеклась Java, начинаю с простого. Суть задачи: есть фрейм, на нем прорисовывается анимация. На этот фрейм нанесены компоненты класса JLabel. Чтобы эти элементы увидеть, я не нашла ничего лучше, чем постоянно их перерисовывать во время смены очередного кадра анимации фрейма В связи с чем проблема: эти самые компоненты время от времени мигают Подскажите, чтобы такого придумать, чтобы не мигало?
Спасибо заранее!
П.С. В коде привела тело основного класса (gain), а также фрейма и одного из Label.
public class gain {
static addit addit;
static additional additional;
static Happy Happy;
static pushMe pushMe;
public static void main(String[] args) {
pushMe=new pushMe();
pushMe.startThread();
addit=new addit();
addit.startThread();
additional=new additional();
additional.startThread();
Happy=new Happy();
Happy.startThread(); } }
class Happy extends JFrame implements Runnable {
Image[] img=new Image[8];
int i;
Happy() {
super();
this.setSize(496, 450);
this.setLayout(null);
this.setResizable(false);
this.setLocation(300,100);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true); }
public void startThread() {
Thread stream1 = new Thread(this);
stream1.start(); }
public void run() {
this.add(gain.addit);
this.add(gain.additional);
this.add(gain.pushMe);
MediaTracker tr = new MediaTracker(this);
for ( i=0; i<8; i++ )
{img[i] = getToolkit().getImage(ResourceLoader.getImage("res/"+i+".png"));
tr.addImage(img[i], 0); }
try {
tr.waitForAll();}
catch(InterruptedException e) {}
i=0;
go();}
public void paint(Graphics g) {
g.drawImage(img[i], 0, 20, this);
gain.pushMe.repaint();
if (addit.status==1)
gain.addit.repaint();
else gain.additional.repaint(); }
public void update(Graphics g) {
paint(g); }
public void go() {
while(true) {
while( i<8 ) {
repaint();
try {
Thread.sleep(350);
i++;}
catch (InterruptedException e) {} }
i=0;} } }
class addit extends JLabel implements Runnable {
ImageIcon ico = new ImageIcon(ResourceLoader.getImage("res/button80.png"));
action Action= new action();
static int status=0;
BufferedImage buffer;
public void rebuildBuffer(){
int w = getWidth();
int h = getHeight();
buffer = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
Graphics2D g2d = buffer.createGraphics(); }
public void paintComponent(Graphics g) {
super.paintComponent(g);
if (buffer == null) {
rebuildBuffer(); }
g.drawImage(buffer, 0, 0, this); }
public addit() {
super();
this.setVisible(false);
this.setLocation(220, 310);
this.setSize(80, 80);
try{
this.setIcon(ico);
} catch (Exception e) {}
this.addMouseListener(Action); }
public void run() {
this.waitBeforeStart();
while(true) {
this.setVisible(true);
status=1;
this.waitMe();
this.setVisible(false);
status=0;
this.waitMe(); } }
public void startThread() {
Thread stream2 = new Thread(this);
stream2.start(); }
public void update(Graphics g) {
paintComponent(g); }
public void waitMe() {
try {
Thread.sleep(1000);}
catch (InterruptedException e) {} }
public void waitBeforeStart() {
try {
Thread.sleep(2000);}
catch (InterruptedException e) {} } }Решение задачи: «Как убрать мигание компонентов фрейма при его перерисовке?»
textual
Листинг программы
pushMe = new pushMe(); pushMe.startThread(); addit = new addit(); addit.startThread(); additional = new additional(); additional.startThread(); Happy = new Happy(); Happy.startThread();