Java repaint() 无法调用
Java repaint() not working on call
我想知道为什么 repiant() 方法不再按预期工作...例如:
public class Main extends JPanel implements ActionListener, MouseListener,MouseMotionListener{
private ArrayList<Node> nodes;
private ArrayList<Edge> edges;
private boolean AddNode;
private int no_Of_Nodes;
private int width = 30, height = 30;
public static void main(String[] args){
Main M = new Main();
M.Start();
}
public void Start() {
nodes = new ArrayList<Node>();
edges = new ArrayList<Edge>();
JFrame f = new JFrame("SFG");
JPanel main_Panel = new JPanel(new BorderLayout());
JPanel buttons = new JPanel();//Buttons Containser
JPanel draw = new JPanel();
ArrayList<JButton> bs = new ArrayList<JButton>();
JButton b1 = new JButton("Add Node");
b1.addActionListener(new Add_Node());
JButton b2 = new JButton("Add Edge");
b2.addActionListener(new Add_Edge());
JButton b3 = new JButton("Add Arc");
b3.addActionListener(new Add_Arc());
JButton b4 = new JButton("Clear all");
b4.addActionListener(new Clear());
JButton b5 = new JButton("Solve");
b5.addActionListener(new Solve());
Bs.add(b1);
Bs.add(b2);
Bs.add(b3);
Bs.add(b4);
Bs.add(b5);
for (int i = 0; i < bs.size(); i++) {
Buttons.add(bs.get(i));
}
Buttons.setBackground(Color.GRAY);
main_Panel.add(Buttons,BorderLayout.SOUTH);
draw.setBackground(Color.darkGray);
draw.addMouseMotionListener(this);
draw.addMouseListener(this);
main_Panel.add(Draw);
main_Panel.setBackground(Color.GRAY);
f.add(main_Panel);
f.setSize(1024, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
这些是方法
public void actionPerformed(ActionEvent arg0) {
this.repaint();
}
public class Add_Node implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Add Node");
addNode = true;
}
}
现在,当我添加节点并调用重新绘制时,绘制区域中没有任何东西出现:
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
if(addNode){
addNode(arg0);
addNode = !addNode;
}
System.out.println(nodes.size());
this.repaint();
}
private void addNode(MouseEvent arg0) {
// TODO Auto-generated method stub
int x = arg0.getX();
int y = arg0.getY();
Node n = new Node(No_Of_Nodes++);
n.setX_Pos(X);
System.out.println(x + " " + y);
n.setX_Pos(Y);
nodes.add(n);
}
那是我的 paint() 方法,它不再起作用了
public void paint(Graphics g){
super.paintComponents(g);
FontMetrics f = g.getFontMetrics();
int nodeHeight = Math.max(height, f.getHeight());
System.out.println("In repaint");
for (Node n : nodes) {
System.out.println(n.getX_Pos() + " " + n.getY_Pos());
int nodeWidth = Math.max(width, f.stringWidth(Integer.toString(n.getNode_ID()))+width/2);
g.setColor(Color.white);
g.fillOval(n.getX_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.setColor(Color.black);
g.drawOval(n.getY_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.drawString(Integer.toString(n.getNode_ID()) , n.getX_Pos()-f.stringWidth(Integer.toString(n.getNode_ID()))/2, n.getY_Pos()+f.getHeight()/2);
}
}
TIA,很抱歉问了这么长的问题 :)
- 在您的
Start
方法中,您实际上从未将 Main
添加到 JFrame
。根据您提供的断章取义的代码片段,我只能 "assume" 您正在覆盖 Main
class 的 paint
,这意味着,paint
永远不会被调用,因为它实际上没有附加到可显示组件
Start
不应创建 JFrame
。您应该创建一个 Main
的实例,然后将其添加到一个 JFrame
的实例中,两者应该是分开的。应创建 Main
并将其添加到 Main
本身
- 你不应该覆盖
paint
(作为一般规则),但你绝对不应该通过调用其他 paint
方法之一来规避绘画过程,比如 paintComponents
.相反(再次 "assuming" 你在 Main
中覆盖 paint
),你应该覆盖 paintComponent
方法并调用 super.paintComponent
在你做任何自定义绘画之前
查看 Performing Custom Painting and Painting in AWT and Swing 了解更多详情。
此外,您可能希望通读 Code Conventions for the Java TM Programming Language,这将使人们更容易阅读您的代码,您也可以更轻松地阅读其他人
我想知道为什么 repiant() 方法不再按预期工作...例如:
public class Main extends JPanel implements ActionListener, MouseListener,MouseMotionListener{
private ArrayList<Node> nodes;
private ArrayList<Edge> edges;
private boolean AddNode;
private int no_Of_Nodes;
private int width = 30, height = 30;
public static void main(String[] args){
Main M = new Main();
M.Start();
}
public void Start() {
nodes = new ArrayList<Node>();
edges = new ArrayList<Edge>();
JFrame f = new JFrame("SFG");
JPanel main_Panel = new JPanel(new BorderLayout());
JPanel buttons = new JPanel();//Buttons Containser
JPanel draw = new JPanel();
ArrayList<JButton> bs = new ArrayList<JButton>();
JButton b1 = new JButton("Add Node");
b1.addActionListener(new Add_Node());
JButton b2 = new JButton("Add Edge");
b2.addActionListener(new Add_Edge());
JButton b3 = new JButton("Add Arc");
b3.addActionListener(new Add_Arc());
JButton b4 = new JButton("Clear all");
b4.addActionListener(new Clear());
JButton b5 = new JButton("Solve");
b5.addActionListener(new Solve());
Bs.add(b1);
Bs.add(b2);
Bs.add(b3);
Bs.add(b4);
Bs.add(b5);
for (int i = 0; i < bs.size(); i++) {
Buttons.add(bs.get(i));
}
Buttons.setBackground(Color.GRAY);
main_Panel.add(Buttons,BorderLayout.SOUTH);
draw.setBackground(Color.darkGray);
draw.addMouseMotionListener(this);
draw.addMouseListener(this);
main_Panel.add(Draw);
main_Panel.setBackground(Color.GRAY);
f.add(main_Panel);
f.setSize(1024, 600);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setLocationRelativeTo(null);
f.setVisible(true);
}
这些是方法
public void actionPerformed(ActionEvent arg0) {
this.repaint();
}
public class Add_Node implements ActionListener{
public void actionPerformed(ActionEvent e) {
System.out.println("Add Node");
addNode = true;
}
}
现在,当我添加节点并调用重新绘制时,绘制区域中没有任何东西出现:
public void mouseClicked(MouseEvent arg0) {
// TODO Auto-generated method stub
if(addNode){
addNode(arg0);
addNode = !addNode;
}
System.out.println(nodes.size());
this.repaint();
}
private void addNode(MouseEvent arg0) {
// TODO Auto-generated method stub
int x = arg0.getX();
int y = arg0.getY();
Node n = new Node(No_Of_Nodes++);
n.setX_Pos(X);
System.out.println(x + " " + y);
n.setX_Pos(Y);
nodes.add(n);
}
那是我的 paint() 方法,它不再起作用了
public void paint(Graphics g){
super.paintComponents(g);
FontMetrics f = g.getFontMetrics();
int nodeHeight = Math.max(height, f.getHeight());
System.out.println("In repaint");
for (Node n : nodes) {
System.out.println(n.getX_Pos() + " " + n.getY_Pos());
int nodeWidth = Math.max(width, f.stringWidth(Integer.toString(n.getNode_ID()))+width/2);
g.setColor(Color.white);
g.fillOval(n.getX_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.setColor(Color.black);
g.drawOval(n.getY_Pos()-nodeWidth/2, n.getY_Pos()-nodeHeight/2, nodeWidth, nodeHeight);
g.drawString(Integer.toString(n.getNode_ID()) , n.getX_Pos()-f.stringWidth(Integer.toString(n.getNode_ID()))/2, n.getY_Pos()+f.getHeight()/2);
}
}
TIA,很抱歉问了这么长的问题 :)
- 在您的
Start
方法中,您实际上从未将Main
添加到JFrame
。根据您提供的断章取义的代码片段,我只能 "assume" 您正在覆盖Main
class 的paint
,这意味着,paint
永远不会被调用,因为它实际上没有附加到可显示组件 Start
不应创建JFrame
。您应该创建一个Main
的实例,然后将其添加到一个JFrame
的实例中,两者应该是分开的。应创建Main
并将其添加到Main
本身- 你不应该覆盖
paint
(作为一般规则),但你绝对不应该通过调用其他paint
方法之一来规避绘画过程,比如paintComponents
.相反(再次 "assuming" 你在Main
中覆盖paint
),你应该覆盖paintComponent
方法并调用super.paintComponent
在你做任何自定义绘画之前
查看 Performing Custom Painting and Painting in AWT and Swing 了解更多详情。
此外,您可能希望通读 Code Conventions for the Java TM Programming Language,这将使人们更容易阅读您的代码,您也可以更轻松地阅读其他人