日期:2014-05-20  浏览次数:20720 次

paintComponent使用的问题
Java code

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import org.omg.CORBA.PUBLIC_MEMBER;

public class Circle extends JPanel{
    private int value;

    public Circle(){
        
    }
    public Circle(int value){
        this.value = value;
    }
    public int getValue(){
        return value;
    }
    
    public void setValue(int value){
        this.value = value;
        
        //System.out.print(value);
        repaint();
        
    }
    
   
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        int clockRadius = (int)(Math.min(getWidth(), getHeight())*0.8*0.5);
        int xCenter = getWidth()/2;
        int yCenter = getHeight()/2;
        g.setColor(Color.RED);
        g.drawOval(xCenter-clockRadius, yCenter-clockRadius, 2*clockRadius, 2*clockRadius);
        
          //System.out.print(value); 
        
            int line1 = (int)(clockRadius);
        int line1X = (int) (xCenter-clockRadius*Math.cos(value*(2*Math.PI/60)));
        int line1Y = yCenter+value;
        g.drawLine(xCenter, yCenter, line1X, line1Y);
    
    }
    public Dimension getPreferredSize() {
        return new Dimension(100,100);
    }
    
}



这是我写的一段画出线条的类代码,经过调试后发现当我在不断改变value值后,通过system.out.print在setValue()函数中显示value的值是在不断变化的,可是在protected void paintComponent(Graphics g)中打印的value值却一直是0没有变化,所以我的图像根本不会有变化一直是静止的,这是为什么呢

------解决方案--------------------
探讨
Java code

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;

import javax.swing.JPanel;

import org.omg.CORBA.PUBLI……