Reducing CPU Usage When Drawing Large Images With Alpha Transparency Java -
i'm in process of making game in java. in game when player damaged image drawn alpha transparency on drawing. image covers entire screen, obscuring player's view. process consumes ~50% of computer's cpu. there way of optimizing process?
sscce:
package com.johng.lonevessel.gui; import java.awt.alphacomposite; import java.awt.borderlayout; import java.awt.color; import java.awt.dimension; import java.awt.graphics; import java.awt.graphics2d; import java.awt.image; import java.awt.event.actionevent; import java.awt.event.actionlistener; import javax.swing.jframe; import javax.swing.jpanel; import javax.swing.timer; import com.johng.assets.assets; import com.johng.assets.images.images; public class test { public static void main(string[] args) { new test(); } //gets image public static final image redvision = assets.getimage(images.redvision); public test() { jframe frame = new jframe(); frame.setsize(new dimension(600, 600)); gamepanel gamepanel = new gamepanel(); frame.getcontentpane().setlayout(new borderlayout()); frame.getcontentpane().add(gamepanel); frame.setvisible(true); gamepanel.start(); } @suppresswarnings("serial") public class gamepanel extends jpanel implements actionlistener { timer timer; player player; public gamepanel() { super(); timer = new timer(15, this); player = new player(); } @override public void paintcomponent(graphics g) { graphics2d g2d = (graphics2d) g; //game drawing here g2d.setcolor(color.black); g2d.fillrect(0, 0, getwidth(), getheight()); if (player.gethealth() / player.getmaxhealth() < 0.25 && player.gethealth() > 0) { g2d.setcomposite(alphacomposite.getinstance(alphacomposite.src_over, 1 - player.gethealth() / player.getmaxhealth() * 4)); g2d.drawimage(test.redvision, 0, 0, getwidth(), getheight(), null); } } public void start() { timer.start(); } @override public void actionperformed(actionevent ae) { repaint(); } public class player { //bare-bone player purpose of question public player() { } public float gethealth() { return 10; } public float getmaxhealth() { return 100; } } } }
Comments
Post a Comment