Java Swing Development Components and Layout - Huawei Developers

1. Java Swing Overview
Java uses a graphical user interface (GUI) to facilitate interaction between a user and a program. Java's Swing toolkit includes a number of classes to support GUI design. For example, component classes such as buttons, menus, lists, and text boxes, and container classes such as windows and panels.
The javax.swing package provides more powerful classes for designing GUIs. The UML class diagrams of some classes in the java.awt and javax.swing packages are as follows:
{
"lightbox_close": "Close",
"lightbox_next": "Next",
"lightbox_previous": "Previous",
"lightbox_error": "The requested content cannot be loaded. Please try again later.",
"lightbox_start_slideshow": "Start slideshow",
"lightbox_stop_slideshow": "Stop slideshow",
"lightbox_full_screen": "Full screen",
"lightbox_thumbnails": "Thumbnails",
"lightbox_download": "Download",
"lightbox_share": "Share",
"lightbox_zoom": "Zoom",
"lightbox_new_window": "New window",
"lightbox_toggle_sidebar": "Toggle sidebar"
}
➢When learning GUI programming, you must have a good grasp of two concepts: container classes and component classes.
➢The JComponent class in the javax.swing package is a direct subclass of the Container class in the java.awt package and an indirect subclass of the Component class in the java.awt package. To learn GUI programming is to master some important subclasses of the Component class and their usage methods.
➢The following are the basics that are often mentioned in GUI programming.
(1) Java refers to an object created by a subclass or indirect subclass of a Component class as a component.
(2) In Java, an object created by a subclass or indirect subclass of a container is called a container.
(3) You can add components to the container. The Container class provides a public method: add(). A container can call this method to add a component to the container.
(4) The container invokes the removeAll() method to remove all components in the container. Invoke the remove(Component c) method to remove the component specified by parameter c in the container.
(5) Note that the container itself is also a component, so one container can be added to another container to achieve nesting of containers.
(6) Each time a new component is added or removed from the container, the container should call the validate() method to ensure that the components in the container can be displayed correctly.
2. Window
➢An instance of the JFrame class provided by Java is an underlying container, commonly referred to as a window. Other components must be added to the underlying container so that information can be exchanged with the operating system through this underlying container.
➢The JFrame class is an indirect subclass of the Container class. When you need a window, you can create an object using JFrame or its subclasses.
➢A window is also a container to which you can add components.
➢ It should be noted that windows are added by the system to the display screen by default, so adding one window to another container is not allowed.
2.1 Common JFrame Methods
(1) Construction method
1
2
3JFrame()//Create an untitled window.
JFrame(String s)// Create a window with the title s.
(2) Common methods
1public void setBounds(int a,int b,int width,int height)//
The initial position of the setting window is (a, b), that is, a pixel from the left side of the screen and b pixels from the upper side of the screen. The width of the window is width and the height is height.
1public void setSize(int width,int height)//
Sets the size of the window.
1public void setLocation(int x,int y)//
Sets the position of the window. The default position is (0, 0).
1public void setLocation(int x,int y)//
Specifies whether the window is visible. By default, the window is invisible.
1public void setResizable(boolean b)//
Specifies whether the window size can be adjusted. By default, the window size can be adjusted.
1public void dispose()//
Undoes the current window and releases the resources used by the current window.
1public void setExtendedState(int state)//
Sets the extended status of the window.
1public void setExtendedState(int state)//
This method is used to set how the program will handle after you click the close icon in the upper right corner of a window. For example: EXIT_ON_CLOSE
1public void setExtendedState(int state)//
Set the layout manager for this container.
1public Component add(Component comp)//
Appends the specified component to the end of this container.
1public void setMenuBar(MenuBar mb)//
Sets the menu bar of this frame to the specified menu bar.
1public void validate()//
Using the validate method causes the container to rearrange its subcomponents again. When you modify the subcomponents of this container (Add or remove components in the container, or change layout-related information) , the above method should be called.
The following example creates two windows using JFrame:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17import javax.swing.*;
import java.awt.*;
public class Example1 {
public static void main(String[] args) {
JFrame window1 = new JFrame("First Window ");
JFrame window2 = new JFrame("Second window ");
Container con = window1.getContentPane();
con.setBackground(Color.red);// Set the color of the window
window1.setBounds(60, 100, 188, 108);// Set the position and size of the window on the screen.
window2.setBounds(260, 100, 188, 108);
window1.setVisible(true);
window1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// Release the current window.
window2.setVisible(true);
window2.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// Exit the program.
}
}
2.2 Menu Bar, Menu, Menu Item
(1) Menu bar
JMenubar, a subclass of the JComponent class, is responsible for creating the menu bar, and the JFrame class has a method for placing the menu bar in a window: setJMenuBar(JMenuBar bar); this method adds the menu bar to the top of the window.
The method for constructing the JMenuBar class of the menu bar is as follows:
1
2JMenuBar ();
JMenuBar Mbar = new JMenuBar ()
(2)Menu
JMenu, a subclass of the JComponent class, is responsible for creating menus.
Method of constructing the menu JMenu class:
1
2
3JMenu();
JMenu(String s);
JMenu m = new JMenu();
Common methods:
1
2
3public void add(JMenuItem item)// Adds a menu item specified by the item parameter to a menu.
public JMenuItem getItem(int n)// Gets the menu options at the specified index.
public int getItemCount()//Gets the number of menu options.
(3) Menu items
The JMenuItem class, a subclass of the JComponent class, creates menu items. The main methods of the JMenuItem class are as follows:
1
2
3JMenuItem(String s)// Constructs a menu item with a title.
JMenuItem(String text, Icon icon) // Construct menu items with titles and icons
public void setAccelerator(KeyStroke keyStroke)// Set shortcut keys for menu items
Example 2 Use the JFrame subclass to create a window with a menu in the main method:
WindowMenu.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42import javax.swing.*;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
public class WindowMenu extends JFrame {
JMenuBar menubar; //Declare a menu bar menubar
JMenu menu, subMenu; //Declare two menus menu,subMenu
JMenuItem itemLiterature, itemCooking; //Declare two menu items itemLiterature, itemCooking
public WindowMenu() {
}
public WindowMenu(String s, int x, int y, int w, int h) { // constructors, creating windows
init(s);// Window initialization
setLocation(x, y);// Position of the window
setSize(w, h);// Window size
setVisible(true);// Window Visible
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);// Actions after closing the window
}
void init(String s) {
setTitle(s); // Set the title of the window
menubar = new JMenuBar(); // Creates a menu bar menubar.
menu = new JMenu("Menu"); // Create a menu called Menu
subMenu = new JMenu("Sports Topics"); // Create a menu called Sports Topics
// Icons on the menu
// Using the icon class Icon, declare an icon, and then create its subclass ImageIcon class Create an icon
//Icon icon = new ImageIcon("a.gif");
itemLiterature = new JMenuItem("Literary Topics", new ImageIcon("a.gif")); // Create a menu itemLiterature with a title and icon
itemCooking = new JMenuItem("Cooking Topics ", new ImageIcon("b.gif")); // Create a menu itemCooking with a title and icon
itemLiterature.setAccelerator(KeyStroke.getKeyStroke('A')); // Set shortcut keys for the itemLiterature menu item
itemCooking.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S, InputEvent.CTRL_MASK)); // Setting shortcut keys for the itemCooking menu item
menu.add(itemLiterature);
menu.addSeparator(); // Add dividers between menus
menu.add(itemCooking);
menu.add(subMenu);
subMenu.add(new JMenuItem("soccer", new ImageIcon("c.gif")));
subMenu.add(new JMenuItem("basketball", new ImageIcon("d.gif")));
menubar.add(menu); // Add a menu to a menu bar
setJMenuBar(menubar); // Place the menu bar in the window
}
}
Example2.java
1
2
3
4
5public class Example2 {
public static void main(String[] args) {
WindowMenu win=new WindowMenu("Window with Menu",20,30,200,190);
}
}
3. Common Components and Layout
You can decompile a component in the command line window to view the attributes and common methods of the component. For example:
1C:\>javap javax.swing.JComponent
For example:
3.1 Common Components
1. Text box: A text box created by JTextField, a subclass of JComponent, allows users to enter a single line of text in the text box.
1
2
3
4
5
6// Construction Methods
JTextField();
JTextField(int columns);
// Common Methods
public String getText();
public void setText(String t);
2. Text area: A text area is created by JTexArea, a subclass of JComponent, to allow users to enter multiple lines of text in the text area.
1
2
3
4
5
6// Construction Methods
JTextArea();
JTextArea(int rows, int columns);
// Common Methods
public String getText();
public void setText(String t);
3. Buttons: Used by the JButton class, a subclass of JComponent, to create buttons that allow users to click buttons.
1
2
3
4
5// Construction Methods
JButton();
JButton(String text);
// Common Methods
public void addActionListener(ActionListener l);
4. Tags: The JLabel class, a subclass of JComponent, creates tags and provides prompt information for users.
1
2
3
4
5
6
7// Construction Methods
JLabel();
JLabel(String text);
JLabel(Icon image);
// Common Methods
public String getText();
public void setText(String t);
5. Selection box: The JCheckBox class, a subclass of JComponent, is used to create selection boxes that provide users with multiple choices.
1
2
3
4
5
6// Construction Methods
JCheckBox();
JCheckBox(String text);
// Common Methods
public void addItemListener(ItemListener l);
public void addActionListener(ActionListener l);
6. Radio button: Used by the JRadioButton class, a subclass of JComponent, to create a single selection box.
1
2
3
4
5// Construction Methods
JRadioButton();
JRadioButton(String text);
// Common Methods
public void addItemListener(ItemListener l);
7. Drop-down list: Used by the JComponent subclass JComboBox class to create a drop-down list.
1
2
3
4
5
6
7// Construction Methods
JComboBox();
JComboBox(Object[] items)
// Common Methods
public void addItemListener(ItemListener l);
public Object getSelectedItem();
public int getSelectedIndex();
8. Password box: The JPasswordField subclass of JComponent creates a password box. The default response character of the password box is *.
1
2
3
4
5
6
7
8
9// Construction Methods
JPasswordField();
JPasswordField(int columns);
// Common Methods
public String getText();
public void setText(String t);
public void setEchoChar(char c)// Use this method to reset the echo character.
public char[] getPassword()//This method returns the actual password.
The following example shows some common components:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43import javax.swing.*;
import java.awt.*;
class ComponentInWindow extends JFrame {
JCheckBox checkBox1, checkBox2; // Declare two check boxes
JRadioButton radioM, radioF; // Declare two radio boxes
ButtonGroup group; // Declare a button group
JComboBox<String> comboBox; // drop-down list
public ComponentInWindow() { // construction method
init(); // Calling the init() method
setVisible(true); // Window Visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
void init() {
setLayout(new FlowLayout()); // Set the layout manager for this container
comboBox = new JComboBox<>(); // Create a drop-down list
// Create two check boxes
checkBox1 = new JCheckBox("Love Java");
checkBox2 = new JCheckBox("I like data structures.");
group = new ButtonGroup();
radioM = new JRadioButton("male");
radioF = new JRadioButton("female");
group.add(radioM);
group.add(radioF); // Single-choice is supported only by grouping.
add(checkBox1);
add(checkBox2);
add(radioM);
add(radioF);
comboBox.addItem("Object-oriented");
comboBox.addItem("Minimum Spanning Tree(MST)");
add(comboBox);
}
}
public class Example9_3 {
public static void main(String[] args) {
ComponentInWindow win = new ComponentInWindow();
win.setBounds(100, 100, 450, 260);
win.setTitle("Common Components");
}
}
3.2 Common Containers
JComponent is a subclass of Container. Therefore, all components created by JComponent subclasses are containers. Containers are often used to add components. The JFrame is the bottom container, and the containers mentioned in this section are traditionally called intermediate containers, which must be added to the bottom container to function.
1. JPanel panel:
1
2
3
4
5// Construction Methods
JPanel();
// For example: JPanel p = new JPanel();
// Common Methods
public void add();
Create a panel using JPanel, add components to the panel, and add the panel to other containers. The default layout for the JPanel panel is the FlowLayout layout.
2. JTabbedPane Tab Pane
You can use the JTabbedPane container as an intermediate container.
When a user adds a component to the JTabbedPane container, the JTabbedPane container automatically assigns a tab to the component. That is, a tab corresponds to a component.
The components corresponding to each tab are stacked in the JTabbedPane container. When a user clicks a tab, the JTabbedPane container displays the components corresponding to the tab.
The tabs are by default at the top of the JTabbedPane container, arranged from left to right.
The JTabbedPane container can use:
add(String text,Component c);
Method to add component c to the JTabbedPane container and specify that the text prompt for the tab corresponding to component c is text.
3. Scroll PaneJscrollPane:
You can add only one component to the scroll pane. You can place a component in a scroll pane and view the component through the scroll bar.
JTextArea does not come with a scroll bar, so you need to place the text area in a scrolling pane.
For example:
1JScrollPane scroll = new JScrollPane(new JTextArea());
4. Split Pane JSplitPane
A split pane is a container that is divided into two parts. There are two types of split panes: horizontal split and vertical split.
Horizontal split panes are divided into left and right parts by a split line. A component is placed on the left and a component is placed on the right. The split line can be moved horizontally. Splitting a pane vertically uses a split line to divide the pane into two parts, with one component on top and one component on bottom. The split line moves vertically.
/* Two Common Construction Methods of JSplitPane*/
JSplitPane(int a,Component b,Component c)
// Parameter a is set to the static constant HORIZONTAL SPLIT or VERTICAL _SPLIT of JSplitPane to determine whether to split horizontally or vertically.
// The last two parameters determine which component to place.
JSplitPane(int a, boolean b,Component c,Component d)
// Parameter a is set to the static constant HORIZONTAL SPLIT or VERTICAL_ SPLIT of JSplitPane to determine whether to split horizontally or vertically.
// Parameter b determines whether the component changes continuously as the split line moves (true is continuous).
5. JLayeredPane Layered Pane
If components added to a container often need to deal with overlap, consider adding components to the hierarchical pane. The hierarchical pane is divided into five layers. The hierarchical pane uses add(Jcomponent com, int layer).
Add component com and specify the layer where com is located. The value of layer is a class constant in the JLayeredPane class.
DEFAULT LAYER、PALETTE I AYER、MODAL LAYER、POPUP I AYER、DRAG LAYER。
DEFAULT_LAYER is the bottom layer. If components added to DEFAULT_LAYER overlap with components of other layers, they are obscured by other components. The DRAG Layer layer is the top layer. If many components are added to the layering pane, you can place a component on the DRAG_Layer layer when you move the component. In this way, the component is not blocked by other components when you move the component. If components that are added to the same layer overlap, the components that are added later obscure the components that are added earlier. The layered pane invokes public void setLayer(Component c, int layer) to reset the layer where component c resides, and invokes public int getLayer(Component c) to obtain the number of layers where component c resides.
3.3 Common Layouts
➢ When adding a component to a container, you want to control the location of the component in the container, which requires learning about layout design.
➢ We will introduce the FlowLayout, BorderLayout, CardLayout, GridLayout layout classes in the java.awt package and the BoxLayout layout classes in the java.swing.border package.
➢ Containers can be used in the following ways:
setLayout(layout object);
to set your own layout and control the placement of components in the container.
1. FlowLayout Layout: is the default layout for JPanel-type containers.
1) Create a layout object: FlowLayout flow=new FlowLayout();
2) Container con uses layout objects: == con.setLayout(flow); ==
3) The con can use the add method provided by the Container class to add components to the container sequentially.
The FlowLayout layout object invokes the corresponding method to reset the alignment mode of the layout.
For example: public void setAlignment(int align)
2. BorderLayout Layout:
The BorderLayout layout is the default layout for Window containers.
If the container con in the BorderLayout layout is used, you can use the add method to add component b to the central area: con.add(b, BorderLayout.CENTER).
or con.add(BorderLayour.CENTER, b);
3. CardLayout Layout: The general steps to use CardLayout are as follows:
1) Create the CardLayout object CardLayout card=new CardLayout();
2) Set the layout con.setLayout(card) for the container.
3) The container calls add(String s, Component b) to add component b to the container, and gives the code s for displaying the component.
4) The layout object card uses the show() method provided by the CardLayout class to display the component code s in the container con:
card.show(con,s);
Containers using CardLayout can hold multiple components, but in reality, containers can only select one of these components at a time to display, like a stack of "playing cards" that can only display the most at a time. As in the previous one, the components shown will take up all the container space, in sequence.
4. GridLayout Layout:
The GridLayout layout strategy divides the container into several rows by several columns, and the components are located in these small grids. The general steps for the GridLayout Layout Editor are as follows:
1) Create a layout object and specify the number of rows (m) and columns for grid division.
GridLayout grid=new new GridLayout(10, 8);
2) Use the container of the GridLayout layout to call the add (Component c) method to add component c to the container.
5. null layout
You can set the layout of a container to null (empty layout). An empty layout container can accurately locate the position and size of components in the container. The setBounds(int a, int b, int width, int height) method is a method owned by all components. A component can call this method to set its size and position in the container.
For example, p is a container,
p.setLayout(null);
Set the layout of p to an empty layout.
Adding a component c to an empty layout container p requires two steps.
First, container p uses the add(c) method to add a component, and then component c calls the setBounds(int a, int b, int width, int height) method to set the location and size of the component in container p. Each component is a rectangular structure, and parameters a and b in the method are location coordinates of an upper left corner of the component c in the container p, that is, the component is a pixel from the left side of the container p, and b pixels from the upper side of the container p, width, and height is the width and height of component c.
The following example adds a tab pane in the center of the window with a grid layout panel and an empty layout panel:
Example.java
1
2
3
4
5public class Example {
public static void main(String[] args) {
new ShowLayout();
}
}
ShowLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26import java.awt.*;
import javax.swing.*;
public class ShowLayout extends JFrame {
PanelGridLayout panelGridLayout; // Panel for Grid Layout
PanelNullLayout panelNull; // Panel with empty layout
JTabbedPane p; // Tab Pane
ShowLayout() {
panelGridLayout = new PanelGridLayout();//Create a panel for a grid layout
panelNull = new PanelNullLayout();//Create a panel with an empty layout
p = new JTabbedPane();//Create a tab for selecting which panel layout
p.add("Panel for Grid Layout", panelGridLayout);// Adding a Grid Layout Board to the Tab Pane
p.add("Panel with empty layout", panelNull);// Add an empty layout panel to the Tab Pane
add(p, BorderLayout.CENTER);// Adding a Tab Pane to a ShowLayout Panel
// Add Buttons to Large Panels
add(new JButton("Form is BorderLayout Layout"), BorderLayout.NORTH);
add(new JButton("South"), BorderLayout.SOUTH);
add(new JButton("West"), BorderLayout.WEST);
add(new JButton("East"), BorderLayout.EAST);
setBounds(10, 10, 570, 390);
setVisible(true);// Window Visible
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
}
}
PanelGridLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20import java.awt.*;
import javax.swing.*;
public class PanelGridLayout extends JPanel {// Mesh cloth panel
PanelGridLayout() {
GridLayout grid = new GridLayout(12, 12); // Grid Layout
setLayout(grid);
Label label[][] = new Label[12][12];
for (int i = 0; i < 12; i++) {
for (int j = 0; j < 12; j++) {
label[j] = new Label();
if ((i + j) % 2 == 0)
label[j].setBackground(Color.black);
else
label[j].setBackground(Color.white);
add(label[j]);// Add a small mesh to the panel
}
}
}
}
PanelNullLayout.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16import javax.swing.*;
public class PanelNullLayout extends JPanel {// Blank Page Layout
JButton button;// “OK” button
JTextField text;// text box
PanelNullLayout() {
setLayout(null); // Empty Layout
button = new JButton("OK "); // Create “OK” button
text = new JTextField();//Create Text Box
add(text); // Add a text box to the PanelNullLayout panel
add(button); // Add a button to the PanelNullLayout panel
text.setBounds(100, 30, 90, 30); // Set Text Box Size
button.setBounds(190, 30, 66, 30); // Set Button Size
}
}
The following figure shows the running screenshot.
6. BoxLayout Layout
The class (static) method createHorizontalBox() of the Box class obtains a row-type box container.
Use the class (static) method createVerticalBox() of the Box class to obtain a column-type box container.
To control the distance between components in a box layout container, you need to use either horizontal or vertical braces.
In the following example, there are two column-type box containers, boxVOne, boxVTwo, and one row-type box container, boxH. Add boxVOne, boxVTwo to boxH and add horizontal braces between them.
Example.java
1
2
3
4
5
6
7
8public class Example {
public static void main(String[] args) {
WindowBoxLayout win = new WindowBoxLayout();
win.setBounds(100, 100, 310, 260);
win.setTitle("Nested Box Layout Container");
}
}
WindowBoxLayout .java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27import javax.swing.*;
public class WindowBoxLayout extends JFrame {
Box boxH; // Row Box
Box boxVOne, boxVTwo; // column box
public WindowBoxLayout() {
setLayout(new java.awt.FlowLayout());
init();
setVisible(true);
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
}
void init() {
boxH = Box.createHorizontalBox();//Get a row box container
boxVOne = Box.createVerticalBox();//Get a column-type box container
boxVTwo = Box.createVerticalBox();//Get a column-type box container
boxVOne.add(new JLabel("姓名:"));// Adding Name Labels to Column-Type Box Containers
boxVOne.add(new JLabel("职业:"));// Add occupational labels to column box containers
boxVTwo.add(new JTextField(10));// Add the name text box for column-shaped box containers.
boxVTwo.add(new JTextField(10));// Add career input box for column-shaped box containers
boxH.add(boxVOne);
boxH.add(Box.createHorizontalStrut(10));
boxH.add(boxVTwo);
add(boxH);
}
}
The command output is as follows:
By Codeplus.
Original link: https://blog.csdn.net/m0_46518461/article/details/115876407

