I have different result if pass same argument why? - java

I am developing some GUI apps in java. I wonder why JFrame.setBackground(Color) and Graphics.setColor(Color) return differ result on screen although I pass the same Color variable?
I create JPanel. It set as JRootPane with setBackground(new Color(0xB7CBEF)). JPanel has a button which draw with overload paintComponent. Inside methode I call Graphics.setColor(0xB7CBEF))..Graphics.fillRect(0,0,button_width, button_heigth). On screen background of button look at little be dark.
Thanks all. Problem was in realize external UI, which some dark background of button in passive condition. Fix this problem with transform button to transparent type.

Related

Java - Graphical glitches on background repaint

I'm making a microwave simulation program and having an annoying problem. I want the microwaves viewing window to change color on a button press, to visualize that the microwave is on. However, because I also have a jlabel with an icon (food image) in the background, I need the background to be semi transparent. I've accomplished this adding an alpha value to the jPanel (cookingWindow).
It starts like this
So what I've basically done is set an actionEvent to the start button with the following code.
cookingWindow.setBackground(new Color (250,234,1, 150));
cookingWindow.repaint();
This works, except this happens
When I minimise and maximise the window, everything goes back to place. But obviously, it would be preferable if you didn't have to minimise the window. Any ideas on how to stop this visual bug?
See Backgrounds With Transparency for an explanation of the problem and a couple of solutions.
Basically the problem is how the Swing opaque property handles transparent backgrounds (it doesn't).
Revalidating and repainting the jFrame seems to do fix everything up. I was previously only repainting the jPanel.

JButton background and foreground color swipe when button pressed

Could any one help to implement below thing?
I have custom button which i have overloaded the paintComponent and plainText method to give my own look. the main focus of overridden is to give my own style for buttons.
so here the requirement is originally button background is white and foreground is black and when user press the button the color should revert like background as black and foreground as white.
how can i achieve this?
Instead of subclassing Swing components to modify how they «look and feel», you can provide your own UI implementation and attach it to a normal component instance.
Look at following methods:
'JComponent.setUI(ComponentUI ui)': method to set the UI strategy/decoration
ButtonUI: the UI used for rendering buttons
BasicButtonUI: basic implementation you can extend easily, overwrite paintButtonPressed or the other paint-methods to create your own button look.

How can I change colors of components when the mouse is pressed in a JFrame in Java?

I am coding a piano in java using rectangles from the java.awt.graphics library. I am using a mouselistener to play the sound of each individual key when the mouse clicks a certain area on the JFrame.
How would I add a shape to the panel upon clicking, repaint, then repaint the keyboard back over top when the user releases the mouse?
Consider adding JLabels to a JPanel that uses GridLayout. Give each JLabel a MouseListener and either swap ImageIcons on mousePress/mouseRelease or change the JLabel's background with press and release. If you go the latter route, you'll want to make sure that the JLabels opaque property is set to true so that the background colors show.
Then for the black keys, you can add the above JPanel to a JLayeredPane and on top of this, add another JPanel that holds the black keys that function in the same way.
Also, you'll want to take care to "play" any notes in a background thread, such as can be obtained with a SwingWorker so as not to tie up the Swing event thread and completely freeze your program.
Consider solution: source
It might not be exactly what you're after, but it might give you an idea of how to approach your problem. It took me a long time to figure out how to use JLayeredPane without setting a null layout, but in the end this was the best I could come up with. Also, assumed some naming conventions for your sound files. :p

Clickable images in Java

I'm making a point-and-click escape type of game. I'm wondering, if there's an easy way to make clickable images? I'm going to use photographs as background and also as items that the player has to collect. So, is there an easy way to make the items clickable and also disappear after clicked (player collects it).
Thanks for answers, and if my explanation was complicated, please say and I'll try to fix it.
For making a game I'd recommend bringing all the logic one level lower.
Create an data structure which will contain the state of your game "level". This data structure will be loaded from some kind of XML level configuration file, and I think it should contain:
A Image object containing the level background (photo).
An array of all items. Each item should have an Image, dimensions on the level screen (X,Y,Width,Height) and some kind of state (visible, highlighted, etc.).
Make a class which extends Canvas. This will be the component which will contain and render your whole game screen (with items, and background photo).
Override it's paint method. In this paint method use drawImage method go through your level data object (specified in step 1) and draw the background (room) and all the items in their respective coordinates. If the item has visible = false - don't draw it. If it has selected = true - draw some highlight around it or whatever you want.
Implement a MouseListener. This listener should check if click coordinates are inside the dimensions of one of your "objects" on the screen (loop through all clickable objects). If it is - do appropriate action (for example increase score and set visible = false for that item) and update your canvas with repaint. This will trigger the paint method again drawing all the changes on your canvas.
Register a MouseListener on your Canvas with addMouseListener to tie it all together.
If you're using Swing, simply set the icon of a JButton. This will create a "clickable image".
There are several ways, the most straightforward being using a JButton and setting an icon on it. But you can also add a MouseListener to any Component (like JPanel) and set an image as background (override paint).
You're going to probably do something like this: Draw a JPanel and then position a bunch of JLabels on it, and each label will draw it's own image too.
If you have a specific code question, we can be more help, but you're being very general. Try working through the Swing examples on the java web site and then ask more targeted questions.
You can set the image to a JButton object as background. The key point is that you should listen to the mouse click event and JButton is the first choice to satisfy this requirement.
A simple example on how you can get a clickable image. For more examples and explanations on java Swing and awt you can look at the official java tutorials here.
//a lable holding an image
JLabel label = new JLabel(new ImageIcon("MyImage));
//Add a mouse listener to get the click event
//
label.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent e) {
System.out.println("Mouse clicked (# of clicks: "
+ e.getClickCount() + ")", e);
}
});

JButton Sub-Class Not Displaying Button until Clicked/Transparency Error

I have run into yet another issue with my program. I have made several JButton sub-classes to do specifically what I need them to do. The problem is that the buttons don't show up until I either click where they are supposed to be or if I hover the mouse over them (when I had setRolloverEnabled() to true). I originally had them all set for setRolloverEnabled() to true. But I understood that when I did hover the mouse over top of them, it had an ugly blue outline of the button which I didn't like at all. So is there any way to make them visible without having to hover over them, or without having to click them?
I have a background on my JFrame (I sub-classed JPanel and overrode the paintComponent() method) allowing JFrame to maintain its role as a container). Also in Adobe Photoshop I have designed the buttons and on the outer edge it has some transparency, I saved the files as .png so the transparency would be kept, but when the buttons are placed in the frame, there is an ugly blue outline still where it should be transparent. Any help on that.
Any suggestions would be appreciated. Below is the code for one of my Button classes.
public class Button extends javax.swing.JButton {
//This Button class is not the AWT Button object.
//It is a custom class designed by me.
public Button(ImageIcon normal){
setRolloverEnabled(false);
setVisible(true);
setIcon(normal);
setSize(normal.getIconWidth(),normal.getIconHeight());
}
public Button(ImageIcon normal, ImageIcon rollover){
this(normal);
setRolloverIcon(rollover);
}
public Button(ImageIcon normal, ImageIcon rollover,ImageIcon selected){
this(normal,rollover);
setSelectedIcon(selected);
}
}
There is a good chance you are not creating your GUI in the Event Dispatch Thread. Swing painting is single threaded by design. If you try to draw your components (even if you dont do it on purpose), the results will be varied. There are well documented methods to properly instruct the jvm to paint your components, including SwingWorker and SwingUtilities.invokeLater(Runnable). Take a look at this tutorial to get more info.

Categories