このグラフィックパネルを再描画しようとしていますが、再描画を呼び出すたびに更新されません。system.out.printlnに表示されるはずの値を確認すると、適切な値がログに出力されますが、画面のグラフィックが更新されません。何か提案/ヘルプはありますか?
私は3つのクラスを持っています(?)これは私の最初のクラスです
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import javax.swing.Timer;
public class ChromeNPlayerScreen extends JFrame implements ActionListener{
public void actionPerformed(ActionEvent e){
repaint();
}
public static void main(String[ ] args){
DrawScreen dPnl = new DrawScreen();
ChromeNPlayerScreen mScreen = new ChromeNPlayerScreen();
Keys keyPress = new Keys();
Timer update = new Timer(1000, mScreen);
// update.start();
int screenNum=1;
dPnl.importDialogue();
mScreen.addKeyListener(keyPress);
mScreen.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
mScreen.add(dPnl);
mScreen.setSize(600,600);;
mScreen.setVisible(true);
mScreen.setResizable(false);
mScreen.setLocation(200, 200);
}
}
これは私の2番目のクラスです
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
public class Keys extends KeyAdapter{
DrawScreen dPnl = new DrawScreen();
int scrnCount=0;
public void keyPressed(KeyEvent e){
int keyCode = e.getKeyCode();//Get key preseed
if (keyCode ==e.VK_Z) {
scrnCount++;
dPnl.setText(scrnCount);
}
}
}
そして最後に私の3番目のもの
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Font;
import java.awt.Graphics;
import java.net.URL;
import java.io.*;
import javax.swing.Timer;
public class DrawScreen extends JPanel {
String picPath = "pictures/", scriptPath = "dialogue/";
String out="ABC";
String[] speech = new String[39];
ClassLoader cl = DrawScreen.class.getClassLoader();
URL imgURL1 = cl.getResource(picPath+"welcomeBG.png"),imgURL2 = cl.getResource(picPath+"dialogBox.png"),
imgURL3 = cl.getResource(picPath+"Professor.png");
Toolkit tk = Toolkit.getDefaultToolkit();
Image imgBG = tk.createImage(imgURL1),
imgDialog = tk.createImage(imgURL2),
imgProfessor = tk.createImage(imgURL3);
int screenCount=1;
int scrn=1;
public void paintComponent(Graphics g) {
g.drawImage(imgBG,0,0,600,600,0,0,500,500, this);
g.drawImage(imgDialog,-5,480,595,565,0,0,500,91, this);
if (scrn==1)
g.drawImage(imgProfessor,200,50,375,475,0,0,340,748, this);
g.drawString(out, 25,515);
}
public void importDialogue(){
Keys keyPress = new Keys();
String [] fields; // array to store the "split" line read // individual field variables
BufferedReader in=null; //variable representing the bufferedreader
String line="A B 1"; //variable to read each line from the data file
File f=new File(scriptPath+"newGameScript.txt"); //variable reprsenting the data file
int count=1;
try{
in=new BufferedReader(new FileReader(f));
System.out.println("File Opening");
}
catch (FileNotFoundException e){
System.out.println("Problem finding File");
return;
}
while(line!=null){
try{
line=in.readLine();
if (line!=null){
fields=line.split(":");
speech[count]=(fields[0]);
count++;
}
}
catch (IOException e){
System.out.println("Problem reading data from file");
}
if (line!=null){}
out=speech[scrn];
}
try{
in.close();
System.out.println("Closing File");
}
catch (IOException e){
System.out.println("Problem Closing "+e);
}
}
public void setText(int num){
scrn=num;
importDialogue();
System.out.println(out);
repaint();
}
}
ご覧のとおり、Zキーを押すと、DrawScreenのoutが次の行で更新されるはずです。よくあるはずですが、そうではありません。テキストファイルの最初の行である「こんにちは!」とだけ表示されます。