gpt4 book ai didi

java - 调用绘制组件

转载 作者:行者123 更新时间:2023-12-01 11:44:05 24 4
gpt4 key购买 nike

我正在尝试在 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();
//}
}
//}

最佳答案

首先你不应该有像chooseOn()和chooseOff()这样的方法。相反,你应该这样做:

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

public Boolean getChooseOn()
{
return chooseOn;
}

也就是说,每当您想要更改类的属性时,您都应该为该属性提供一个 getter 和一个 setter

I am having issues in the TimerListener class

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

因此,Timer 监听器类中的代码将与 OffListener 和 OnListener 类中的代码类似。也就是说,您调用更改属性的方法。

因此,在您的情况下,代码可能如下所示:

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

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

关于java - 调用绘制组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29300497/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com