Reputation: 1
I am trying to draw objects in Java and I am having issues in the TimerListener class where it "cannot resolve symbol paintComponent"(or symbol g). I'm guessing this is simple but I can't seem to solve it?
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();
//}
}
//}
Upvotes: 0
Views: 74
Reputation: 324108
First you should not have methods like chooseOn() and choseOff(). Instead you should do something like:
public void setChooseOn(boolean chooseOn)
{
this.chooseOn = chooseOn;
repaint();
}
public Boolean getChooseOn()
{
return chooseOn;
}
That is whenever you want to change a property of your class you should have a getter
and a setter
for the property.
I am having issues in the TimerListener class
You can't do painting in a listener class. Painting is only done in the paintComponent() method of the panel.
So the code in your Timer listener class would be similar to the code in your OffListener and OnListener classes. That is you invoke the method that changes your property.
So in your case the code might just look like:
beaconPanel.setChooseOn( ! beconPanel.getChooseOn() );
This code will toggle the property between true/false every time the Timer is invoked. There is no need to invoke repaint() because the setChooseOn(...) method does it for you.
Upvotes: 1