Java - 方法 repaint() 不调用 paintComponent() 方法
Posted
技术标签:
【中文标题】Java - 方法 repaint() 不调用 paintComponent() 方法【英文标题】:Java - method repaint() don't invoke paintComponent() method 【发布时间】:2013-12-13 18:46:29 【问题描述】:大家好!
我有一个与所有这些油漆“材料”有关的奇怪问题。 我正在创建可以绘制高速公路曲线的小型 SWING 应用程序 (http://en.wikipedia.org/wiki/Dragon_curve)。 我的项目中有 3 个类:
DragonData - 计算和存储龙点数所涉及的一切, GUI - 用户界面窗口, Point - 指定单点的类,现在好:我有一个 JPanel,我想在其上绘制点,点击一个按钮开始计算点坐标并绘制龙。
我的问题是:为什么它不起作用?我已经为这个 JPanel 创建了覆盖方法paintComponent 并在 'buttonDrawActionPerformed' 方法中调用它。我做错了什么?
我的项目有一个链接:http://www73.zippyshare.com/v/9630962/file.html
代码
Point.java
public class Point
private double x;
private double y;
public Point(double x, double y)
this.x = x;
this.y = y;
public double getX()
return x;
public void setX(double x)
this.x = x;
public double getY()
return y;
public void setY(double y)
this.y = y;
DragonData.java
import java.util.ArrayList;
public class DragonData
private static final int RATE = 500;
private static ArrayList<Point> dragonPoints = null;
public static void generatePoints(double x, double y, int iter,
int shiftX, int shiftY) // ### TYP double
dragonPoints = new ArrayList<Point>();
for(int i = 0 ; i < iter ; i++)
int option = (int)(Math.random() * 2);
switch(option)
case 0:
x = -0.4 * x - 1;
y = -0.4 * y + 0.1; //### !!! ### - aktualizujemy wartosc zmiennej
break;
case 1:
x = 0.76 * x - 0.4 * y;
y = 0.4 * x + 0.76 * y;
break;
dragonPoints.add(new Point(x, y));
;
calibrateCoordinates();
filterPoints(shiftX, shiftY);
private static void calibrateCoordinates()
int pointsCount = dragonPoints.size();
for(int i = 0 ; i < pointsCount ; i++)
double scaledX = dragonPoints.get(i).getX() * RATE;
double scaledY = dragonPoints.get(i).getY() * RATE;
dragonPoints.get(i).setX(scaledX);
dragonPoints.get(i).setY(scaledY);
private static void filterPoints(int shiftX, int shiftY)
int pointsCount = dragonPoints.size();
ArrayList<Point> choosen = new ArrayList<Point>();
for(int i = 0 ; i < pointsCount ; i++)
double x = dragonPoints.get(i).getX() + shiftX;
double y = dragonPoints.get(i).getY() + shiftY;
if((x > 0 && x < 800) && (y > 0 && y < 600))
choosen.add(new Point(x,y));
dragonPoints.clear();
dragonPoints.addAll(choosen);
System.out.println("New points count: " + dragonPoints.size());
public static ArrayList<Point> getDragonPoints()
return dragonPoints;
GUI.java
import java.awt.Graphics;
import java.util.ArrayList;
public class GUI extends javax.swing.JFrame
/**
* Creates new form GUI
*/
public GUI()
initComponents();
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents()
jPanel2 = new javax.swing.JPanel();
buttonDraw = new javax.swing.JButton();
fieldX = new javax.swing.JTextField();
fieldY = new javax.swing.JTextField();
fieldIter = new javax.swing.JTextField();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
panelCanvas = new javax.swing.JPanel()
@Override
protected void paintComponent(Graphics g)
super.paintComponent(g);
ArrayList<Point> dragonPoints = DragonData.getDragonPoints();
int newPointsCount = dragonPoints.size();
System.out.println("REPAINT BEFORE");
for(int i = 0 ; i < newPointsCount; i++)
int pointX = (int) dragonPoints.get(i).getX() + 100;
int pointY = (int) dragonPoints.get(i).getY();
g.drawRect(pointX, pointY, 1, 1);
;
panelCanvas = new javax.swing.JPanel();
jLabel4 = new javax.swing.JLabel();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setMinimumSize(new java.awt.Dimension(800, 600));
setResizable(false);
jPanel2.setBackground(new java.awt.Color(255, 255, 255));
buttonDraw.setText("Draw Dragon");
buttonDraw.addActionListener(new java.awt.event.ActionListener()
public void actionPerformed(java.awt.event.ActionEvent evt)
buttonDrawActionPerformed(evt);
);
jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel1.setText("X:");
jLabel2.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel2.setText("Y:");
jLabel3.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
jLabel3.setText("Loops (>=1000)");
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fieldX, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel2)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fieldY, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel3)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(fieldIter, javax.swing.GroupLayout.PREFERRED_SIZE, 141, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 60, Short.MAX_VALUE)
.addComponent(buttonDraw, javax.swing.GroupLayout.PREFERRED_SIZE, 149, javax.swing.GroupLayout.PREFERRED_SIZE)
.addContainerGap())
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(fieldX, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fieldY, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(fieldIter, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addComponent(buttonDraw))
.addGap(49, 49, 49))
);
panelCanvas.setBackground(new java.awt.Color(255, 255, 255));
panelCanvas.setMinimumSize(new java.awt.Dimension(800, 600));
panelCanvas.setPreferredSize(new java.awt.Dimension(800, 600));
panelCanvas.setRequestFocusEnabled(false);
jLabel4.setText("Author Label");
javax.swing.GroupLayout panelCanvasLayout = new javax.swing.GroupLayout(panelCanvas);
panelCanvas.setLayout(panelCanvasLayout);
panelCanvasLayout.setHorizontalGroup(
panelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCanvasLayout.createSequentialGroup()
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(jLabel4)
.addContainerGap())
);
panelCanvasLayout.setVerticalGroup(
panelCanvasLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, panelCanvasLayout.createSequentialGroup()
.addContainerGap(575, Short.MAX_VALUE)
.addComponent(jLabel4)
.addContainerGap())
);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(panelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, 817, Short.MAX_VALUE)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(panelCanvas, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
pack();
// </editor-fold>//GEN-END:initComponents
private void buttonDrawActionPerformed(java.awt.event.ActionEvent evt) //GEN-FIRST:event_buttonDrawActionPerformed
double x = Double.valueOf(this.fieldX.getText());
double y = Double.valueOf(this.fieldY.getText());
int iter = Integer.valueOf(this.fieldIter.getText());
int middleX = this.panelCanvas.getSize().width / 2;
int middleY = this.panelCanvas.getSize().height / 2;
DragonData.generatePoints(x, y, iter, middleX, middleY);
this.panelCanvas.repaint();
//GEN-LAST:event_buttonDrawActionPerformed
/**
* @param args the command line arguments
*/
public static void main(String args[])
GUI gui = new GUI();
gui.setLocationRelativeTo(null);
gui.setVisible(true);
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton buttonDraw;
private javax.swing.JTextField fieldIter;
private javax.swing.JTextField fieldX;
private javax.swing.JTextField fieldY;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JPanel jPanel2;
private javax.swing.JPanel panelCanvas;
// End of variables declaration//GEN-END:variables
【问题讨论】:
以后将您的代码发布为文本而不是 rar 文件的链接 确实——请不要链接。所有相关的代码都应该在这里发布。链接经常被防火墙阻止(正如我所经历的那样)并且经常变得陈旧,因此对于有类似问题的未来访问者来说绝对没有帮助。 不上传代码,我自己来。再说一次,如果你自己努力做到这一点,下次会更好。 【参考方案1】:在initComponents()
方法中的GUI
类中,首先您使用覆盖的paintComponent(...)
创建面板:method panelCanvas = new javax.swing.JPanel() ...
,然后在下一行中使用通用JPanel
重新创建该对象。
所以删除下一行panelCanvas = new javax.swing.JPanel();
,一切都会奏效。
【讨论】:
它正在工作!非常感谢! Netbeans 中的内置构建器产生了这个愚蠢的错误 :) @MichalAngryProgrammer:不使用代码生成器实用程序的另一个原因,至少在您了解所使用的 GUI 库之前是这样。 1+ 到答案。 @MichalAngryProgrammer:再次,请发布您的代码,以便我可以删除我的反对票。以上是关于Java - 方法 repaint() 不调用 paintComponent() 方法的主要内容,如果未能解决你的问题,请参考以下文章
java中paint repaint update 之间的关系