Is there any possible way to reuse the same Color type object without creating a new one? Because I think creating a new Color object is slowing the program down if the color is changed frequently.
something like this:
//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'm posting the whole code here. Sry it's a little bit messy:
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);
}
}
java.awt.Color is immutable class. You have to create a new Color instance if you need a new color, there is no other way. However, creating a new Color will not slow you down because you typically don't create a new Color in a tight loop like in your code. You create a color, do something with it and then create another color for some other purpose and do some other actions. In that kind of a real case just creating new color on need basis will not at all slow down the code!
Adding this after few messages exchanges with the author of the question:
It is definitely not slowing down after some time. Do this to verify:Add time logging in paintCompoment method. Take the method beginning time at the beginning of the method. Take the method ending time at the end of the method. Print how much milliseconds the method took. You will see that the time does not change considerably at all even if you run it for several minutes.
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.
}
Then the question is - why does the wave does not progress in the same rate? Looks like the painting algorithm is creating the illusion of wave slowing down. It may be a combination of interaction of existing pattern being overwritten, screen refresh frequency etc. You may consider clearing the background before painting every time. Use double buffering to make it smoother. Hopefully these 2 things will improve the situation!
It is possible, but you would have to override the Color class. Below is an example of how to do so, but be warned; this is very unsafe for threads.
The following test is a very minimal and easy to read example that override the Color class and add functionality to set the individual color channels. The ColorPanel contains a function that if called, will loop infinitely until the user closes the window. While looping, the program will step through states decrementing the current R, G, or B channel until the value for that channel is zero then it moves to the next one. This only uses one Color object.
You can inspect all the code for the Color class here: [email protected]
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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With