Moje pytanie: Chcę mieć możliwość zmiany jasności obrazu zasobu i mieć trzy jego instancje jako ImageIcons. Jeden o jasności 50% (tak ciemniejszy), inny o jasności 75% (trochę jaśniejszy), a na końcu inny o jasności 100% (taki sam jak obraz oryginalny). Ja również chcę zachować przejrzystość.Jak zmienić jasność obrazu?
Co próbowałem: Szukałem i wygląda na to, że najlepszym rozwiązaniem jest użycie RescaleOp
, ale po prostu nie mogę tego rozgryźć. Nie wiem, co to jest scaleFactor i offset. Oto mój kod na to, co próbowałem.
public void initialize(String imageLocation, float regularBrightness, float focusedBrightness, float pressedBrightness, String borderTitle) throws IOException {
BufferedImage bufferedImage = ImageIO.read(ButtonIcon.class.getResource(imageLocation));
setRegularIcon(getAlteredImageIcon(bufferedImage, regularBrightness));
setFocusedIcon(getAlteredImageIcon(bufferedImage, focusedBrightness));
setPressedIcon(getAlteredImageIcon(bufferedImage, pressedBrightness));
setTitle(borderTitle);
init();
}
private ImageIcon getAlteredImageIcon(BufferedImage bufferedImage, float brightness) {
RescaleOp rescaleOp = new RescaleOp(brightness, 0, null);
return new ImageIcon(rescaleOp.filter(bufferedImage, null));
}
Wezwanie będzie coś takiego:
seeATemplateButton.initialize("/resources/templateIcon-regular.png", 100f, 75f, 50f, "See A Template");
//I think my 100f, 75f, 50f variables need to change, but whenever I change them it behaves unexpectedly (changes colors and stuff).
Co się dzieje z tym kodem: Obraz pojawi się „niewidzialne” Wiem, że to nie dlatego, że jest na JLabel ze zdarzeniem kliknięcia myszą na tym i to działa dobrze. Jeśli po prostu pominę część zmieniającą jasność i powiem setRegularIcon(new ImageIcon(Button.class.getResource(imageLocation));
, to działa dobrze, ale oczywiście nie jest ciemniejsza.
Co Chyba muszę: Niektóre pomóc zrozumienie co offset
, scaleFactor
, a metoda filter
oznaczać/zrobić, a co za tym idzie jakie numery dać zmiennej jasności.
Każda pomoc będzie bardzo ceniona! Dzięki!
zobaczyć to podobne pytanie: http: // stackoverflow. com/questions/3433275/adjust-brightness-and-contrast-of-bufferedimage-in-java –