1
public class Game implements ActionListener {

    public static Game game ;
    public static Render render ;
    JFrame frame = new JFrame("Game");
    Timer timer = new Timer(25,this);
    public Game(){
        render = new Render();
        frame.setSize(300, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Dimension windowSize = frame.getSize();
        GraphicsEnvironment ge = GraphicsEnvironment.getLocalGraphicsEnvironment();
        Point centerPoint = ge.getCenterPoint();

        int dx = centerPoint.x - windowSize.width / 2;
        int dy = centerPoint.y - windowSize.height / 2;    
        frame.setLocation(dx, dy);
        frame.add(render);
        timer.start();
        frame.setVisible(true);


    }

    public static void main(String[] args){
        game = new Game();
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        render.repaint();

    }

}

public class Render extends JPanel{

    public static int i = 166673;
    protected void paintComponent(Graphics g){
        super.paintComponent(g);
        g.setColor(new Color(i));
        g.fillRect(0, 0,300,300);
        i += 10;
    }

}

I am trying to make a program that changes color in every second or something I made a Timer and now my program works but it starts with blue and so on. I am starting it as an Hex to Dec number such as 1666073 some greenish color. it starts but goes into blue again I don't know why?. Sorry to bother you. Thanks for the help.

Frakcool
  • 10,915
  • 9
  • 50
  • 89
mertk
  • 19
  • 1
  • 10
  • Idk about everyone else, but I find this very vague. Can you please try to give more description? – Carcigenicate Oct 24 '17 at 21:20
  • I am so sorry. What I am trying to do is changing background color by every second. I tried making it by new Color("decimal number") but I cant find anything except blue I am not sure why it happens. (I am starting the colors in green and it goes to blue. If you need more description I can share the other part of my code.) – mertk Oct 24 '17 at 21:27
  • I'd use the other constructor that lets you specify the value of each color channel. Color should have a constructor that takes 3 ints instead of one. – Carcigenicate Oct 24 '17 at 21:28
  • Okay I will check it out. Sorry to bother you. Thanks a lot. – mertk Oct 24 '17 at 21:29
  • https://docs.oracle.com/javase/7/docs/api/java/awt/Color.html Look at the constructor section. – Carcigenicate Oct 24 '17 at 21:29
  • Probably not exactly what you're looking for, but related: https://stackoverflow.com/questions/46778963/drawingpanel-color-change-with-displacement/46792479#46792479 – Frakcool Oct 24 '17 at 21:35

0 Answers0