Reuse color type (color reset) in java

Is there any possible way to reuse the same Color object without creating a new one? Since I think that creating a new Color object slows down the program if the color changes frequently.

something like that:

//passing Graphics g into the function
int color_setting=1;
while(1)
{
    Color cl=new Color(color_setting,0,0);
    g.setPaint(cl);
    //the rest of drawing
    color_setting=(color_setting+1)%256;
}
//is there a way to reset the color of old cl without always creating a new Color object?

Ok, I post all the code here. Sry is a little dirty:

import java.lang.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.Timer;
import javax.swing.*;
import java.awt.geom.*;
import static java.lang.System.*;
import java.util.*;
class compo extends JComponent
{
    static int color,dir;
    {
        color=0;
        dir=1;
    }
    Toolkit kit=Toolkit.getDefaultToolkit();
    Dimension sz=kit.getScreenSize();
    public Dimension getPreferredSize(){return new Dimension(sz.width/2,sz.height/2);}
    public void paintComponent(Graphics g)
    {

        Graphics2D g2=(Graphics2D)g;
        float x=255;
        int count=255;
        while(count!=0)
        {
            color=color+dir;
            if(color%128==0)
               dir=-dir;
            Color temp_color=new Color(color+64,0,0);
            Line2D line=new Line2D.Double(0.0,x,sz.width,x);
            g2.setPaint(temp_color);
            x--;
            count--;
            g2.draw(line);
        }
    }

}

public class hello
{
    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
            {
                public void run()
                {
                    SimpleFrame frame=new SimpleFrame();
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.setVisible(true);
                    class temp implements ActionListener
                    {
                        Timer timer;
                        Toolkit kit=Toolkit.getDefaultToolkit();
                        Dimension sz=kit.getScreenSize();
                        SimpleFrame frame;
                        public temp(SimpleFrame _frame)
                        {
                            frame=_frame;
                        }

                        public void actionPerformed(ActionEvent event)
                        {
                            timer.stop();
                            frame.setTitle("this is the frame!");
                            frame.add(new compo());
                            frame.pack();
                            timer.start();
                        }
                        public void storing(Timer timer)
                        {
                            this.timer=timer;
                        }
                    }
                    ActionListener test=new temp(frame);
                    Timer t=new Timer(0,test);
                    ((temp)test).storing(t);
                    t.start();
                }
            }
        );
    }
}
class SimpleFrame extends JFrame
{
    private static final int DEFAULT_WIDTH=300;
    private static final int DEFAULT_HEIGHT=200;
    public SimpleFrame()
    {
        setSize(DEFAULT_WIDTH,DEFAULT_HEIGHT);
    }
}
+3
source share
4 answers

java.awt.Color - . Color, , . Color , Color , . , - , - . , , !

:

. , : paintCompoment. . . . , .

public void paintComponent(Graphics g)
{
    long methodBeginTime = System.currentTimeMillis(); //<< Added this line.
    Graphics2D g2=(Graphics2D)g;
    float x=255;
    int count=255;
    while(count!=0)
    {
        color=color+dir;
        if(color%128==0)
           dir=-dir;
        Color temp_color=new Color(color+64,0,0);
        Line2D line=new Line2D.Double(0.0,x,sz.width,x);
        g2.setPaint(temp_color);
        x--;
        count--;
        g2.draw(line);
    }
    long methodEndTime = System.currentTimeMillis(); //<< Added this line.
    System.out.println("Method time:" + (methodEndTime - methodBeginTime)); //<< Added this line.
}

: ? , . , , .. . , . , 2 !

+2

, Color. , , ; .

- , Color . ColorPanel , , , , . , R, G B, , . Color.

Color: java.awt.Color@grepcode.com

ColorPanel.java

import java.awt.Container;
import java.awt.Dimension;
import java.awt.Graphics;

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

public class ColorPanel extends JPanel {
    private static final long serialVersionUID = 2243743609739642647L;

    private MyColor color;
    private int state, r, g, b;

    public ColorPanel() {
        setPreferredSize(new Dimension(500, 300));

        color = new MyColor(0xFF0000);
        state = 0;
        r = 255;
        g = 0;
        b = 0;
    }

