'Example'에 해당되는 글 6건

  1. 2023.02.21 python에서 adb logcat 실행 후 결과 가져오기
  2. 2022.02.21 c++ getline 예제 코드
  3. 2022.01.06 Swing CustomLineBorder 만들기(top, left, bottom, right 지정하여 적용하기)
  4. 2021.12.27 FlowLayout 에서 component 가 다음 라인으로 이동시 panel resize
  5. 2021.12.13 Swing 단축키(KeyStroke) 등록하기
  6. 2021.12.03 Android System Font list 가져오기, 적용하기

python에서 adb logcat 실행 후 결과 가져오기

프로그래밍/python 2023. 2. 21. 22:25
반응형
#!/usr/bin/python

import subprocess

process = subprocess.Popen(['adb', 'logcat'], stdout=subprocess.PIPE)
while True:
    line = process.stdout.readline()
    line = line.decode('utf-8', errors='ignore')
    line = line.strip()
    print(line)
 

 

반응형
:

c++ getline 예제 코드

프로그래밍/c,c++ 2022. 2. 21. 14:01
반응형

코드

#include <iostream>

using namespace std;
int main() {
    string input = "";

    while (input != "quit") {
        getline(cin, input);
        cout << "read string = " << input << endl;
    }

    return 0;
}

 

실행

$ ./a.out

read string = 
aa
read string = aa
aa bb
read string = aa bb
quit
read string = quit
반응형
:

Swing CustomLineBorder 만들기(top, left, bottom, right 지정하여 적용하기)

프로그래밍/java 2022. 1. 6. 22:58
반응형

생성시 지정된 면만 border 적용 하도록 border 재정의 함

package test.swing;

import javax.swing.*;
import javax.swing.border.AbstractBorder;
import java.awt.*;

// Custom Line border
public class MainTest {
    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.setVisible(true);
    }
}

class CustomLineBorder extends AbstractBorder
{
    public static final int TOP = 0x1;
    public static final int LEFT = 0x2;
    public static final int BOTTOM = 0x4;
    public static final int RIGHT = 0x8;
    private Color mColor;
    private int mThickness;
    private int mTarget;

    public CustomLineBorder(Color colour, int thickness, int target)
    {
        mColor = colour;
        mThickness = thickness;
        mTarget = target;
    }

    @Override
    public void paintBorder(Component c, Graphics g, int x, int y, int width, int height)
    {
        if (g != null)
        {
            g.setColor(mColor);
            if ((mTarget & TOP) != 0) {
                for (int i = 0; i < mThickness; i++) {
                    g.drawLine(x, y + i, width, y + i);
                }
            }
            if ((mTarget & LEFT) != 0) {
                for (int i = 0; i < mThickness; i++) {
                    g.drawLine(x + i, y, x + i, height);
                }
            }
            if ((mTarget & BOTTOM) != 0) {
                for (int i = 0; i < mThickness; i++) {
                    g.drawLine(x, height - i - 1, width, height - i - 1);
                }
            }
            if ((mTarget & RIGHT) != 0) {
                for (int i = 0; i < mThickness; i++) {
                    g.drawLine(width - i - 1, y, width - i - 1, height);
                }
            }
        }
    }

    @Override
    public Insets getBorderInsets(Component c)
    {
        return (getBorderInsets(c, new Insets(0, 0, 0, 0)));
    }

    @Override
    public Insets getBorderInsets(Component c, Insets insets)
    {
        insets.top = 0;
        insets.left = 0;
        insets.bottom = 0;
        insets.right = 0;
        if ((mTarget & TOP) != 0) {
            insets.top = mThickness;
        }
        if ((mTarget & LEFT) != 0) {
            insets.left = mThickness;
        }
        if ((mTarget & BOTTOM) != 0) {
            insets.bottom = mThickness;
        }
        if ((mTarget & RIGHT) != 0) {
            insets.right = mThickness;
        }

        return insets;
    }

    @Override
    public boolean isBorderOpaque()
    {
        return true;
    }
}

class MainUI extends JFrame {
    MainUI() {
        setPreferredSize(new Dimension(400, 420));
        
        JPanel pane = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 20));
        pane.setBackground(new Color(0x85, 0x85, 0x85));

        JLabel label = new JLabel("Red, 1, TOP");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.RED, 1, CustomLineBorder.TOP));
        pane.add(label);

        label = new JLabel("Red, 5, LEFT");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.RED, 5, CustomLineBorder.LEFT));
        pane.add(label);

        label = new JLabel("Red, 5, Bottom");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.RED, 5, CustomLineBorder.BOTTOM));
        pane.add(label);

        label = new JLabel("Red, 15, Right");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.RED, 15, CustomLineBorder.RIGHT));
        pane.add(label);

        label = new JLabel("Blue, 5, TOP LEFT");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.BLUE, 5, CustomLineBorder.LEFT | CustomLineBorder.TOP));
        pane.add(label);

        label = new JLabel("Blue, 5, TOP RIGHT BOTTOM");
        label.setPreferredSize(new Dimension(300, 40));
        label.setOpaque(true);
        label.setBackground(Color.WHITE);
        label.setBorder(new CustomLineBorder(Color.BLUE, 5, CustomLineBorder.TOP | CustomLineBorder.RIGHT | CustomLineBorder.BOTTOM));
        pane.add(label);

        add(pane);

        pack();
    }
}

