Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (581)
Games in Android Showcase (162)
games submitted by our members
Games in WIP (632)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Slick2D Radial Gradient  (Read 1465 times)
0 Members and 1 Guest are viewing this topic.
Offline bcsharp

Senior Newbie

Exp: 6 years

« Posted 2013-06-28 23:19:53 »

With apologies, I think that I originally posted this under the wrong section, so I moved my question here. I am having difficulty creating a lighting system for my game. The way I plan to render lighting is like this:
  • Create a lighting map by using additive blending and radial gradients from all of the lightsources
  • Combine the lightmap with the rendered game using multiplicative blending
The issue with my code so far is creating a radial gradient class. I am using Slick2D, which doesn't include a radial gradient class that implements ShapeFill, the interface used to fill shapes. I decided to create one myself, which looks like this:
import org.newdawn.slick.*;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.svg.*;

public class RadialFill implements ShapeFill {
   Color start;
   Color end;
   float centerX;
   float centerY;
   public RadialFill(Color s, Color e){
      start = s;
      end = e;

   public Color colorAt(Shape shape, float x, float y) {
      float e = (float) (Math.sqrt((x - centerX) * (x - centerX) + (y - centerY) * (y - centerY))) / ((shape.getHeight() > shape.getWidth() ? shape.getHeight() : shape.getWidth()) / 2 );
      float s = 1 - e;
      Color col = new Color(1,1,1,1);
      col.r = (e * end.r) + (s * start.r);
      col.b = (e * end.b) + (s * start.b);
      col.g = (e * end.g) + (s * start.g);
      col.a = (e * end.a) + (s * start.a);
      return col;
   public void setCenter(float x, float y){
      centerX = x;
      centerY = y;
   public float getCenterX(){
      return centerX;
   public float getCenterY(){
      return centerY;
   public void setStartColor(Color s){
      start = s;
   public Color getStartColor(){
      return start;
   public void setEndColor(Color e){
      end = e;
   public Color getEndColor(){
      return end;
   public Vector2f getOffsetAt(Shape shape, float x, float y) {
      // TODO Auto-generated method stub
      return new Vector2f(0,0);


Here is the test code that I made:
import org.newdawn.slick.*;
import org.newdawn.slick.fills.*;
import org.newdawn.slick.geom.*;
import org.newdawn.slick.state.*;
import org.newdawn.slick.svg.*;

public class LightTestState extends BasicGameState {
   Input input;
   GradientFill grad;
   RadialFill gradFill;
   Ellipse mouseLight;
   int mouseX;
   int mouseY;
   boolean ready = false;

   public void init(GameContainer container, StateBasedGame game)
         throws SlickException {
      input = container.getInput();
      grad = new GradientFill(0, 0, Color.yellow, 100, 100,, false);
      gradFill = new RadialFill(Color.yellow,;
      ready = true;

   public void render(GameContainer container, StateBasedGame game, Graphics g)
         throws SlickException {
      if (ready){
         mouseLight = new Ellipse(mouseX, mouseY, 100, 100);
         gradFill.setCenter(mouseX, mouseY);
         g.fill(mouseLight, grad);

   public void update(GameContainer container, StateBasedGame game, int delta)
         throws SlickException {
      mouseX = input.getMouseX();
      mouseY = input.getMouseY();

   public int getID() {
      // TODO Auto-generated method stub
      return 1;


import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.tests.*;

public class Noteblock extends StateBasedGame {
   public Noteblock(){

   public void initStatesList(GameContainer container) throws SlickException {
      // TODO Auto-generated method stub
      addState(new LightTestState());

    * @param args
   public static void main(String[] args) {
      try {
         AppGameContainer container = new AppGameContainer(new Noteblock());
      } catch (SlickException e) {


However, when I try to create a radial gradient with this code, it doesn't work. At best, it can only make a fill of one color.  Huh Any ideas? Any help would be appreciated.

Pages: [1]
  ignore  |  Print  
You cannot reply to this message, because it is very, very old.

MrMapcom (11 views)
2015-05-23 20:26:16

MrMapcom (17 views)
2015-05-23 20:23:34

Waterwolf (29 views)
2015-05-20 15:01:45

chrislo27 (35 views)
2015-05-20 03:42:21

BurntPizza (70 views)
2015-05-10 15:53:18

FrozenShade (55 views)
2015-05-07 09:11:21

TheLopais (217 views)
2015-05-06 13:36:48

TheLopais (202 views)
2015-05-06 13:35:14

TheLopais (206 views)
2015-05-06 13:33:39

TheLopais (226 views)
2015-05-06 13:32:48
List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00 is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!