Last active
December 12, 2019 04:50
-
-
Save miracleyoo/d221900457cbebd4d064181beb52020c to your computer and use it in GitHub Desktop.
[Swing None Frame ] #Java #GUI #Swing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* A Frame class which provide a boarder-less Frame. | |
* Also, drag the window by mouse event is implemented. | |
* */ | |
package com.miracleyoo.utils; | |
import java.awt.Color; | |
import java.awt.Point; | |
import java.awt.event.*; | |
import javax.swing.*; | |
public class NoneFrame extends JFrame { | |
private static final long serialVersionUID = 1L; | |
private Point pressedPoint; | |
public NoneFrame() { | |
this.getContentPane().setBackground(new Color(195, 184, 162)); // Set background color | |
this.setUndecorated(true); // Invalidate window decoration | |
this.getContentPane().setLayout(null); // Window use absolute layout | |
this.setLocationRelativeTo(null); // Move window to the center | |
this.setAlwaysOnTop(true); // Show window on the top | |
/* | |
Drag window by mouse | |
*/ | |
this.addMouseListener(new MouseAdapter() { | |
public void mousePressed(MouseEvent e) { | |
pressedPoint = e.getPoint(); // Record mouse position | |
} | |
}); | |
this.addMouseMotionListener(new MouseMotionAdapter() { | |
// Mouse drag event | |
public void mouseDragged(MouseEvent e) { | |
Point point = e.getPoint(); // Get current position | |
Point locationPoint = getLocation(); // Get window location | |
int x = locationPoint.x + point.x - pressedPoint.x; // Compute the new position after dragging | |
int y = locationPoint.y + point.y - pressedPoint.y; | |
setLocation(x, y);// Change the position of window | |
} | |
}); | |
this.setTitle(""); | |
this.setVisible(true); | |
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
} | |
} | |
////////////////////////////////////////////////////////////////////////////// | |
/////////////////////////// USAGE //////////////////////////////// | |
//////////// Attention: Used with BackgroundPanel Class //////////////// | |
////////////////////////////////////////////////////////////////////////////// | |
/** | |
* Functionality: A cool welcome interface. It will ask user to select | |
* a *.s file and parse the file into a list of map. Each line will be | |
* put into the corresponding array as a item. | |
* Feature: It initialize a window which is boarder-less and mouse-drag-able. | |
* Also, a automatically scalable background image is added. | |
* */ | |
package com.miracleyoo.UIs; | |
import com.miracleyoo.utils.BackgroundPanel; | |
import com.miracleyoo.utils.NoneFrame; | |
import com.miracleyoo.utils.ParseFile; | |
import com.miracleyoo.utils.UICommonUtils; | |
import javax.imageio.ImageIO; | |
import javax.swing.*; | |
import java.awt.*; | |
import java.awt.event.ActionEvent; | |
import java.awt.event.ActionListener; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import java.io.IOException; | |
import java.nio.file.Paths; | |
import java.util.List; | |
import java.util.Map; | |
public class CoolMainUI { | |
private BackgroundPanel MainPanel; // The main panel. | |
private CoolMainUI() throws IOException { | |
// Initialize the MainPanel | |
BufferedImage img = null; | |
img = ImageIO.read(new File("Assets/image_01.jpg")); | |
MainPanel = new BackgroundPanel(img, BackgroundPanel.SCALED, 1.0f, 0.5f); | |
GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, 600, 0, Color.RED); | |
MainPanel.setPaint(paint); | |
// Initialize the FileSelectedCap | |
FileSelectedCap = new JLabel("<html><font color='white'>Welcome! Please select a *.s file to start!</font></html>"); | |
FileSelectedCap.setHorizontalAlignment(SwingConstants.CENTER); | |
FileSelectedCap.setVerticalAlignment(SwingConstants.CENTER); | |
FileSelectedCap.setFont(new Font("Dialog", Font.BOLD, 20)); | |
// Initialize the ChooseFileBtn | |
ChooseFileBtn = new JButton(); | |
ChooseFileBtn.setText("<html><font color='white'>Select a *.s file to start</font></html>"); | |
ChooseFileBtn.setFont(new Font("Dialog", Font.BOLD, 15)); | |
ChooseFileBtn.setOpaque(false); | |
ChooseFileBtn.setContentAreaFilled(false); | |
ChooseFileBtn.setBorderPainted(false); | |
// Initialize the ExitBtn | |
ExitBtn = new JButton(); | |
ExitBtn.setText("<html><font color='white'>×</font></html>"); | |
ExitBtn.setFont(new Font("Dialog", Font.PLAIN, 12)); | |
ExitBtn.setOpaque(false); | |
ExitBtn.setContentAreaFilled(false); | |
ExitBtn.setBorderPainted(false); | |
// Action on ChooseFileBtn | |
ChooseFileBtn.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
FileDialog fd = new FileDialog(new JFrame(), "Choose a file", FileDialog.LOAD); | |
System.out.println(System.getProperty("user.dir")); | |
// Set the default folder is a subfolder of current package root path. | |
fd.setDirectory(Paths.get(System.getProperty("user.dir"), "asm_code").toString()); | |
fd.setFilenameFilter((dir, name) -> name.endsWith(".s")); | |
fd.setVisible(true); | |
String fileName = fd.getFile(); | |
String filePath = fd.getDirectory(); | |
if (fileName == null) | |
System.out.println("You cancelled the choice"); | |
else | |
System.out.println("You chose " + filePath + fileName); | |
try { | |
// Load and try to parse the file | |
selectedFileName = filePath + fileName; | |
listFlagMap = ParseFile.parseFile(new File(selectedFileName)); | |
Object[][] operandListArray = listFlagMap.get("textList").toArray(new Object[0][0]); | |
Object[][] dataListArray = listFlagMap.get("dataList").toArray(new Object[0][0]); | |
DataUIFrame = new DataUI(operandListArray, dataListArray); | |
MainFrame.dispose(); | |
} catch (IOException ex) { | |
ex.printStackTrace(); | |
} | |
} | |
}); | |
// Action on ExitBtn: Exit the program | |
ExitBtn.addActionListener(new ActionListener() { | |
@Override | |
public void actionPerformed(ActionEvent e) { | |
System.exit(0); | |
} | |
}); | |
} | |
public static void main(String[] args) throws IOException { | |
// Initialize the MainFrame and set bounds | |
CoolMainUI coolMainUI = new CoolMainUI(); | |
MainFrame.setSize(frameSize[0], frameSize[1]); | |
UICommonUtils.makeFrameToCenter(MainFrame); | |
MainFrame.setContentPane(coolMainUI.MainPanel); | |
MainFrame.getContentPane().setLayout(null); | |
// Set bounds of the components | |
coolMainUI.FileSelectedCap.setBounds(0, 10, MainFrame.getWidth() - 20, 30); | |
coolMainUI.ExitBtn.setBounds(MainFrame.getWidth() - 30, 0, 30, 22); | |
coolMainUI.ChooseFileBtn.setBounds(0, MainFrame.getHeight() - 60, MainFrame.getWidth(), 60); | |
// Add components to the MainFrame | |
MainFrame.getContentPane().add(coolMainUI.ChooseFileBtn); | |
MainFrame.getContentPane().add(coolMainUI.ExitBtn); | |
MainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
MainFrame.setVisible(true); | |
MainFrame.setResizable(false); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment