Java 更新图形
我一直有一些问题,这段代码,图形似乎不想更新一些奇怪的原因。我不知道如何解决这个问题,试图解决这个问题真的很烦人,请帮助!代码如下:Java 更新图形,java,swing,Java,Swing,我一直有一些问题,这段代码,图形似乎不想更新一些奇怪的原因。我不知道如何解决这个问题,试图解决这个问题真的很烦人,请帮助!代码如下: 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
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
}
}
}
package org.jeopredy;
导入java.awt.*;
导入java.awt.event.MouseEvent;
导入java.awt.event.MouseListener;
导入java.util.ArrayList;
导入javax.swing.*;
公共类主框架{
公用干管(){
超级(“Jeopredy”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这个.setSize(517640);
此.setVisible(true);
this.add(newpanel());
这个.addMouseListener(新面板());
}
公共静态void main(字符串参数[]){
新的Main();
}
私有类面板扩展JPanel实现MouseListener{
公共矩形[][]瓷砖=新矩形[5][6];
公共布尔启动、问题、答案;
公共字符串[][]类别={{“众议院”,“代表”},
{“参议院”},{“总统”},{“副总统”},
{“最高法院”};
公共字符串[][]问题={{“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" } };
公共字符串[][]答案={{{,'','','','','},
{ "", "", "", "", "" }, { "", "", "", "", "" },
{ "", "", "", "", "" }, { "", "", "", "", "" } };
公共点q,a;
使用的公共ArrayList=新ArrayList();
公共事务委员会(){
开始=真;
对于(int x=0;x<500;x+=100){
对于(int y=0;y<600;y+=100){
瓷砖[x/100][y/100]=新矩形(x,y,100100);
}
}
重新油漆();
}
@凌驾
公共空间涂料(图g){
//超级组件(g);
//g、 setColor(Color.white);
System.out.println(“开始:+Start+”问题:+question
+回答:“+回答”;
如果(启动){
int i=0;
for(字符串[]字符串:类别){
如果(str.length==1){
g、 抽绳(str[0],tiles[i][0].x,tiles[i][0].y+20);
}否则{
g、 抽绳(str[0],tiles[i][0].x,tiles[i][0].y+20);
g、 抽绳(str[1],tiles[i][0].x,tiles[i][0].y+30);
}
i++;
}
对于(矩形[]矩形:平铺){
for(矩形r:rect){
g、 drawRect(r.x、r.y、r.宽度、r.高度);
}
}
}若否(问题){
//System.out.println(“QUeStionJOSNLN”);
g、 抽绳(问题[q.x][q.y],100100);
}
重新油漆();
}
@凌驾
公共无效鼠标单击(鼠标事件arg0){
点p=新点(arg0.getPoint().x-17,arg0.getPoint().y-40);
//重新油漆();
如果(启动){
对于(int x=0;x
paint()
。我认为这是你问题的主要原因SwingUtilities.invokeLater()
加载GUI私有类面板扩展JPanel实现
类应以大写字母开头,如MyPanel
paint()
。我认为这是你问题的主要原因SwingUtilities.invokeLater()
加载GUI私有类面板扩展JPanel实现
类应以大写字母开头,如MyPanel
当我四处挖掘时,让我们从
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
}
这永远不会将鼠标事件发送到屏幕上的面板。您需要在屏幕上的面板
上注册鼠标侦听器
你最好还是这样做
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
}
}
}
public类主框架{
公用干管(){
超级(“Jeopredy”);
此.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
这个.setSize(517640);
添加(新面板());
此.setVisible(true);
}
公共静态void main(字符串参数[]){
EventQueue.invokeL
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
}
}
}