带有定时器的Java颜色变化图形 [英] Java color changing Graphics with Timer

查看:148
本文介绍了带有定时器的Java颜色变化图形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一张每秒更换两次颜色的光盘。该磁盘在一个扩展JPanel的DrawPanel上绘制,在主要方法中,DrawPanel被添加到一个框架中。
对于colorchanging,我使用一个计时器,当我试图在主方法中更改DrawPanel的背景时(我注释掉了)。



有人可以告诉我为什么它不适用于Graphics g对象或任何其他建议?



我刚从主方法复制代码并将其添加到paintComponent()方法,但这里不起作用。

  import java.awt。*; 
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

公共类DrawPanel扩展JPanel {

公共GridBagLayout gbl;

//位置和尺寸
int x = 0,y = 0,width = 200,height = 200;

public DrawPanel(){
repaint();
}

public DrawPanel(GridBagLayout gridBagLayout){
this.gbl = gridBagLayout;
}

public void paintComponent(Graphics g){
//覆盖旧图片
super.paintComponent(g);

ActionListener action = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Random gen = new Random();

Color color = new Color(gen.nextInt(256),gen.nextInt(256),gen.nextInt(256));

//绘制彩色磁盘
g.setColor(color);
g.fillArc(x,y,width,height,0,360);
}
};

定时器t =新定时器(500,动作);
t.setRepeats(true);
t.setInitialDelay(0);
t.start();



//绘制圆的边界
g.setColor(Color.BLACK);
g.drawArc(x,y,width,height,0,360);


$ b $ public static void main(String [] args){
final JFrame frame = new JFrame();
frame.setSize(300,300);
最终DrawPanel面板=新的DrawPanel();
panel.setOpaque(true);
frame.getContentPane()。add(panel);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);

// ActionListener action = new ActionListener(){
// @Override
// public void actionPerformed(ActionEvent e){
// Random gen = new随机();
// Color color = new Color(gen.nextInt(256),gen.nextInt(256),gen.nextInt(256));
// panel.setBackground(color);
//}
//};
//
//定时器t =新定时器(500,动作);
// t.setRepeats(true);
// t.setInitialDelay(0);
// t.start();



$ b


解决方案

Graphics 对象是暂时的,所以即使编译器允许,也不应该缓存它。相反,在类的构造函数中建立计时器,设置面板的BG,然后调用重新绘制。 E.G。

  import java.awt。*; 
import java.awt.event。*;
import javax.swing。*;
import java.util.Random;

public class DrawPanel extends JPanel {

Random gen = new Random();
//位置和尺寸
int x = 0,y = 0,width = 200,height = 200;
颜色drawColor = Color.BLACK;

public DrawPanel(){
repaint();
ActionListener action = new ActionListener(){
@Override
public void actionPerformed(ActionEvent e){
Color color = new Color(gen.nextInt(256),gen.nextInt (256),gen.nextInt(256));

//绘制彩色磁盘
drawColor = color;
DrawPanel.this.repaint();
}
};

定时器t =新定时器(500,动作);
t.setRepeats(true);
t.setInitialDelay(0);
t.start();

$ b @Override
public void paintComponent(Graphics g){
//覆盖旧图片
super.paintComponent(g);

//绘制圆的边界
g.setColor(drawColor);
g.drawArc(x,y,width,height,0,360);
}

public static void main(String [] args){
final JFrame frame = new JFrame();
frame.setSize(300,300);
最终DrawPanel面板=新的DrawPanel();
panel.setOpaque(true);
frame.getContentPane()。add(panel);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}


I want to draw a disc that changes colors twice a second. The disk is drawn on a DrawPanel which extends a JPanel and in the main method the DrawPanel is added to a frame. For the colorchanging I use a timer which works when I'm trying to change the background of the DrawPanel in the main method (what i commented out).

Can someone tell me why it doesn't work for the Graphics g object or any other suggestions?

I just copied the code from the main method and added it into the paintComponent() method, but here it doesn't work.

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;

public class DrawPanel extends JPanel{

    public GridBagLayout gbl;

    //position and dimension
    int x = 0, y = 0, width = 200, height = 200;

    public DrawPanel(){
        repaint();
    }

    public DrawPanel(GridBagLayout gridBagLayout) {
        this.gbl = gridBagLayout;
    }

    public void paintComponent(Graphics g){
        //Overwriting of old picture
        super.paintComponent(g);

        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Random gen = new Random();

                Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));

                //Draw color disk
                g.setColor(color);
                g.fillArc(x, y, width, height, 0, 360);
            }
        };

        Timer t = new Timer(500, action);
        t.setRepeats(true);
        t.setInitialDelay(0);
        t.start();



        //Draw boundary of circle
        g.setColor(Color.BLACK);
        g.drawArc(x, y, width, height, 0, 360);
    }


    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setSize(300, 300);
        final DrawPanel panel = new DrawPanel();
            panel.setOpaque(true);
            frame.getContentPane().add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);

//      ActionListener action = new ActionListener() {
//          @Override
//          public void actionPerformed(ActionEvent e) {
//              Random gen = new Random();
//                  Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
//                  panel.setBackground(color);         
//          }
//      };
//
//      Timer t = new Timer(500, action);
//      t.setRepeats(true);
//      t.setInitialDelay(0);
//      t.start();

    }

}

解决方案

The Graphics object is transient, so you should not cache it even if the compiler allows that. Instead establish the timer in the constructor of the class, set the BG of the panel, then call for a repaint. E.G.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;

public class DrawPanel extends JPanel {

    Random gen = new Random();
    //position and dimension
    int x = 0, y = 0, width = 200, height = 200;
    Color drawColor = Color.BLACK;

    public DrawPanel() {
        repaint();
        ActionListener action = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));

                //Draw color disk
                drawColor = color;
                DrawPanel.this.repaint();
            }
        };

        Timer t = new Timer(500, action);
        t.setRepeats(true);
        t.setInitialDelay(0);
        t.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        //Overwriting of old picture
        super.paintComponent(g);

        //Draw boundary of circle
        g.setColor(drawColor);
        g.drawArc(x, y, width, height, 0, 360);
    }

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setSize(300, 300);
        final DrawPanel panel = new DrawPanel();
        panel.setOpaque(true);
        frame.getContentPane().add(panel);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
}

这篇关于带有定时器的Java颜色变化图形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