调用paint组件

时间:2015-03-27 12:18:36

标签: java

我正在尝试用Java绘制对象,而我在TimerListener类中遇到问题,它无法解析符号paintComponent"(或符号g)。我猜这很简单,但我似乎无法解决它?

import javax.swing.*;
import java.awt.*;
import java.awt.geom.Ellipse2D;


public class Beacon extends JPanel {
    private boolean choseOn = false;

   // Timer timer = new Timer(1000, onListener );

    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D) g;
        Rectangle box1 = new Rectangle(165, 75, 10, 25);
        g2.draw(box1);
        Rectangle box2 = new Rectangle(165, 100, 10, 25);
        g2.draw(box2);
        Rectangle box3 = new Rectangle(165, 125, 10, 25);
        g2.draw(box3);
        Rectangle box4 = new Rectangle(165, 150, 10, 25);
        g2.draw(box4);
        Rectangle box5 = new Rectangle(165, 175, 10, 25);
        g2.draw(box5);
        Rectangle box6 = new Rectangle(165, 200, 10, 25);
        g2.draw(box6);
        Rectangle box7 = new Rectangle(165, 225, 10, 25);
        g2.draw(box7);
        Rectangle box8 = new Rectangle(165, 250, 10, 25);
        g2.draw(box8);
        Rectangle box9 = new Rectangle(165, 275, 10, 25);
        g2.draw(box9);
        Rectangle box10 = new Rectangle(165, 300, 10, 25);
        g2.draw(box10);
        g2.setColor(Color.BLACK);
        g2.fill(box1);
        g2.fill(box3);
        g2.fill(box5);
        g2.fill(box7);
        g2.fill(box9);
        Ellipse2D.Double light = new Ellipse2D.Double(145, 26, 50, 50);
        if (choseOn) {
            //timer.setInitialDelay(1000);
            //timer.start();
            g2.setPaint(Color.GRAY);
            g2.fill(light);


        } else {
            g2.setColor(Color.ORANGE);
            g2.fill(light);
        }
    }
    public void chooseOn() { choseOn = false; }
    public void chooseOff() { choseOn = true; }

}

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;


public class BeaconViewer extends JFrame {
    Timer timer;
    JButton off = new JButton("OFF");
    JButton on = new JButton("ON");
    JPanel panel1 = new JPanel();
    Beacon beaconPanel = new Beacon();

    public BeaconViewer() {
        panel1.add(off);
        panel1.add(on);
        this.add(panel1, BorderLayout.SOUTH);
        this.add(beaconPanel, BorderLayout.CENTER);
        //off.addActionListener(new offListener());
        //on.addActionListener(new onListener());

    }

    public static void main(String[] args) {
        JFrame beaconFrame = new BeaconViewer();
        beaconFrame.setTitle("Circle-Square");
        beaconFrame.setSize(350, 450);
        beaconFrame.setDefaultCloseOperation((JFrame.EXIT_ON_CLOSE));
        beaconFrame.setVisible(true);
    }

    class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {

            Ellipse2D.Double light = new Ellipse2D.Double(145, 26, 50, 50);
            super.paintComponent(g);
            Graphics2D g2 = (Graphics2D) g;
            g2.setPaint(Color.ORANGE);
            g2.fill(light);
            repaint();
        }
    }


    //class offListener implements ActionListener {
      //  public void actionPerformed(ActionEvent e) {
        //    beaconPanel.chooseOff();
         //   repaint();
       // }
    //}
    //class onListener implements ActionListener {
      //  public void actionPerformed(ActionEvent e) {
        //    beaconPanel.chooseOn();
          //  repaint();
        //}
    }
//}

1 个答案:

答案 0 :(得分:1)

首先,您不应该使用chooseOn()和chooseOff()等方法。相反,你应该做类似的事情:

public void setChooseOn(boolean chooseOn)
{
    this.chooseOn = chooseOn;
    repaint();
}

public Boolean getChooseOn()
{
    return chooseOn;
}

也就是说,只要您想要更改班级的属性,就应该为该属性设置gettersetter

  

我在TimerListener类中遇到问题

你不能在监听器类中绘画。绘画仅在面板的paintComponent()方法中完成。

因此,Timer侦听器类中的代码与OffListener和OnListener类中的代码类似。那就是你调用改变你的属性的方法。

因此,在您的情况下,代码可能看起来像:

beaconPanel.setChooseOn( ! beconPanel.getChooseOn() );

每次调用Timer时,此代码都会在true / false之间切换属性。无需调用repaint(),因为setChooseOn(...)方法可以为您完成。