1

私はこのコードでいくつかの問題を抱えていました.グラフィックは奇妙な理由で更新したくないようです. これを修正する方法がわかりません。これを修正しようとするのは本当に面倒でした。助けてください! コードは次のとおりです。

package org.jeopredy;

import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.*;

public class Main extends JFrame {

public Main() {
    super("Jeopredy");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(517, 640);
    this.setVisible(true);
    this.add(new panel());
    this.addMouseListener(new panel());
}

public static void main(String args[]) {
    new Main();
}

private class panel extends JPanel implements MouseListener {

    public Rectangle[][] tiles = new Rectangle[5][6];

    public boolean start, question, answer;

    public String[][] Catagories = { { "House of", "representitives" },
            { "Senate" }, { "President" }, { "Vice president" },
            { "Supreme court" } };

    public String[][] Questions = { { "1", "2", "3", "4", "5" },
            { "1", "2", "3", "4", "5" }, { "1", "2", "3", "4", "5" },
            { "1", "2", "3", "4", "5" }, { "1", "2", "3", "4", "5" } };

    public String[][] Answers = { { "", "", "", "", "" },
            { "", "", "", "", "" }, { "", "", "", "", "" },
            { "", "", "", "", "" }, { "", "", "", "", "" } };

    public Point q, a;

    public ArrayList<Point> used = new ArrayList<Point>();

    public panel() {
        start = true;
        for (int x = 0; x < 500; x += 100) {
            for (int y = 0; y < 600; y += 100) {
                tiles[x / 100][y / 100] = new Rectangle(x, y, 100, 100);
            }
        }
        repaint();
    }

    @Override
    public void paint(Graphics g) {
        // super.paintComponent(g);
             //g.setColor(Color.white);

         System.out.println("Start: " + start + " question: " + question
         + " answer: " + answer);
        if (start) {
            int i = 0;
            for (String[] str : Catagories) {
                if (str.length == 1) {
                    g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                } else {
                    g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                    g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
                }
                i++;
            }
            for (Rectangle[] rect : tiles) {
                for (Rectangle r : rect) {
                    g.drawRect(r.x, r.y, r.width, r.height);
                }
            }
        } else if (question) {
            // System.out.println("QUeStionJOSNLN");
            g.drawString(Questions[q.x][q.y], 100, 100);
        }
        repaint();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
        Point p = new Point(arg0.getPoint().x - 17, arg0.getPoint().y - 40);
        // repaint();
        if (start) {
            for (int x = 0; x < tiles.length; x++) {
                for (int y = 0; y < tiles[x].length; y++) {
                    if (tiles[x][y].contains(p) && y != 0) {
                        question = true;
                        start = false;
                        answer = false;
                        q = new Point(x, y - 1);
                        used.add(q);
                    }
                }
            }
        } else if (question) {
            answer = true;
            question = false;
            start = false;
        } else if (answer) {
            start = true;
            answer = false;
            question = false;
        }

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseExited(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mousePressed(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
        // TODO Auto-generated method stub

    }
}
}
4

2 に答える 2

3
  1. まず第一に、あなたは決してオーバーライドすることになっていないpaint()それがあなたの問題の主な理由だと思います。

Swingのpaint、paintComponent、paintComponentsの違い

  1. SwingUtilities.invokeLater()GUIのロードに使用

  2. JFrameを拡張しないでください。これはまったく推奨されておらず、上記のコードでは役に立ちません。

