Swing + Paint:绘制2个或更多对象

时间:2016-07-22 18:31:56

标签: java swing object paint paintcomponent

我对Swing和paintComponent方法有疑问。 我有三节课。其中一个是班级时钟。当我想绘制一个时钟时没有问题,但如果我尝试绘制两个时钟,则只显示第二个时钟。

绘画只绘制最后一个对象吗?我创建了类时钟的实例,但它不起作用? 如何解决问题,以便显示两个时钟?

我的代码:

import java.awt.Dimension;
import java.awt.Toolkit;

import javax.swing.JFrame;


public class Hour 
{
    JFrame frm;
    Dimension dim;

    // Clock(x, y, angle, length)
    Clock u1 = new Clock(50, 50, 1, 50);
    Clock u2 = new Clock(100, 100, 15, 15);

    public Hour()
    {
        dim = Toolkit.getDefaultToolkit().getScreenSize();

        frm = new JFrame();
        frm.setSize(200, 200);
        frm.setLocation(dim.width/2 - frm.getSize().width/2, dim.height/2 - frm.getSize().height/2);
        frm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frm.setVisible(true);

        frm.add(u1);
        frm.add(u2);
    }

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

时钟级:

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

import javax.swing.JPanel;


public class Clock extends JPanel
{
    Calc c;

    private long clockspd = 50; 

    private int x = 50 ,y = 50;
    private int nx = 50 ,ny = 50;

    double angle = 0, length = 0;

    public Clock(int px, int py, double pangle, double plength)
    {
        x = px;
        y = py;
        angle = pangle;
        length = plength;

        Thread thread = new Thread()
        {
            public void run()
            {
                action();
            }
        };
        thread.start();
    }

    public void action()
    {
        for(int i=0; i<360/angle+1; i++)
        {
             c = new Calc(this, angle*i, length);
            nx = (int)c.getBx();
            ny = (int)c.getBy();
            try {
                Thread.sleep(clockspd);
            } 
            catch(InterruptedException ex) {
                Thread.currentThread().interrupt();
            }
            repaint();
        }
    }

    public void paintComponent(Graphics g)
    {
        super.paintComponent(g);

        Graphics2D g2 = (Graphics2D)g;
        g2.setStroke(new BasicStroke(2));

        g2.drawLine(x, y, x+nx, y+ny); //0, -30
        g2.drawArc(x-(int)length, y-(int)length, (int)length*2, (int)length*2, 0, 360);
    }

    public int getXCoor()
    {
        return x;
    }

    public int getYCoor()
    {
        return y;
    }
}

计算值:

public class Calc 
{
    double bx = 0;
    double by = 0;

    public Calc(Clock u, double angle, double length)
    {
        if(angle <= 180)
        {
            by = -length * Math.cos(Math.toRadians(angle));
            bx = Math.sqrt(Math.pow(length, 2)-Math.pow(by, 2));
        }
        if(angle > 180)
        {
            by = -length * Math.cos(Math.toRadians(angle));
            bx = -Math.sqrt(Math.pow(length, 2)-Math.pow(by, 2));
        }
    }

    public double getBx()
    {
        return bx;
    }

    public double getBy()
    {
         return by;
    }
}

希望有人可以帮助我

编辑:我尝试使用不同的布局,但每次使用Layoutmanager时,框架上都没有任何内容,因此绘图似乎不适用于Layoutmanager。

0 个答案:

没有答案