Some gains from developing the Java application "continuous view" in eclipse

Source: Internet
Author: User

Write code when you are free. In this simple program world, I can also find happiness!

Soon after I learned Java, I was eager to try it before I finished reading Java programming ideas. Haha.

I spent two hours every day to solve the following problems step by step. Share ^_^

 

<1> interface. The simpler question is → how can I display a window in Java?
You can use Java. AWT and javax. Swing packages!
Create a javaproject using eclipse. You can see the two packages in jresystem Library [JavaSE-1.7]> Rt. jar.
I have used a lot of interface components in <continuous View>, and I will summarize them as follows:
Jframe window: Contains icons, minimize the maximum close button, etc.
Jpanel canvas: Basic. You can set various la S.
Jscrollpane: the scroll bar is automatically generated when the content exceeds the page size.
Jsplitpane partition canvas: You can divide the interface into two columns.
Jlayeredpane hierarchical canvas: very easy to use. To set the background image, use this
Flowlayout floating layout: the most basic layout
Borderlayout edge layout: divided into east, west, north, and South
Cardlayout card layout: Very easy to use! Switch the screen with this
Formlayout table layout: very powerful! You can set the size of each column in each row.
Gridlayout grid layout: the interface is evenly separated into grids. This is used for picture links.
(Each canvas has its own layout. Layout affects the layout of child components on the canvas)
(Components)
Jlabel label: displays text and images.
Jbutton: usually sets the listener actionlistener.
Jtextpane: displays an article that can be edited during runtime.
Jlist: You can use listmodel to dynamically manage elements in the list.
Jprogressbar progress bar: Set max to Set Value

[2] interface events
Many events can be monitored on the interface.
Componentlistener: monitors events such as window movement, display, and size changes.
Actionlistener: Click Event of the listener button
Windowlistener: Listener window close and other events
Mouselistener: Mouse Click Event
Mousemotionlistener: events such as mouse movement, entry, and exit

 

[3] Is it inconvenient to use the code to "write" the interface?
Install the windowbuilder plug-in!
: Http://www.eclipse.org/windowbuilder/download.php
Eclipse menu help> install new software> Add select the downloaded ZIP file for Installation
After the installation is complete, restart eclipse and select your own interface class (for example, inheriting the jframe class) Java file.
Right-click> open with> window builder Editor
You can open the visual interface design.
There are other plug-in tools on the Internet. I will not introduce them if I have never used them.

 

[4] database
I am using a lightweight database SQLite
Use sqlitestudio-2.0.21.exe to create a database file (suffix. DB)
When a database is used, we also need to allow our programs to connect to the database,
This requires a database driver, so we need to import a package sqlite-jdbc-3.7.2.jar
Http://www.xerial.org/maven/repository/artifact/org/xerial/sqlite-jdbc/3.7.2/
How to import the jar package to your project?
Right-click Java Project Name> Properties> JAVA build path> libraries> Add jars...
How to load the database driver? How to connect to the database? How to execute SQL statements?

Class.forName("org.sqlite.JDBC");Connection conn = DriverManager.getConnection( "jdbc:sqlite:"+GameConfig.DB_NAME ); Statement stat = conn.createStatement();stat.execute(sql);

 

[5] the serialization algorithm? How can I determine whether two points can be deleted?
I believe that various algorithms are available on the Internet.
Here I want to talk about my own idea: two vertices A (3, 2) B (3, 8)
How can we determine whether a connection can be established?
Idea: There are five [3, 4, 5, 6, 7] horizontal lines between the two points. How can we determine whether there is a "channel" between the AB nodes that can pass through these five lines?
The simplest operation: bitwise operation! I think this is the fastest and easiest.
The entire interface is represented by a char array. the lattice of an image is represented by 1, and the lattice of no image is represented by 0. The following is the initial matrix of 10*10.

0000000000001111 //matrix[0]0111111111101111 //matrix[1]0111111111101111 //matrix[2]0111111111101111 //matrix[3]0111111111101111 //matrix[4]0111111111101111 //matrix[5]0111111111101111 //matrix[6]0111111111101111 //matrix[7]0111111111101111 //matrix[8]0111111111101111 //matrix[9]0000000000001111 //matrix[10]

 

Char [] matrix = new char [10]; char temp = 0x0000; For (INT I = 3; I <= 7; ++ I) {temp | = matrix [I]; // bitwise OR operation} If (temp = 0 xFFFF) {; // No channel} else {; // channel available}

The above is only part of the algorithm, but the problem can be solved.
[6] how to scale an image according to the size of the jlabel?
Inherit jlabel, find the project folder in the workspaceof eclipse, and put the image beauty.jpg

Public class imagejlabel extends jlabel {image IMG = ImageIO. read (new file ("Res/beauty.jpg"); // catch exception @ override public void paintcomponent (Graphics g) {G. drawimage (IMG, 0, 0, this. getwidth (), this. getheight (), this );}

[7] database exception: resultsetis type_forward_only
The statement object is used to execute SQL statements. The instance can be obtained through the createstatement () method.
Statement statement = connection. createstatement ();
Run the SELECT statement to obtain a result set resultset.
You need to judge whether the result set is empty after regular queries. For example:
Resultset rs = statement.exe cutequery ("select * from table ");
If (Rs. First ()){...}
Then, an exception will be reported, because the result set cursor obtained and queried by using the createstatement () method can only be moveforward
The constant indicating the type for a resultset object whose cursor may moveonly forward.
There are two solutions:
1: statementstatement = connection. createstatement (resultset. type_scroll_insensitive, resultset. concur_updatable );
2: If (Rs. Next () {...} // use next () instead of first ()

 

<8> How do I generate images corresponding to each other?

My image name is simple: 001.jpg, 002.jpg, 003.jpg, and so on

The question is, how can we obtain the numbers corresponding to two pairs?

The following are my own classes.

package com.yarkey.tools;public class IntClass {private int value;private int num;public IntClass( int value ){this.value = value;num = 0;}public int add( int i ){num += i;return num;}public int minus( int i ){num -= i;return num;}public int getValue(){return value;}public int getNum(){return num;}}

 

package com.yarkey.tools;import java.util.ArrayList;import java.util.Random;public class RandomType {private static final String TAG = "RandomType";Random random = new Random();ArrayList<IntClass> typeList = new ArrayList<IntClass>();private final int type;private final int numOfPerType;public RandomType( int type, int numOfPerType ){this.type = type;this.numOfPerType = numOfPerType;for( int i=0; i<type; ++i ){typeList.add( new IntClass(i) );}}public int get(){int rd = random.nextInt( typeList.size() );int rdType = typeList.get(rd).getValue();if( typeList.get(rd).add(1) >= numOfPerType ){typeList.remove(rd);Log.d(TAG, "typeList.remove("+rd+") >>> type="+rdType, 15);}return rdType;}public void reset(){typeList.clear();for( int i=0; i<type; ++i ){typeList.add( new IntClass(i) );}}}

Type: number of images

Numofpertype: the number of times each image appears.

Okay. If we have 25 images in 10x10 for a continuous view, then each image appears four times.

We generate random numbers in sequence, and distribute them to the interface positions ranging from 0 to 99.

RandomType randomType = new RandomType( type, blocks/type );
for( int i=0; i<100; ++i ){
    int randomInt = randomType.get();
; // Select an image or other operations
}

 

This is my first article on csdn. Sorry, the layout Seems messy. I am not familiar with this editor.

It's time to write so much at night. I have limited capabilities. You are welcome to contact us and criticize and correct me.

Good night world!

 

Http://www.cnblogs.com/bastard/archive/2013/01/25/2876047.html

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.