  3. private class panel extends JPanel implementsクラスは次のような大文字で始める必要がありますMyPanel

于 2013-01-16T05:23:04.907 に答える
3

私が掘り下げている間、始めましょう...

public void paint(Graphics g) { // <<-- This is bad
    // super.paintComponent(g);
    //g.setColor(Color.white);

    System.out.println("Start: " + start + " question: " + question+ " answer: " + answer);
    if (start) {
        int i = 0;
        for (String[] str : Catagories) {
            if (str.length == 1) {
                g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
            } else {
                g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
                g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
            }
            i++;
        }
        for (Rectangle[] rect : tiles) {
            for (Rectangle r : rect) {
                g.drawRect(r.x, r.y, r.width, r.height);
            }
        }
    } else if (question) {
        // System.out.println("QUeStionJOSNLN");
        g.drawString(Questions[q.x][q.y], 100, 100);
    }
    repaint(); // <<-- This is bad
}

オーバーライドは避けてくださいpaint。理由はたくさんありますが、基本的には、ペイントチェーン全体を完全に破壊しました。Swingでのペイントは複雑で、非常に簡単に台無しになります。を呼び出す必要がありますsuper.paint(g)

オーバーライドする方が良いpaintComponentです、電話することを忘れないでくださいsuper.paintComponent!!

ペイントメソッド内からUIを変更しないでください。ペイントは通常、再ペイントリクエストに応答して呼び出されます。別のリクエストを引き起こす可能性のあるUIを変更すると、CPUの過負荷とリソースの占有の滑りやすい坂道を下って送られます...

更新しました

this.add(new panel());
this.addMouseListener(new panel());

これにより、画面上にあるパネルにマウスイベントが送信されることはありません。画面に表示されているものに対してマウスリスナーを登録する必要がありpanelます。

あなたは実際にこれを行う方が良いでしょう...

public panel() {
    addMouseListener(this);
    //...continue...
}

アップデート#2

ペイントはステートレスであることを覚えておいてください。以前にペイントされたものは、次のペイントサイクルには存在しません。塗装が必要なものはすべて、各サイクルで塗装する必要があります。

アップデート#3

私はあなたのプログラムが何を達成しようとしているのかについて少し困惑していますが、変更を見て、それが役立つかどうかを確認してください...

public class Main extends JFrame {

  public Main() {
    super("Jeopredy");
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    this.setSize(517, 640);
    this.add(new panel());
    this.setVisible(true);
  }

  public static void main(String args[]) {
    EventQueue.invokeLater(new Runnable() {
      @Override
      public void run() {
        new Main();
      }
    });
  }

  private class panel extends JPanel implements MouseListener {

    public Rectangle[][] tiles = new Rectangle[5][6];
    public boolean start, question, answer;
    public String[][] Catagories = {{"House of", "representitives"},
      {"Senate"}, {"President"}, {"Vice president"},
      {"Supreme court"}};
    public String[][] Questions = {{"1", "2", "3", "4", "5"},
      {"1", "2", "3", "4", "5"}, {"1", "2", "3", "4", "5"},
      {"1", "2", "3", "4", "5"}, {"1", "2", "3", "4", "5"}};
    public String[][] Answers = {{"", "", "", "", ""},
      {"", "", "", "", ""}, {"", "", "", "", ""},
      {"", "", "", "", ""}, {"", "", "", "", ""}};
    public Point q, a;
    public ArrayList<Point> used = new ArrayList<Point>();

    public panel() {
      addMouseListener(this);
      start = true;
      for (int x = 0; x < 500; x += 100) {
        for (int y = 0; y < 600; y += 100) {
          tiles[x / 100][y / 100] = new Rectangle(x, y, 100, 100);
        }
      }
      repaint();
    }

    @Override
    protected void paintComponent(Graphics g) {
      super.paintComponent(g); 

      System.out.println("Start: " + start + " question: " + question
          + " answer: " + answer);
      if (start) {
        int i = 0;
        for (String[] str : Catagories) {
          if (str.length == 1) {
            g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
          } else {
            g.drawString(str[0], tiles[i][0].x, tiles[i][0].y + 20);
            g.drawString(str[1], tiles[i][0].x, tiles[i][0].y + 30);
          }
          i++;
        }
        for (Rectangle[] rect : tiles) {
          for (Rectangle r : rect) {
            g.drawRect(r.x, r.y, r.width, r.height);
          }
        }
      } else if (question) {
        // System.out.println("QUeStionJOSNLN");
        g.drawString(Questions[q.x][q.y], 100, 100);
      }
//      repaint();
    }

    @Override
    public void mouseClicked(MouseEvent arg0) {
//      Point p = new Point(arg0.getPoint().x - 17, arg0.getPoint().y - 40);
      Point p = arg0.getPoint();
      // repaint();
      if (start) {
        for (int x = 0; x < tiles.length; x++) {
          for (int y = 0; y < tiles[x].length; y++) {
            if (tiles[x][y].contains(p) && y != 0) {
              question = true;
              start = false;
              answer = false;
              q = new Point(x, y - 1);
              used.add(q);
            }
          }
        }
      } else if (question) {
        answer = true;
        question = false;
        start = false;
      } else if (answer) {
        start = true;
        answer = false;
        question = false;
      }

      repaint();

    }

    @Override
    public void mouseEntered(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mouseExited(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mousePressed(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }

    @Override
    public void mouseReleased(MouseEvent arg0) {
      // TODO Auto-generated method stub
    }
  }
}
于 2013-01-16T05:26:52.630 に答える