    public void shitJustGotReal() {
        try {
            while (true) {
                switch (state) {
                    case 0:
                        r -= 0xF;
                        if (r < 0) { r = 0; state = 1; g = 0xFF; }
                        color.setRed(r);
                        break;
                    case 1:
                        g -= 0xF;
                        if (g < 0) { g = 0; state = 2; b = 0xFF; }
                        color.setGreen(g);
                        break;
                    case 2:
                        b -= 0xF;
                        if (b < 0) { b = 0; state = 0; r = 0xFF; }
                        color.setBlue(b);
                        break;
                }

                repaint();

                System.out.printf("%06X\n", color.getRGB());

                Thread.sleep(100);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);

        g.setColor(color);
        g.fillRect(0, 0, getWidth(), getHeight());
    }

    public static void main(String[] args) {
        JFrame f = new JFrame("Chessboard");
        ColorPanel c = new ColorPanel();

        f.setContentPane(c);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.setLocationRelativeTo(null);
        f.pack();
        f.setVisible(true);

        c.shitJustGotReal();
    }
}

MyColor.java

import java.awt.Color;

public class MyColor extends Color {
    private static final long serialVersionUID = 6892714783307773362L;

    protected int value;

    public MyColor(int rgb) {
        super(rgb);

        value = 0xFF000000 | rgb;
    }

    public MyColor(int r, int g, int b) {
        this(r, g, b, 0xFF);
    }

    public MyColor(int r, int g, int b, int a) {
        super(r, g, b, a);

        setARGB(a, r, g, b);
    }

    public void setRed(int r) {
        setRGB(r, getGreen(), getBlue());
    }

    public void setGreen(int g) {
        setRGB(getRed(), g, getBlue());
    }

    public void setBlue(int b) {
        setRGB(getRed(), getGreen(), b);
    }

    public void setARGB(int a, int r, int g, int b) {
        value = ((a & 0xFF) << 24) |
                ((r & 0xFF) << 16) |
                ((g & 0xFF) <<  8) |
                ((b & 0xFF) <<  0);
    }

    public void setRGB(int r, int g, int b) {
        setARGB(0xFF, r, g, b);
    }

    public void setRGB(int value) {
        this.value = value;
    }

    @Override
    public int getRGB() {
        return value;
    }
}
+2

, Color, , . , , , . Color 256 ( , , ), Color .

, , , , Color

import java.awt.*;
import java.awt.color.ColorSpace;

/**
 * Extends Color so the value can be changed. As the original value-field is only accessible from
 * within java.awt package we override {@link #getRGB()} which is used to access the value even in
 * functions like {@link #getRed()}.
 */
public class VaryingColor extends Color {
    public static final int ALPHA_PATTERN = 0xff000000;
    public static final int RED_PATTERN   = 0x00ff0000;
    public static final int GREEN_PATTERN = 0x0000ff00;
    public static final int BLUE_PATTERN  = 0x000000ff;

    public static final int ALPHA_OFFSET = 24;
    public static final int RED_OFFSET   = 16;
    public static final int GREEN_OFFSET = 8;
    public static final int BLUE_OFFSET  = 0;
    /**
     * ARGB encoded. In hex it is two digits each.
     */
    private int rgb;

    public VaryingColor(int r, int g, int b) {
        super(r, g, b);
        rgb = super.getRGB();
    }

    public VaryingColor(int r, int g, int b, int a) {
        super(r, g, b, a);
        rgb = super.getRGB();
    }

    public VaryingColor(int rgb) {
        super(rgb);
        rgb = super.getRGB();
    }

    public VaryingColor(int rgba, boolean hasalpha) {
        super(rgba, hasalpha);
        rgb = super.getRGB();
    }

    public VaryingColor(float r, float g, float b) {
        super(r, g, b);
        rgb = super.getRGB();
    }

    public VaryingColor(float r, float g, float b, float a) {
        super(r, g, b, a);
        rgb = super.getRGB();
    }

    public VaryingColor(ColorSpace cspace, float[] components, float alpha) {
        super(cspace, components, alpha);
        rgb = super.getRGB();
    }


    /**
     * Returns the RGB value representing the color in the default sRGB
     * {@link java.awt.image.ColorModel}.
     * (Bits 24-31 are alpha, 16-23 are red, 8-15 are green, 0-7 are
     * blue).
     */
    @Override
    public int getRGB() {
        return rgb;
    }

    public void setRGB(final int rgb) {
        this.rgb = rgb;
    }

    public void setAlpha(final int alpha) {
        this.setRGB((this.getRGB() & (~ALPHA_PATTERN)) | ((alpha & 0xFF) << ALPHA_OFFSET));
    }

    public void setRed(final int red) {
        this.setRGB((this.getRGB() & (~RED_PATTERN)) | ((red & 0xFF) << RED_OFFSET));
    }

    public void setGreen(final int green) {
        this.setRGB((this.getRGB() & (~GREEN_PATTERN)) | ((green & 0xFF) << GREEN_OFFSET));
    }

    public void setBlue(final int blue) {
        this.setRGB((this.getRGB() & (~BLUE_PATTERN)) | ((blue & 0xFF) << BLUE_OFFSET));
    }
}

Color, getRGB() . , , Color .

, , , .

+1

, , pic , frame.remove(JComponent) . , , , , JComponents ( , ()).

0
source

All Articles