JTextArea不显示当重绘()时,但是当revalidate()时图形没有更新?

时间:2013-05-15 22:58:50

标签: java multithreading swing jtextarea repaint

我正在制作一个模拟NHL选秀抽奖的程序,屏幕右侧应该有一个JTextField,并且左边的选项会被反弹。我创建了一个名为Ball的类,它实现了Runnable,并在我的主要DraftLottery类中作为Thread运行。但是,当我的draw方法调用repaint()时,JTextArea不会显示。我尝试将repaint()切换到revalidate(),但随后球没有移动。然后我尝试调用repaint()和revalidate(),但它的行为与repaint()相同。 以下是DraftLottery类的代码:

package ca.WiltzSports.DraftLottery;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

import ca.WiltzSports.DraftLottery.DraftBall.Ball;

public class DraftLottery extends JFrame {
public static final long serialVersionUID = 89L;

int teams;
String[] teamNames;
int[] balls;
JTextArea display;
JPanel screen;
JPanel animation;
List<String> entries;
public static List<Ball> ball;
String [] draftOrder;
Random rand;
int counter = 1;
javax.swing.Timer t;
public static int width = 1024;
public static int height = 768;
Graphics dbg;
Image dbImage;
int i = 0;

public DraftLottery(int teams, String[] teamNames, int[] balls){
    t = new javax.swing.Timer(2000, new ActionListener(){
        @Override
        public void actionPerformed(ActionEvent arg0) {
            draft();
        }   

    });
    ball = new ArrayList<Ball>();
    rand = new Random();
    this.teams = teams;
    this.teamNames = teamNames;
    this.balls = balls;
    this.screen = new JPanel();
    this.animation = new JPanel();
    display = new JTextArea(20,50);
    display.setBackground(Color.BLACK);
    display.setForeground(Color.YELLOW);
    draftOrder = new String[teams];
    this.entries = new ArrayList<String>();
    addTeamBalls();
    setSize(width,height);
    setVisible(true);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setTitle("Draft Lottery");
    setLayout(new BorderLayout());
    screen.add(display);
    add("East", new JScrollPane(display));
    add("West", animation);
    t.start();
}

@Override
public void paint(Graphics g){
    dbImage = createImage(getWidth(), getHeight());
    dbg = dbImage.getGraphics();
    Graphics2D g2d = (Graphics2D) dbg;
    draw(g2d);
    g.drawImage(dbImage, 0, 0, this);
    Ball.runBalls(ball);
}

public void draw(Graphics2D g2d){
    for (Ball b: ball){
        b.draw(g2d);
    }
    revalidate();
    repaint();
}

public void addTeamBalls(){
    for (int c = 0; c < teamNames.length; c++){
        for (int j = 0; j < balls[c]; j++){
            entries.add(teamNames[c]);
            ball.add(new Ball(rand.nextInt(width/2 - Ball.SIZE), rand.nextInt(height - Ball.SIZE), teamNames[c], i++));
        }
    }
}

public void draft(){
    int q = 0;
    String [] e = entries.toArray(new String[entries.size()]);
    String draftedTeam;
    int index = rand.nextInt(entries.size());
    draftedTeam = e[index];
    if(!draftedTeam.equals("X")){
        display.append("" + counter++ + ". " + draftedTeam + "\n");
        for(int c = 0; c < e.length; c++){
            if (e[c].equals(draftedTeam)){
                e[c] = "X";
            }
        }
        removeBalls();
    }else {
        boolean again = false;
        for (int c = 0; c < e.length; c++){
            if (!e[c].equals("X")){
                again = true;
            }
        }
        if(again){
        }else{
            t.stop();
            display.append("DRAFT LOTTERY COMPLETE");
        }
    }
    entries = Arrays.asList(e);
}

public void removeBalls(){
    Ball [] bs;
    int q = 0;
    for (Ball b: ball){
        if (b.getTeamName().equals("X")){
            continue;
        }else{
            q++;
        }
    }
    bs = new Ball[q];
    q = 0;
    for (Ball b: ball){
        if (!b.getTeamName().equals("X")){
            bs[q++] = b;
        }
    }
    ball = Arrays.asList(bs);
}

}

任何帮助都会非常感激。 非常感谢!

1 个答案:

答案 0 :(得分:3)

你有很多问题:

  1. 您已覆盖顶级容器的paint方法。这通常不推荐,因为它不是双缓冲的,并且可以通过动画和其他油漆更新产生闪烁。

  2. 您未能致电super.paintpaint mathod负责将所有子绘制方法拉到一起,包括paintComponents;但由于你不允许paint完成它的工作,所以它不会为你渲染这些组件。

  3. 相反,

    1. 创建自定义组件,从JPanel

    2. 扩展
    3. 将动画所需的所有逻辑添加到此组件。

    4. 覆盖它的paintComponent方法并将所有动画和自定义绘画添加到其中。

    5. 将其添加到框架中。

    6. 请查看Performing Custom PaintingPainting in AWT and Swing了解详情。