So I am creating a program and I have one button and need to change the background color from yellow to green and back again for an infinite amount of times. How do I do this?
I can make one button change to one color but I cannot make one button change between two colors.
here is an examples to change a button between colors
this will change the color of a button everytime you click it
if Button1.BackColor = Color.FromArgb(0, 128, 0) then
Button1.BackColor = Color.FromArgb(255,255,0)
else
Button1.BackColor = Color.FromArgb(0, 128, 0)
end if
Related
I am trying to add an image as a background of a button in pygame gui. I have the button created now and I just need to add the image. How can I add the image to the button?
This is my code
game1 = pygame.Rect(150 , 100, 200, 150) # creates a rect object
pygame.draw.rect(screen, [255, 100, 0], game1) # draw objects down here
it works fine
You're probably looking for pygame.Surface.blit (http://www.pygame.org/docs/ref/surface.html#pygame.Surface.blit)
Load your background image and blit it wherever you want.
img = pygame.image.load('thisisanimage.png')
WhateverYourDisplayNameIs.blit(img,(x,y))
Just in case you're confused, it looks like you're using screen as your display surface.
I've got a Xamarin Forms cross-platform application (iOS and Android), and on one of the screens I want a list with details:
Heading 1
Detail 1
Detail 2
Detail 3
Heading 2
Detail 1
Heading 3
Detail 1
Detail 2
As you can see, the amount of detail under each heading is variable.
I want the page to display at first with just the headings:
Heading 1
Heading 2
Heading 3
And then when the user presses on a heading, the details for that particular heading appear. Pretty standard stuff.
I've tried several different ways to get this to work, the only path that seems open to me is to have a StackLayout where I define a bunch of labels:
new StackLayout
{
Orientation = StackOrientation.Vertical,
Children =
{
new Label { Text = "Heading 1" },
new Label { Text = " Detail 1\n Detail 2\n Detail 3", IsVisible = false },
new Label { Text = "Heading 2" },
new Label { Text = " Detail 1", IsVisible = false },
new Label { Text = "Heading 3" },
new Label { Text = " Detail 1\n Detail 2", IsVisible = false }
}
}
I then add a TapGestureRecognizer to the heading labels, and when tapped I toggle the value of IsVisible for the detail labels. It works!
The only thing I don't like, is that there is no transition. I click on the heading label and BAM the detail label appears (correctly pushing down all the following labels to make space for itself). I would like an animation so that when I click on the header, the space beneath the header "slowly" opens up to reveal the detail.
As I read about animations online, one possibility is to set the HeightRequest of the detail labels to zero (instead of hiding them with IsVisible=false) and then creating an animation that "slowly" changes the HeightRequest from zero to the actual height of the label. And that's where I run into a problem.
I can't figure out how to get Xamarin to tell me the height of my "details" label.
If I inspect the Height and HeightRequest properties of my details label right after creating it, they are both -1 (no big surprise there). If I inspect those same two properties when I click on the heading, they are still -1. The only way I've found to get the height of my detail label, is to set the detail label visible, call ForceLayout() on my stack layout, store the detail label height, and then set the detail label invisible again. The problem with that is that I sometimes see the detail label flash visible for an instant while I do this.
What's the best/recommended way to accomplish my desired UI?
You can use the Animation API.
Read the blog about it - Creating Animations with Xamarin.Forms.
In particular for your scenario you can use the FadeTo method to animate the Opacity property of a Visual Element.
Eg :
await image.FadeTo (1, 4000);
For more information, see Animation.
In your case my suggested approach would be for showing a label, to set opacity of label to 0, then make it visible, and then use FadeTo to make the opacity to 1.
Use the opposite to hide the label, set opacity 0 via FadeTo, then set IsVisible to false.
If I understand right your problem the only thing you need is to avoid the flash on the label, if this is the case then you can set the Opacity to 0, in this way the label will not be visible until you set again the opacity to 1.
I can suggest you to make a custom XF control (to use as item DataTemplate) as follow:
2 vertical parts:
The header part (A) (when you click on it it will show the second part)
The second part is a 'Listview' control (B) that is empty at the beginning
When you click on (A):
it will show (B)
It will start to populate (B) with your details elements
The trick in my mind is to implement a method that populate the listview (B) item by item (getting them from your viewmodel) with some delay (a few milliseconds) between each insertion and maybe a 'fadeTo' effect too in the same time.
You can see here what I mean (see the "Fade" section):
Insert / fade list item effect sample in HTML
You can improve your template as you want, by embedding the two parts into a 'border' for instance, to make a graphical separation...
Tell me if it's unclear, all you need is time :)
And maybe if you are ready, you can try to make native controls / animations...
I'm working on a tic tac toe game where each square is its own button and the image of the button changes when the square is tapped. I have done this by connecting all of the square buttons to the same IBAction #IBAction func buttonPressed(sender: AnyObject) and by using sender.setImage() to change the image. The problem is, the images are all blue. I can change this blue to a different color by changing the global tint color, but what I really want is for the O and X images to be different colors. sender.tintColor throws an error and button.tintColor, as I have in the code below, just changes the color of one image back and forth every time a square is tapped. I have tried setting the global tint to no color in the File Inspector, but it just goes back to the default blue. Any thoughts?
if activePlayer == 1 {
sender.setImage(UIImage(named: "o-img"), forState: .Normal)
activePlayer = 2
button.tintColor = UIColor.blackColor()
} else {
sender.setImage(UIImage(named: "x-img"), forState: .Normal)
activePlayer = 1
button.tintColor = UIColor.whiteColor()
}
When you drag the image to the view controller's code to connect, before clicking "Ok" change the sender to UIImage instead of AnyObject.
You need to disable the button so the next player can not tap on it.
sender.enabled = false
As it turns out, changing the button type from 'System' to 'Custom' removes the tint. Hope that helps someone else who came across the same issue!
My question concerns the sdl2.ext.Renderer mainly, and has to do with a problem I've encountered when trying to render sprites on a sdl2.ext.Window surface.
So right now, for coloring my background on an SDL2 Window, I make the following call:
White = sdl2.ext.Color(255,255,255)
class Background(sdl2.ext.SoftwareSpriteRenderSystem):
def __init__(self,window):
super(Background,self).__init__(window)
sdl2.ext.fill(self.surface,White)
This colors the surface of the Window with a white background. However, I also want to display text on the screen. This is done by creating a TextureSprite using the from_text method of the sdl2.ext.SpriteFactory class as follows:
Renderer = sdl2.ext.Renderer(W) # Creating Renderer
ManagerFont = sdl2.ext.FontManager(font_path = "OpenSans.ttf", size = 14) # Creating Font Manager
Factory = sdl2.ext.SpriteFactory(renderer=Renderer) # Creating Sprite Factory
Text = Factory.from_text("Unisung Softworks",fontmanager=ManagerFont) # Creating TextureSprite from Text
Renderer.copy(Text, dstrect= (0,0,Text.size[0],Text.size[1])) # Resizing the Texture to fit the text dimensions when rendered
The problem occurs when the event loop is run.
running = True
while running:
events = sdl2.ext.get_events()
for event in events:
if event.type == sdl2.SDL_QUIT:
running = False
break
if event.type == sdl2.SDL_MOUSEBUTTONDOWN:
pass
Renderer.copy(Text, dstrect= (0,0,Text.size[0],Text.size[1]))
Renderer.present() # Problem 1
W.refresh() # Problem 2
return 0
Full Example Paste
When both Renderer.present() and W.refresh() are called, I get a flickering effect from the screen. This seems to be because the Renderer overwrites the White colored Window surface, which then gets colored over again by the Window.refresh() call. This then repeats, resulting in a flickering mess.
I would like to know what I can do to solve this problem? Should I even be using both Window.refresh() and the Renderer at the same time? Is there a way to let one render the other? (Renderer renders background for example). If anyone can help me out, that would be great.
The problem is that you are using Window.refresh() along with a SDL_Renderer and a SoftwareSpriteSystem with SDL_Texture objects.
As outlined in the PySDL2 docs, this is likely to break (since software surface buffers get mixed with hardware-based textures) and you should avoid it. If you want to color the window background, you should use Renderer.clear(White) instead of your Background class and call it before copying the text via Renderer.copy():
Renderer.clear(White)
Renderer.copy(Text, dstrect= (0,0,Text.size[0],Text.size[1]))
Renderer.present()
Do not forget to change the color for your FontManager. By default it uses a white text color, so you should change it to e.g. your Red color:
ManagerFont = sdl2.ext.FontManager(font_path = "OpenSans.ttf", size = 14, color=Red)
How do I make the corners of a TextField round?
I'm using Javax.swing.*;
Here's my code for BlueJ:
inputLine = new JTextField();
inputLine.setBounds(480, 350, 150, 30);
contentPane.add(inputLine);
inputLine.setForeground(Color.black);
inputLine.setBackground(Color.white);
inputLine.setBorder(BorderFactory.createLineBorder(Color.black));
inputLine.setFont(new Font("Tahoma", Font.BOLD, 13));
inputLine.setHorizontalAlignment(JTextField.CENTER);
inputLine.setEditable(true);
contentPane.add(inputLine);
inputLine.addActionListener(this);
inputLine.setText("ID Number");
what you can do, is first create a Jframe window.
If you want only a text Field, make the dimensions of the j Frame a bit bigger than the preferred text Field size.
Then, set the layout of the jFrame to absolute.
Create a Jlabel that covers the whole screen Jframe.
Create the text Field only after this step.
Then create the text Field at the preferred location in the j Frame.
Design a graphic / picture for the background and set the icon of the background jLabel to the graphic you have designed.
The graphic you have designed must have a rounded rectangular picture in it and the text Field must be exactly positioned on top of that.
Once that is done set the opacity of the Text Field to 0.0. This should work very well...
JFrame frame = new JFrame("FrameDemo");
jframe.setPreferredSize(new Dimension(x, y));
[please add the other methods to create a jframe window...these are only the basic commands]
pane.setLayout(null);//makes it an absolute layout so that it does not reshape itself
frame.setVisible(true);
after doing this, design your graphics.
then:
import it into the current working directory, where this program has been written.
then:
create a jLabel inside the jFrame.
JLabel label1 = new JLabel("JLabel Set Icon Example.");
set its icon to the graphic you have designed.
label1.setIcon(new ImageIcon("full path of the image _ with the proper file extension"));
then
textField = new JTextField(desired value);//please add the desired value into the brackets
//set its location and all the other thing that are required
set the location of the textField exactly over the the rounded rectangle in the graphic.
then:
textField.isOpaque("false");
jTextField1.setBorder(null);//this should work
and there you go...the user will think that the textField is a rounded rectangle but actually it is like a rectangle which has a transparent background...
let me know if there are any problems.
thanks.