Witam!!!
Mam problem z aplikacją, którą piszę. Aplikacja ma przedstawiać zegar 2d utworzony na formie. Co sekunde powinna się przesuwać wskazówka. W programie wykorzystałem Timer niestety nie wiem dlaczego wywołanie funkcji w postaci: comp.paint(comp.getGraphics()); w celu przerysowywania sceny nie działa. Może ktoś wie dlaczego? Oto mój mail [email protected], na wszelki wypadek...
Z góry dziękuję za wszelką pomoc i uwagi. Pozdrawiam
Poniżej zamieszczam cały kod aplikacji:
Klasa nr 1:
import java.awt.;
import java.awt.event.;
import java.util.Date;
import javax.swing.*;
public class Zegarek {
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
JFrame frame = new BounceFrame();
ActionListener słuchacz = new BounceFrame();
Timer czas = new Timer(1000, słuchacz);
czas.start();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
}
class BounceFrame extends JFrame implements ActionListener
{
public BounceFrame()
{
setSize(DEFAULT_WIDTH, DEFAULT_HIGHT);
setTitle("Zegar");
comp = new BallComponent();
add(comp, BorderLayout.CENTER);
ball = new Ball();
comp.add(ball);
}
static void setSekunda()
{
sekunda +=1;
if(sekunda==12) sekunda = 0;
}
public void actionPerformed(ActionEvent zdarzenie)
{
ball.move(sekunda);
<span style="color: red">comp.paint(comp.getGraphics());// to wywołanie nie powoduje przerysowania sceny</span>
setSekunda();
Toolkit.getDefaultToolkit().beep();
}
private BallComponent comp;
private Ball ball;
public static final int DEFAULT_WIDTH = 500;
public static final int DEFAULT_HIGHT = 500;
public static final int STEPS = 12;
public static final int DELAY = 1000;
public static int sekunda = 1;
}
Klasa nr 2:
import java.awt.geom.*;
public class Ball
{
public void move(int sekunda)
{
if(sekunda==1) { dx = 330; dy=110; };
if(sekunda==2) { dx = 385; dy = 170; };
if(sekunda==3) { dx = 410; dy = 250; };
if(sekunda==4) { dx = 385; dy = 330; };
if(sekunda==5) { dx = 330; dy = 390; };
if(sekunda==6) { dx = 250; dy = 410; };
if(sekunda==7) { dx = 170; dy = 390; };
if(sekunda==8) { dx = 115; dy = 330; };
if(sekunda==9) { dx = 90; dy = 250; };
if(sekunda==10) { dx = 115; dy = 170; };
if(sekunda==11) { dx = 170; dy = 110; };
if(sekunda==12) { dx = 250; dy = 90; };
}
public Line2D getShape()
{
return new Line2D.Double(x, y, dx, dy);
}
private double x = 250;
private double y = 250;
private double dx = 250;
private double dy = 90;
}
Klasa nr 3:
import java.awt.;
import java.awt.geom.;
import java.util.;
import javax.swing.;
public class BallComponent extends JPanel
{
public void add(Ball b)
{
balls.add(b);
}
public void paintComponent(Graphics g)
{
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2 = (Graphics2D) g;
g2.setPaint(Color.blue);
//godziny na tarczy
g2.drawString("3", 430, 250);
g2.drawString("6", 250, 440);
g2.drawString("9", 60, 250);
g2.drawString("12", 250, 70);
//rysowanie obręczy tarczy
double radius = 170;
Ellipse2D koło = new Ellipse2D.Double();
koło.setFrameFromCenter(250, 250, 250 + radius, 250 + radius);
g2.draw(koło);
g2.setPaint(Color.RED);
for (Ball b : balls)
{
g2.draw(b.getShape());
}
}
private ArrayList<Ball> balls = new ArrayList<Ball>();
}
</ort>