Witam,
mam problem z menu oraz rysowaniem w swing. menu jest pod tym co rysuje. moze najlepiej wytlumaczy to screen.
to ze w ogole bylo cos widac to dlatego bo najechalem na menu myszka i zaczelem klikac, menu sie pokazuje i chowa natychmiast.
moze teraz kod
public class Tablica extends JFrame implements KeyListener, WindowListener{
private void ustawMenu(){
for(int i=0; i< items.length; i++)
items[i].addActionListener(menual);
menus[0].add(items[0]);
menus[0].add(items[1]);
menus[1].add(items[2]);
JMenuBar mb = new JMenuBar();
mb.add(menus[0]);
mb.add(menus[1]);
mb.add(menus[2]);
this.setJMenuBar(mb);
}
public Tablica(){
start = false;
v = new Predkosciomierz();
skrzynia = new Skrzynia();
wyswietlacz = new Wyswietlacz();
timerczas = new Timer();
ustawMenu();
this.setTitle("Mitsubishi Lancer Evo X");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(920,420);
this.setVisible(true);
this.addKeyListener(this);
this.addWindowListener(this);
this.createBufferStrategy(2);
loop();
}
private void loop(){
while(1==1){
paint();
try {
Thread.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void paint(){
BufferStrategy bf = this.getBufferStrategy();
Graphics g = null;
try {
g = bf.getDrawGraphics();
ImageIcon ico = new ImageIcon("zegary.jpg");
Image img = ico.getImage();
g.drawImage(img, 0,0, null);
v.rysuj(g);
skrzynia.rysuj(g);
wyswietlacz.rysuj(g);
} finally {
g.dispose();
}
bf.show();
Toolkit.getDefaultToolkit().sync();
}
}
po zakomentowaniu funkcji paint() menu jest widoczne, wiec to jest przez moje rysowanie. Jak to rozwiazac?? W WinAPI takie rzeczy rozwiazywaly sie same a tutaj taki troche szok.
mam jeszcze kolejny problem z timerem.
public void rysuj( final Graphics g){
g.setColor(Color.red);
g.drawString("M "+Integer.toString((int)bieg), 410, 133);
double x2 = 647 + 110*Math.cos(Math.PI*stopnie/180);
double y2 = 195 + 110*Math.sin(Math.PI*stopnie/180);
g.drawLine(647, 195, (int)x2, (int)y2);
if (stopnie > 363 && migaj == true){
timerobroty = new Timer();
timerobroty.schedule(new TimerTask(){
public void run(){
g.fillOval(640, 170, 10, 10);
System.out.println("beep");
migaj = false;
}
},0, 1000);
}
else if (stopnie <= 363 && migaj == false){ migaj = true; timerobroty.cancel();}
}
Timer ogolnie dziala poprawnie, tylko nie rysuje nic w oknie :/
Zbedne linijki kodu nie sa pokazane.
Z gory dziekuje za pomoc