Related

[Q] Importing and drawing moving images.

I am making an app that currently only make squares move on a "road" and make random desicions on where to go. Now I want to replace them with a image of a car.
I'm currently using the "onDraw(Canvas canvas)" method. confused
So how do I import images to the app.
And how do I do If I want it to animate with 2-3 images.
Easyer explained. I want to replace the drawRect with drawImage or drawBitmap and make it work. I want to load the image from the drawable folder.
Code:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square1.x, square1.y, square1.x + 20, square1.y + 20,
paint);
paint.setColor(android.graphics.Color.BLUE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square2.x, square2.y, square2.x + 20, square2.y + 20, paint);
paint.setColor(android.graphics.Color.GREEN);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square3.x, square3.y, square3.x + 20, square3.y + 20,
paint);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square4.x, square4.y, square4.x + 20, square4.y + 20,
paint);
paint.setColor(android.graphics.Color.YELLOW);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square5.x, square5.y, square5.x + 20, square5.y + 20,
paint);
Generally the easiest way to animate a limited number of frames on any system is using clip rectangles and a single image that contains the frames side by side... depending on the system this doesn't even create a whole let of overhead if you use an uncompressed format like TGA or BMP, laid out vertically. Is there anything specific you need to know about that... or is your problem with the timing? You could just use the nanotime and decide which frame to draw based on y=(time%fullAnimationTime)/frameTime*tileHeight
That will be my problem in the future.
But right now I don't know how to even import and use images properly.
I'm very new at programming for android. Have only programmed a little java before
Just read up a little on this... seems AnimationDrawable actually does exactly what you need, just look it up in the reference.
If you still want to do the side-by-side thing I suggested first, here's some code to get you started, which uses assets to access a BMP. You still need a timer to actually cause the drawing, but I guess you've already set that up:
Code:
Bitmap animBmp=null;
try {
InputStream animStream=null;
animStream=this.getAssets().open("anim.bmp");
BufferedInputStream animBufStream = new BufferedInputStream(animStream);
animBmp = BitmapFactory.decodeStream(animBufStream);
animBufStream.close();
animStream.close();
} catch (IOException e) {
e.printStackTrace();
}
class AnimView extends View{
public int frameTime=100;
public int frames=-1;
public int frameWidth=0;
public int frameHeight=0;
public Bitmap animBmp=null;
public Paint paint=null;
private int lastFrame=-1;
public AnimView(Context ctx, Bitmap bmp,int frms) {
super(ctx);
animBmp=bmp;
frames=frms;
frameWidth=bmp.getWidth();
frameHeight=bmp.getHeight()/frames;
paint=new Paint();
}
protected void onDraw(Canvas c){
Log.i("draw", "draw");
int frame=(int)((SystemClock.elapsedRealtime()%(frameTime*frames))/frameTime);
if(frame!=lastFrame){
c.drawBitmap(animBmp, new Rect(0,frame*frameHeight,frameWidth,(frame+1)*frameHeight), new Rect(0,0,frameWidth,frameHeight),paint);
lastFrame=frame;
}
}
};
AnimView v=new AnimView(this,animBmp,3);
It says that
"animStream=this.getAssets().open("anim.bmp");"
The method getAssets() is undefined for the type MyTimedView
I am a real noob at this. Where am I suppose to set my test.bmp? (The image is named test) Which variables should I change for it to understand that it always should use the "test" image?
Also. I have a image of a house that I want to be placed on the screen. And this that you gave me doesn't help me there I think?
Thanks for all your help!!
getAssets operates on the Activity, not the View.
Change this line to your bitmap's path:
animStream=this.getAssets().open("anim.bmp");
in your case
animStream=this.getAssets().open("test.bmp");
You'll also have to change this line to match the actual number of frames:
AnimView v=new AnimView(this,animBmp,3);
And lastly, the file (test.bmp) should be inside the ASSETS directory of your eclipse project.
Ok I have done everything you have said. I made 3 images and stored them in assets. Named test1, test2, test3. But now I only get a white screen. I had some stuff from the beginning but they are now gone. And I don't get a image or anything.
Even more complex. How do I set the X and Y values to the animation?
It's supposed to be one image with the frames ontop of each other. Give me a second... I'll pack it into a project and attach it.
Here you go. I added a few comments:
Code:
package net.tapperware.simpleanimation;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
public class SimpleAnimationActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//This will hold our single bitmap which will contain all frames stacked ontop of each other
Bitmap animBmp=null;
try {
//First, load the file: This is supposed to be in the assets folder of the project
InputStream animStream=this.getAssets().open("anim.bmp");
//BitmapFactory needs a BufferedInputStream, so let's just convert our InputStream
BufferedInputStream animBufStream = new BufferedInputStream(animStream);
//OK, now we decode it to an Android-Bitmap
animBmp = BitmapFactory.decodeStream(animBufStream);
//And finally, we clean up
animBufStream.close();
animStream.close();
} catch (IOException e) {
e.printStackTrace();
}
//This will be used to operate the loop... looping on Android needs a Runnable, and this class provides it. When
//run, it will invalidate the view, which causes onDraw to be called.
class AnimLoop implements Runnable{
private View view;
AnimLoop(View v){ view=v; }
@Override public void run() { view.invalidate(); }
}
//And here the actual view
class AnimView extends View{
//Time between frames
public int frameTime=100;
//Number of frames
public int frames=-1;
//Width of each frame
public int frameWidth=0;
//Height of each frame
public int frameHeight=0;
//The previous frame number shown
public int lastFrame=0;
//This is our Runnable that will call invalidate()
public AnimLoop loop=null;
//Of course, the actual Bitmap
public Bitmap animBmp=null;
//Just a default Paint object, since it's required by drawBitmap
public Paint paint=null;
public AnimView(Context ctx, Bitmap bmp,int frms) {
super(ctx);
//Assign the args to our attributes
animBmp=bmp;
frames=frms;
//Since the images are stacked ontop of each other, each frame is widthOfBitmap * (heightOfBitmap / numberOfFrames )
frameWidth=bmp.getWidth();
frameHeight=bmp.getHeight()/frames;
//And finally, our dummy height
paint=new Paint();
//And the Runnable that will cause invalidate to be called
loop=new AnimLoop(this);
}
protected void onDraw(Canvas c){
//Draw the rect that corresponds to the current frame
c.drawBitmap(
animBmp
//We need to move down by frame number*the frame index
,new Rect(0,lastFrame*frameHeight,frameWidth,(lastFrame+1)*frameHeight)
//This is our target rect... draw wherever you want
,new Rect(0,0,frameWidth,frameHeight)
//And our standard paint settings
,paint);
//Increase the frame number by one, if it's >= the total number of frames, make it wrap back to 0
lastFrame=(lastFrame+1)%frames;
//And ask for the next frame to be drawn in frameTime milliseconds.
this.getHandler().postDelayed(loop, frameTime);
}
};
//Arguments: Activity/Context , Bitmap, Frame Count
AnimView v=new AnimView(this,animBmp,3);
setContentView(v);
}
}
[2011-08-21 01:53:39 - SimpleAnimation] ERROR: Application requires API version 10. Device API version is 8 (Android 2.2).
[2011-08-21 01:53:39 - SimpleAnimation] Launch canceled!
Right now I feel like a 10 year old that doesn't understand how to spell.
Any quickfix to make it 2.2 compatible?
Yep, go to Project /Properties /Android and set the checkmark somewhere else... the code is not using any of it, I just set it to something. Any 2.x should be fine, probably even 1.x
Thanks for all your awsome work!
But I have 1 problem. The picture. anim.bmp never gets placed on the screen. I have altered the x and y values but it seems like it's never getting loaded. I have changed all that I thought needed to be changed.
Lets keep this simple. After I am done with the above. I want to make 1 image on a specific x and y. And I want it to be clickable. Nobody really goes into detail about how to even get a picture into the app. Everyone believes it's so simple...
That's... troubling. You mean the unchanged project right? It works perfectly here and I can't see anything that would cause issues with the code, so it must be the project setup. Could you open the compiled APK with WinRAR or 7zip and verify that there is indeed a folder named assets, containing anim.bmp?
It's supposed to appear at 0,0... to move it, you'd have to change this line
,new Rect(0,0,frameWidth,frameHeight)
to
,new Rect(desiredX,desiredY,desiredX+frameWidth,desiredY+frameHeight)
I'm not really qualified to tell you how to work with resources... I just plain don't like the system, so I usually work around it and use assets instead, which are simply put into the assets folder and that's the end of it (BUT, they don't have any automatic resolution handling, so you have to implement that yourself).
Yay it worked! If I would want to use this code for my app, do I create a new class for it or just dump everything in activitys? Because right now when I dumped it in activitys it just made a white screen. No animation or anything. It's just printing out the backround.
Phew! Your code will usually consist of a bunch of classes which inherit from View, which should each be contained in their own file. This AnimView will just be one more class among your collection.
According to the guidelines, you should then reference your Classes in res/layout/*.xml, but I've never done that (this strange, broken thing that Android calls XML gives me a headache), so well... you've got to find out how to do that without my help.
Why does it still give me a white screen? It is now back to the original and no new view is implemented.
I know my code looks horrible.
You load R.layout.main... but from what I can tell, there's nothing in there?
It is suppose to be loaded I think. If you mean when it is activitys.
And yes it isn't that much code. I recently started.
Where?:
StepByStepActivity:
Code:
public class StepByStepActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
(...)
setContentView(R.layout.main);
}
}
This loads R.layout.main... as far as I can tell, that's the only thing being executed on load, aside from a few config changes.
res/layout/main.xml:
Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
(...) android:id="@+id/mainlayout" (...)>
</LinearLayout>
That defines a linear layout.... but where is its content
This is the content. 5 squares and some lines drawed so it would be easyer to see where they are suppose to go.
Code:
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
paint.setColor(android.graphics.Color.RED);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square1.x, square1.y, square1.x + 20, square1.y + 20,
paint);
paint.setColor(android.graphics.Color.BLUE);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square2.x, square2.y, square2.x + 20, square2.y + 20, paint);
paint.setColor(android.graphics.Color.GREEN);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square3.x, square3.y, square3.x + 20, square3.y + 20,
paint);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square4.x, square4.y, square4.x + 20, square4.y + 20,
paint);
paint.setColor(android.graphics.Color.YELLOW);
paint.setStyle(Paint.Style.FILL);
canvas.drawRect(square5.x, square5.y, square5.x + 20, square5.y + 20,
paint);
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(95, 200, 650, 200, paint);
// 6 Vågrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(100, 200, 100, 480, paint);
// 5 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(650, 200, 650, 480, paint);
// 4 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(600, 0, 600, 200, paint);
// 3 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(340, 200, 340, 480, paint);
// 2 lodrät
paint.setStrokeWidth(3);
paint.setColor(android.graphics.Color.BLACK);
paint.setStyle(Paint.Style.FILL);
canvas.drawLine(180, 0, 180, 200, paint);
// 1 lodrät
}

OpenGL 2.0 shader not working on Galaxy S2

I've done a lot of coding of shader with GL and DX over the years so I thought I'll knock up a simple rotating cube on my phone. For me this really is a 10 minute job. 2 hours later I was stumped..........
The shader is just a transforms of the xyz and copy of the vertex colour. This works fine with one matrix. When I separated the model matrix out so now I upload a view*invcam matrix and a model matrix it all failed. After some hacking about I got to a point where just adding the one extra mat4 uniform stopped the shader from working. Feels like a bug in the OS, anyone know of Samsung GLES 2.0 shader bugs? I'm at work at the moment but later I'll post the shader. It really is "my first shader" simple stuff.
Here is my pixel and vertex shaders, very simple.
If I uncomment out the line "uniform mat4 trans;" the shader stops working.
Code:
precision highp float;
uniform mat4 proj_cam; //This is the projection * invcam matrix.
//uniform mat4 trans; //This is model to world space.
attribute vec4 vPosition;
attribute vec4 vColour;
varying vec4 v_colour; //The colour that is passed to the pixel shader.
void main()
{
gl_Position = proj_cam * vPosition;
v_colour = vColour;
}
Code:
precision mediump float;
varying vec4 v_colour; //The colour that is passed to the pixel shader.
void main()
{
gl_FragColor = v_colour;
}
So if I change the shader to use an array of matrices and upload the two matrices from one float[32] array it works...
Code:
precision highp float;
uniform mat4 proj_cam[2];
attribute vec4 vPosition;
attribute vec4 vColour;
varying vec4 v_colour;
void main()
{
gl_Position = proj_cam[0] * (proj_cam[1] * vPosition);
v_colour = vColour;
}
I smell an OS bug............
Questions or Problems Should Not Be Posted in the Development Forum
Please Post in the Correct Forums
Moving to Q&A
Sorry, I don't understand. This is a software development question, why move to the Q&A thread? Did I not post it in the software development forum?????
Just rewrote my test application in c/c++ (I think that is software development? ) with the NDK and it works so Samsung must have broken the GL2 mapping to the native code side in their version of the API.
I don't really need to work in c/c++ so thought using Java would allow rapid prototyping. Guess not seeing it's broken...........

[Q] OnClickListener vs. android:OnClick

I'm looking at both a book and the beginning projects on developers.android.com and have a question. Google is utilizing the android:nClick to handle the click event which then points to a public method for execution....but the book is setting up click listeners by setting a View with the buttons ID and then executing the setOnClickListener. OnClickListenver has one method called OnClick which is then executed.
//book
public class MyGame extends Activity implements OnClickListener {
View continueButton = findViewById(R.id.continue_button);
continueButton.setOnClickListener(this);
View newButton = findViewById(R.id.new_button);
newButton.setOnClickListener(this);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
View exitButton = findViewById(R.id.exit_button);
exitButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.about_button:
Intent i = new Intent(this, About.class);
startActivity(i);
break;
}
}
My question. Which is the better way to handle the Button Click? In looking at what google does in the XML, it seems a helluva lot cleaner....but curious on what the advantage is for the other way (and I cannot think of any). Search of the site didn't yield anything.
I dunno if the methods have advantages between them, however, I prefer the androidnClick method since the code looks clearer
You usually want to use the onClick method, as there is no benefit to implementing the OnClickListener (only more effort and possible performance loss).
If you want things like long clicks or other events, you will have to implement that (or define an inline listener), as there is no xml tag for those.
I think the OnClickListener is actually only there for consistency with these other classes

How does one use a seekbar to cycle through an array?

I am currently trying to make a seekbar cycle through colors on progress(when moved). I have an xml file with some defined colors within it, this is what i have tried so far.
seekbarcolor.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
int seekbarprogress = 0;
@override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
seekbarprogress = progress;
int colorset = Color.argb(0xff, progress*0x10000, progress*0x100, progress)+0xff000000;
drawPaint.setColor(colorset);
}
This code generates and uses only 1 base color. I want to instead pull from an xml file or an array defined in the class. How do i do this?

Several Easy Steps to Integrate the Fundamental Capability SDK of Video Editor Kit

The launch of HMS Core Video Editor Kit 6.2.0 has brought two notable highlights: various AI-empowered capabilities and flexible integration methods. One method is to integrate the fundamental capability SDK, which is described below.
Preparations​For details, please check the official document.
Code Development​Configuring a Video Editing Project
1. Set the authentication information for your app through an API key or access token.
Use the setAccessToken method to set an access token when the app is started. The access token needs to be set only once.
Code:
MediaApplication.getInstance().setAccessToken("your access token");
Use the setApiKey method to set an API key when the app is started. The API key needs to be set only once.
Code:
MediaApplication.getInstance().setApiKey("your ApiKey");
2. Set a License ID.
This ID is used to manage your usage quotas, so ensure that the ID is unique.
Code:
MediaApplication.getInstance().setLicenseId("License ID");
3. Initialize the running environment for HuaweiVideoEditor.
When creating a video editing project, first create a HuaweiVideoEditor object and initialize its running environment. When exiting a video editing project, release the HuaweiVideoEditor object.
Create a HuaweiVideoEditor object.
Code:
HuaweiVideoEditor editor = HuaweiVideoEditor.create(getApplicationContext());
Specify the position for the preview area.
This area renders video images, which is implemented by creating SurfaceView in the fundamental capability SDK. Ensure that the preview area position on your app is specified before creating this area.
Code:
<LinearLayout
android:id="@+id/video_content_layout"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="@color/video_edit_main_bg_color"
android:gravity="center"
android:orientation="vertical" />
// Specify the preview area position.
LinearLayout mSdkPreviewContainer = view.findViewById(R.id.video_content_layout);
// Set the layout of the preview area.
editor.setDisplay(mSdkPreviewContainer);
Initialize the running environment. If the license verification fails, LicenseException will be thrown.
After the HuaweiVideoEditor object is created, it has not occupied any system resource. You need to manually set the time for initializing the running environment of the object. Then, necessary threads and timers will be created in the fundamental capability SDK.
Code:
try {
editor.initEnvironment();
} catch (LicenseException error) {
SmartLog.e(TAG, "initEnvironment failed: " + error.getErrorMsg());
finish();
return;
}
4. Add a video or image.
Create a video lane and add a video or image to the lane using the file path.
Code:
// Obtain the HVETimeLine object.
HVETimeLine timeline = editor.getTimeLine();
// Create a video lane.
HVEVideoLane videoLane = timeline.appendVideoLane();
// Add a video to the end of the video lane.
HVEVideoAsset videoAsset = videoLane.appendVideoAsset("test.mp4");
// Add an image to the end of the video lane.
HVEImageAsset imageAsset = videoLane.appendImageAsset("test.jpg");
5. Add audio.
Create an audio lane and add audio to the lane using the file path.
Code:
// Create an audio lane.
HVEAudioLane audioLane = timeline.appendAudioLane();
// Add an audio asset to the end of the audio lane.
HVEAudioAsset audioAsset = audioLane.appendAudioAsset("test.mp3");
6. Add a sticker and text.
Create a sticker lane and add a sticker and text to the lane. A sticker needs to be added using its file path, while the text needs to be added by specifying its content.
Code:
// Create a sticker lane.
HVEStickerLane stickerLane = timeline.appendStickerLane();
// Add a sticker to the end of the lane.
HVEStickerAsset stickerAsset = stickerLane.appendStickerAsset("test.png");
// Add text to the end of the lane.
HVEWordAsset wordAsset = stickerLane.appendWord("Input text",0,3000);
7. Add a special effect.
Special effects are classified into the external special effect and embedded special effect.
Add an external special effect to an effect lane. This special effect can be applied to multiple assets, and its duration can be adjusted.
Code:
// Create an effect lane.
HVEEffectLane effectLane = timeline.appendEffectLane();
// Create a color adjustment effect with a duration of 3000 ms. Add it to the 0 ms playback position of the lane.
HVEEffect effect = effectLane.appendEffect(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), 0, 3000);
Add an embedded special effect to an asset. Such a special effect can be applied to a single asset. The special effect's duration is the same as that of the asset and cannot be adjusted.
Code:
// Create an embedded special effect of color adjustment.
HVEEffect effect = videoAsset.appendEffectUniqueOfType(new HVEEffect.Options(HVEEffect.EFFECT_COLORADJUST, "", ""), ADJUST);
8. Play a timeline.
To play a timeline, specify its start time and end time. The timeline will play from its start time to end time at a fixed frame rate, and the image and sound in the preview will play simultaneously. You can obtain the playback progress, playback pause, payback completion, and playback failure via the registered callback.
Code:
// Register the playback progress callback.
editor.setPlayCallback(callback);
// Play the complete timeline.
editor.playTimeLine(timeline.getStartTime(), timeline.getEndTime());
9. Export a video.
After the editing is complete, export a new video using the assets in the timeline via the export API. Set the export callback to listen to the export progress, export completion, or export failure, and specify the frame rate, resolution, and path for the video to be exported.
Code:
// Path for the video to be exported.
String outputPath =
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
+ File.separator + Constant.LOCAL_VIDEO_SAVE_PATH
+ File.separator + VideoExportActivity.getTime() + ".mp4";
// Resolution for the video to be exported.
HVEVideoProperty videoProperty = new HVEVideoProperty(1920, 1080);
// Export the video.
HVEExportManager.exportVideo(targetEditor, callback, videoProperty, outputPath);
Managing Materials
After allocating materials, use APIs provided in the on-cloud material management module to query and download a specified material. For details, please refer to the description in the official document.
Integrating an AI-Empowered Capability
The fundamental capability SDK of Video Editor Kit provides multiple AI-empowered capabilities including AI filter, track person, moving picture, and AI color, for integration into your app. For more details, please refer to the instruction in this document.
AI Filter
Lets users flexibly customize and apply a filter to their imported videos and images.
Code:
// Create an AI algorithm engine for AI filter.
HVEExclusiveFilter filterEngine = new HVEExclusiveFilter();
// Initialize the engine.
mFilterEngine.initExclusiveFilterEngine(new HVEAIInitialCallback() {
@Override
public void onProgress(int progress) {
// Initialization progress.
}
@Override
public void onSuccess() {
// The initialization is successful.
}
@Override
public void onError(int errorCode, String errorMessage) {
// The initialization failed.
}
});
// Create an AI filter of the extract type from an image, by specifying the image bitmap and filter name.
// The filter ID is returned. Using the ID, you can query all information about the filter in the database.
String effectId = mFilterEngine.createExclusiveEffect(bitmap, "AI filter 01");
// Add the filter for the first 3000 ms segment of the effect lane.
effectLane.appendEffect(new HVEEffect.Options(
HVEEffect.CUSTOM_FILTER + mSelectName, effectId, ""), 0, 3000);
Color Hair
Changes the hair color of one or more persons detected in the imported image, in just a tap. The color strength is adjustable.
Code:
// Initialize the AI algorithm for the color hair effect.
asset.initHairDyeingEngine(new HVEAIInitialCallback() {
@Override
public void onProgress(int progress) {
// Initialization progress.
}
@Override
public void onSuccess() {
// The initialization is successful.
}
@Override
public void onError(int errorCode, String errorMessage) {
// The initialization failed.
}
});
// Add the color hair effect by specifying a color and the default strength.
asset.addHairDyeingEffect(new HVEAIProcessCallback() {
@Override
public void onProgress(int progress) {
// Handling progress.
}
@Override
public void onSuccess() {
// The handling is successful.
}
@Override
public void onError(int errorCode, String errorMessage) {
// The handling failed.
}
}, colorPath, defaultStrength);
// Remove the color hair effect.
asset.removeHairDyeingEffect();
Moving Picture
Animates one or more persons in the imported image, so that they smile, nod, or more.
Code:
// Add the moving picture effect.
asset.addFaceReenactAIEffect(new HVEAIProcessCallback() {
@Override
public void onProgress(int progress) {
// Handling progress.
}
@Override
public void onSuccess() {
// The handling is successful.
}
@Override
public void onError(int errorCode, String errorMessage) {
// The handling failed.
}
});
// Remove the moving picture effect.
asset.removeFaceReenactAIEffect();
This article presents just a few features of Video Editor Kit. For more, check here.
To learn more, please visit:
>> HUAWEI Developers official website
>> Development Guide
>> Reddit to join developer discussions
>> GitHub to download the sample code
>> Stack Overflow to solve integration problems
Follow our official account for the latest HMS Core-related news and updates.

Categories

Resources