java - 这不会重新绘制,我不知道为什么 - Java

标签 java swing repaint

我一直在尝试重新绘制此图形面板,但每次我调用重新绘制时,它都不会更新。我检查了应该在 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);
   }
 }

这是我的第二堂课

 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);
     }
   }
 }

最后是我的第三个

 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 上的输出。本来应该如此,但事实并非如此。它只是说文本文件中的第一行“嗨,那里!”。

最佳答案

只需使用浏览器在此页面上的文本搜索功能,您就可以找到主要问题,甚至可能是问题本身。我希望您使用浏览器搜索字符串“new DrawScreen()”,当然不带引号。忽略我的帖子,您会发现它在上面的代码中出现了两次,一次在 ChromeNPlayerScreen 类中:

public class ChromeNPlayerScreen extends JFrame implements ActionListener{
   public void actionPerformed(ActionEvent e){
     repaint();
   }
   public static void main(String[ ] args){
     DrawScreen dPnl = new DrawScreen(); // ****
     // ...

进入 Keys 类后:

 public class Keys extends KeyAdapter{
   DrawScreen dPnl = new DrawScreen();
   // ....

了解,每次调用此函数时,您都在创建一个唯一且独立的 DrawScreen 对象,并且对 Keys 类中未显示的 DrawScreen 对象进行更改绝对不会对 ChromeNPlayerScreen 类中显示的 DrawScreen 对象产生任何影响.

解决方案是仅创建一个 DrawString 实例一次(可能在 ChromeNPlayerScreen 类中),并传递这个相同实例 通过构造函数参数进入您的 Key 类。这样,您对 Keys 类持有的实例所做的更改将反射(reflect)在 ChromeNPlayerScreen 类中显示的同一个对象中。

 public class Keys extends KeyAdapter{
   DrawScreen dPnl;

   public Keys(DrawString dPnl) {
     this.dPnl = dPnl;
   }

public static void main(String[ ] args){
     DrawScreen dPnl = new DrawScreen();
     ChromeNPlayerScreen mScreen = new ChromeNPlayerScreen();
     Keys keyPress = new Keys(dPnl);

关于java - 这不会重新绘制,我不知道为什么 - Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13774613/

相关文章:

java - Java中线程用户时间和线程CPU时间的区别

java - 从不扩展 Activity 的类调用 Activity

Java:联立方程

Java:如何拖放到 TrayIcon 上?

java - 如何在绘制多边形后清除 Netbeans 中的 JFrame

javascript - 在 JavaScript 运行之前重绘 CSS(以显示进度指示器)

java - Netty 中的业务逻辑?

Java Swing - 获取鼠标悬停的对象

java - 如何从菜单中打开 JPanel?

Android 自定义 View 不重绘,虽然正在调用 onDraw