실행결과

 

 

반응형
:

FlowLayout 에서 component 가 다음 라인으로 이동시 panel resize

프로그래밍/java 2021. 12. 27. 22:31
반응형

Resize 이벤트 발생시 JPanel의 높이를 마지막 component 에 맞추는 코드

package test.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

// resize panel
public class MainTest {
    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.setVisible(true);
    }
}

class MainUI extends JFrame {
    FlowLayout flowLayout = new FlowLayout(FlowLayout.LEFT, 0, 0);
    JPanel resizePane = new JPanel(flowLayout);
    JButton testBtn1 = new JButton("TEST 1");
    JButton testBtn2 = new JButton("TEST 2");
    JButton testBtn3 = new JButton("TEST 3");
    JButton testBtn4 = new JButton("TEST 4");
    JButton testBtn5 = new JButton("TEST 5");
    JComponent lastComponent = testBtn5;

    MainUI() {
        setPreferredSize(new Dimension(500, 300));
        setLayout(new BorderLayout());

        resizePane.setBackground(Color.BLUE);
        resizePane.addComponentListener(new ComponentAdapter() {
            Point prevPoint = null;

            @Override
            public void componentResized(ComponentEvent e) {
                super.componentResized(e);
                if (prevPoint == null || prevPoint.y != lastComponent.getLocation().y) {
                    System.out.println("lastComonent moved to " + lastComponent.getLocation());
                    resizePane.setPreferredSize(new Dimension(resizePane.getPreferredSize().width, lastComponent.getLocation().y + lastComponent.getHeight()));
                    resizePane.updateUI();
                }
                prevPoint = lastComponent.getLocation();
            }
        });

        resizePane.add(testBtn1);
        resizePane.add(testBtn2);
        resizePane.add(testBtn3);
        resizePane.add(testBtn4);
        resizePane.add(testBtn5);
        add(resizePane, BorderLayout.NORTH);

        JPanel pane = new JPanel();
        pane.add(new JLabel("TEST TEST"));
        add(pane, BorderLayout.CENTER);

        pack();
    }
}

최초 실행

JPanel 크기 변경 1

JPanel 크기 변경 2

반응형
:

Swing 단축키(KeyStroke) 등록하기

프로그래밍/java 2021. 12. 13. 21:00
반응형

ESC 입력시 창 닫기

package test.swing;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.awt.event.WindowEvent;

public class MainTest {
    public static void main(String[] args) {
        MainUI mainUI = new MainUI();
        mainUI.setVisible(true);
    }
}

class MainUI extends JFrame {
    MainUI() {
        setPreferredSize(new Dimension(400, 300));
        setLayout(new FlowLayout());
        JButton button = new JButton("TEST");
        button.setPreferredSize(new Dimension(100, 100));
        add(button);
        pack();

        KeyStroke escStroke = KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0);
        String actionMapKey = getClass().getName() + ":WINDOW_CLOSING";
        Action closingAction = new AbstractAction() {
            public void actionPerformed(ActionEvent event) {
                MainUI.this.dispatchEvent(new WindowEvent(MainUI.this, WindowEvent.WINDOW_CLOSING));
            }
        };

        installKeyStroke(this, escStroke, actionMapKey, closingAction);
    }

    public void installKeyStroke(final RootPaneContainer container, final KeyStroke stroke, final String actionMapKey, final Action action) {
        JRootPane rootPane = container.getRootPane();
        rootPane.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(stroke, actionMapKey);
        rootPane.getActionMap().put(actionMapKey, action);
    }
}

 

반응형
:

Android System Font list 가져오기, 적용하기

OS/Android 2021. 12. 3. 12:36
반응형

system/fonts 디렉토리에 있는 폰트 파일 리스트 출력

 

폰트 파일 적용

 

테스트 코드

import android.graphics.Typeface
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.util.Log.*
import android.widget.TextView
import java.io.File

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    override fun onResume() {
        changeFont()
        super.onResume()
    }

    fun changeFont() {
    	// get font files
        val FONT_DIR = "/system/fonts/"
        val fontPaths = ArrayList<String>()
        val fontDir = File(FONT_DIR)
        val fontSuffix = ".ttf";

        for(font in fontDir.listFiles()) {
            if(font.name.endsWith(fontSuffix)) {
                fontPaths.add(font.absolutePath);
            }
        }

        for (fontFile in fontPaths) {
            Log.i("Font test","Font : $fontFile")
        }

		// apply font file
        val textView1 : TextView = findViewById(R.id.textview1)
        val textView2 : TextView = findViewById(R.id.textview2)

        val typeface1 = Typeface.createFromFile("/system/fonts/NotoSerif-Bold.ttf")
        textView1.typeface = typeface1
        textView1.text = "TestView 123 "

        val typeface2 = Typeface.createFromFile("/system/fonts/Roboto-Regular.ttf")
        textView2.typeface = typeface2
        textView2.text = "TestView 123 "
    }
}

 

반응형
: