在由单独的GUI更改的JFrame画布上使用paintComponent()

时间:2017-11-15 02:36:59

标签: java swing jframe paintcomponent jcomponent

我正在尝试创建一个使用两个JFrame的JComponent应用程序,一个框架带有可变滑块和文本字段,用于在第二个框架上显示烟花。当"火"按下按钮,应该出现烟花的渲染。但是,通过放置策略性打印语句,我发现即使包含代码的条件语句满足,我的paintComponent()方法也不会运行。我还仔细检查了所有其他方法,以确保在正确的时间生成正确的值。在查看了我能找到的所有JComponent文献和问题之后,我恐怕无法让它工作 - 这个问题很可能源于我对图书馆的不熟悉。话虽如此,任何建议,无论多么初级,都将受到高度赞赏。简短的代码如下:

*摇摆计时器也可能是问题,我不确定我是否正确使用它

[fireworksCanvas.java]

try:
    KeywordFileName=input('Input keyword file name: ')
    KeywordFile = open(KeywordFileName, 'r')
except FileNotFoundError:
    print('The file you entered does not exist or is not in the directory')
    exit()
KeyLine = KeywordFile.readline()
while KeyLine != '':
    if list != []:
        KeyLine = KeywordFile.readline()
        KeyLine = KeyLine.rstrip()
        list = KeyLine.split(',')
        list[1] = int(list[1])
        print(list)
    else:
        break

try:
    TweetFileName = input('Input Tweet file name: ')
    TweetFile = open(TweetFileName, 'r')
except FileNotFoundError:
    print('The file you entered does not exist or is not in the directory')
    exit()

TweetLine = TweetFile.readline()
while TweetLine != '':
    TweetLine = TweetFile.readline()
    TweetLine = TweetLine.rstrip()

[GUI.java]

    public class fireworkCanvas extends JComponent implements ActionListener{

private static final long serialVersionUID = 1L;
private ArrayList<Ellipse2D> nodes = new ArrayList<Ellipse2D>();
private ArrayList<Line2D> cNodes = new ArrayList<Line2D>();
private ArrayList<QuadCurve2D> bCurves = new ArrayList<QuadCurve2D>();
private int[] arcX; 
private int[] arcY;
private Color userColor;
private Random rand = new Random();
private int shellX, shellY, fType, theta, velocity;
private Timer timer;
private int time;
private double g = -9.8; //gravity in m/s
public boolean explosivesSet;

public fireworkCanvas() {
    time = rand.nextInt(3000) + 2000;
    timer = new Timer(time, this); // 5 seconds
    timer.start();
    fType = 0;
}

@Override 
public void paintComponent(Graphics g){

    if (explosivesSet) {
        System.out.println("fType" + fType);
        super.paintComponent(g);
        Graphics2D g2D = (Graphics2D) g;

        g.setColor(Color.BLACK);
        g.drawPolyline(arcX, arcY, arcX.length);

        for (Ellipse2D e : nodes) {
            System.out.println("painting nodes"); // NEVER PRINTS
            g.setColor(userColor);
            g.fillOval(shellX + (int) e.getX(), shellY + (int) e.getY(), (int) e.getWidth(), (int) e.getHeight());
        }
        for (Line2D l: cNodes) {
            System.out.println("painting cNodes"); // NEVER PRINTS
            g.setColor(determineColor("l"));
            g.drawLine(shellX + (int) l.getX1(), shellY + (int) l.getY1(), shellX + (int) l.getX2(), shellY + (int) l.getY2());
        }
        for (QuadCurve2D c: bCurves) {
            System.out.println("painting curves"); // NEVER PRINTS
            g.setColor(determineColor("c"));
            g2D.draw(c);
        }
    }
}

public Color determineColor(String type) {

    // returns color
}

public void setExplosives() {

    if (fType != 5 && fType != 0) {

        nodes.clear(); // clears three array lists with FW components 
        cNodes.clear(); // these are the components to paint for the
        bCurves.clear(); // firework explosion graphic
        setArc(); // stores path of shell for a polyLine to be drawn

        // builds and generates components for FW based on type chosen (fType)
        setExplosivesSet(true);
        repaint();
    }
}

public void setArc() {
    // builds int[] for shellX, shellY
}

@Override
public void actionPerformed(ActionEvent e) {
     // nothing is here??
     // should I use the action performed in some way?
}

1 个答案:

答案 0 :(得分:2)

首先:

public fireworkCanvas()

类名应以大写字母开头。代码中的所有其他类都遵循此规则。通过实例学习。

private Choice fireworkChooser = new Choice();

选择是一个AWT组件,不要在Swing应用程序中混合使用AWT组件。使用JComboBox

  

我的paintComponent()方法没有运行

fireworkCanvas canvas = new fireworkCanvas();
canvasFrame.pack();
canvasFrame.add(canvas);

您将画布添加到帧后()包装(),因此画布的大小为(0,0),并且没有任何颜色可供绘制。

应该在pack()之前将画布添加到框架中,并且应该在FireworkCanvas类中实现getPreferredSize(),以便pack()方法可以正常工作。

阅读Custom Painting上Swing教程中的部分,了解基础知识和工作示例,以帮助您入门。