/*
updated 2/4/2021 simplest version
*/
package jim3d;
import java.awt.Color;
import java.awt.Component;
import java.awt.Frame;
import java.awt.Panel;
import java.awt.TextArea;
import java.awt.TextField;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
/**
*
* @author jimakoskx
*/
public class Spl extends Panel implements ComponentListener,MouseListener,MouseMotionListener,KeyListener{
public static void main(String[] args) {
Frame fr=new Frame();
fr.setBounds(0,0,555,555);
fr.setVisible(true);
Spl s=new Spl();
fr.add(s);
fr.validate();
TextField tf=new TextField("hfgh");
TextArea ta=new TextArea("Area");
s.add(tf);
s.add(ta);
s.resetLayout();
s.setDividerSize(6);
}
private int orientation=1;
private double divloc=0.2;
private int firstsize=1;
private int divsizehalf=5;
private int secondsize=1;
public Spl() {
super();
super.setBackground(Color.red);
super.setLayout(null);
super.addComponentListener(this);
super.addMouseListener(this);
super.addMouseMotionListener(this);
super.addKeyListener(this);
}
public int getOrientation(){
return orientation;
}
public Spl setOrientation(int i01){
orientation=Math.abs(i01)%2;
resetLayout();
return this;
}
public Spl setDividerLocation(double middle){
if(middle<0.0){middle=0.0;}
else if(middle>0.99){middle=0.99;}
divloc=middle;
resetLayout();
return this;
}
public Spl setDividerSize(int uptocurrsize){
if(orientation==0){
uptocurrsize=Math.min(getHeight(), uptocurrsize);
}
else{
uptocurrsize=Math.min(getWidth(), uptocurrsize);
}
if(uptocurrsize<2){
uptocurrsize=2;
}
divsizehalf=uptocurrsize/2;
resetLayout();
return this;
}
public void switchOrientation(){
setOrientation(getOrientation()+1);
}
public void switchComponents(){
Component cs[]=getComponents();
if(cs.length>1){
removeAll();
add(cs[1]);
add(cs[0]);
int i=2;
while(i<cs.length){
add(cs[i]);
++i;
}
}
resetLayout();
}
protected void resetLayout(){
Component cs[]=getComponents();
if(cs.length>0){
int w=getWidth();
int h=getHeight();
if(cs.length==1){
cs[0].setBounds(0,0,w,h);
}
else {
if(orientation==0){
firstsize=(int)(divloc*h)-divsizehalf-1;
secondsize=h-firstsize-divsizehalf-divsizehalf-1;
cs[0].setBounds(0, 0,w, firstsize);
cs[1].setBounds(0, firstsize+divsizehalf+divsizehalf,w, secondsize);
}
else{
firstsize=(int)(divloc*w)-divsizehalf-1;
secondsize=w-firstsize-divsizehalf-divsizehalf-1;
cs[0].setBounds(0, 0,firstsize, h);
cs[1].setBounds(firstsize+divsizehalf+divsizehalf,0,secondsize,h);
}
//System.out.println(w+" , "+h);
//System.out.println(divloc+" +2*"+divsizehalf);
//System.out.println(firstsize+" - "+secondsize);
//System.out.println(""+cs[0].getBounds());
//System.out.println(""+cs[1].getBounds());
//System.out.println("");
}
}
validate();
repaint();
}
@Override
public void componentResized(ComponentEvent e) {
resetLayout();
}
@Override
public void componentMoved(ComponentEvent e) {
}
@Override
public void componentShown(ComponentEvent e) {
}
@Override
public void componentHidden(ComponentEvent e) {
}
@Override
public void mouseClicked(MouseEvent e) {
}
@Override
public void mousePressed(MouseEvent e) {
requestFocus();
}
@Override
public void mouseReleased(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent e) {
}
@Override
public void mouseExited(MouseEvent e) {
}
@Override
public void mouseDragged(MouseEvent e) {
if(orientation==0){
int h=getHeight();
double dif=e.getY();
dif/=h;
setDividerLocation(dif);
}
else{
int w=getWidth();
double dif=e.getX();
dif/=w;
setDividerLocation(dif);
}
}
@Override
public void mouseMoved(MouseEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
@Override
public void keyPressed(KeyEvent e) {
int kc=e.getKeyCode();
if(kc==KeyEvent.VK_F2){
VK_F2();
}
else if(kc==KeyEvent.VK_F3){
VK_F3();
}
else if(kc==KeyEvent.VK_F4){
VK_F4();
}
}
@Override
public void keyReleased(KeyEvent e) {
}
public void VK_F2(){
switchOrientation();
}
public void VK_F3(){
setDividerLocation(0.5);
}
public void VK_F4(){
switchComponents();
}
}
Δεν υπάρχουν σχόλια:
Δημοσίευση σχολίου