자바

 

 

 

 

 

class BasicPaint

package java11;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JPanel;

//drawRect(x, y, width, height) 사각형 그리기 (테두리)
//fillRect(x, y, width, height) 사각형 그리기(색상 처리)
//drawLine ( x1, y1, x2, y2 ) 직선 그리기
//drawString(문자열, x, y ) 문자열 출력
// drawImage( 이미지, x, y , 이미지관찰자객체 ) 이미지 출력


public class BasicPaint extends JFrame{

	//내부 클래스
	class MyPanel extends JPanel implements MouseListener{
		int squareX =50;
		int squareY =50;
		int squareW =20;
		int squareH =20;
		
		public MyPanel() {
			//테주리 설정
			setBorder(BorderFactory.createLineBorder(Color.BLACK));
			
			//마우스 이벤트 처리
			addMouseListener(this);
		}
		
		void moveSquare(int x, int y){
			//그래픽 갱신 요청
			repaint(); //=> update() => paint() =>paintComponent()
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			super.paintComponent(g);
			//문자열 출력
			g.drawString("마우스를 클릭하세요", 10, 20);
			g.setColor(Color.red);
			//사각형 그리기(색상 처리)
			g.fillRect(squareX, squareY, squareW, squareH);
			g.setColor(Color.BLACK);
			//사각형 그리기 (테두리만 그리기)
			g.drawRect(squareX*2, squareY*2, squareW, squareH);
			
			///RoundRect 그리기
			g.setColor(Color.blue);
			//x,y ,width,height, 가로라운드길이, 세로 라운드 길이
			g.fillRoundRect(120, 240, 90, 55, 40, 40);
			//원그리기
			g.setColor(Color.RED);
			// x, y, width, height
			g.fillOval(220, 40, 90, 55);
			
			//다각형 그리기
			int[] x = {10, 30, 50 , 40, 20 }; // x좌표 배열
			int[] y ={257, 240, 257, 280, 280 }; //y 좌표 배열
			g.setColor(Color.yellow);
			g.fillPolygon(x, y, x.length); //x배열,y배열, 꼭지점수
			
		}

		@Override
		public void mouseClicked(MouseEvent e) {
			//클릭한 x, y 좌표값 을 저장
			squareX=e.getX();
			squareY=e.getY();
			
			//화면갱신 요청
			repaint();
		}

		@Override
		public void mousePressed(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseEntered(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void mouseExited(MouseEvent e) {
			// TODO Auto-generated method stub
			
		}

		
		
		
		
	}
	
	//생성자
	public BasicPaint() {
		add(new MyPanel());
		setSize(500, 500);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	
	public static void main(String[] args) {
		new BasicPaint();
	}
	
	
}




 

 

 

 


 

class RandomLine

 

package java11;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;

//선 그리기
// drawLine(x1, y2, x2, y2)
public class RandomLine extends JFrame{

	class MyPanel extends JPanel{
		
		
		
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			super.paintComponent(g);
			System.out.println("paintComponect 호출......");
			
			Random rand =new Random();//랜덤 객체
			for(int i=0; i<100; i++){
				int red =rand.nextInt(256);//0~255 사이의 난수 발생
				int green=rand.nextInt(256);
				int blue=rand.nextInt(256);
				g.setColor(new Color(red, green, blue));
				//new Color(R,G, B) RGB color 설정
				g.setColor(new Color(red, green, blue));
				int x1=rand.nextInt(300);//0~ 299 사이의 난수 발생
				int x2=rand.nextInt(300);
				int y1=rand.nextInt(300);
				int y2=rand.nextInt(300);
				g.drawLine(x1, y1, x2, y2);//선 그리기
			}
			
		}
	}
	
	public RandomLine(){
		add(new MyPanel());
		setSize(500, 500);
		setVisible(true);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new RandomLine();
	}
}

 

 

 

 

 

class ColorTest

 

 

 

package java11;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;

//new Color(색상상수)
// Color.red, Color.blue ...
//new Color (red, green , blue)
public class ColorTest extends JFrame{

	class MyPanel extends JPanel{
		JButton button1, button2;
		Color color =new Color(0,0,0); // black 색상
		public MyPanel(){
			//패널의 기본 레이아웃을 BorderLayout 으로 설정
			setLayout(new BorderLayout());
			button1 =new JButton("색상 변경");
			button2 =new JButton("색상 선택 변경");
			add(button1, "North");//버튼을 North 영역에 배치
			add(button2, "South");
			button1.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					// TODO Auto-generated method stub
					
					Random rnd =new Random();
					int r =rnd.nextInt(256);
					int g=rnd.nextInt(256);
					int b=rnd.nextInt(256);
					color=new Color(r, g, b);
					//화면 갱신 요청  ->pintComponent() 호출
					repaint();
				}
			});
			
			button2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
				
					color=JColorChooser.showDialog(MyPanel.this, "색상을 선책 하세요.", Color.BLUE);
					repaint();
					
				}
			});
			
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			super.paintComponent(g);
			g.setColor(color); //색상 설정
			//색칠 사각형(x, y, width, height)
			g.fillRect(10, 10, 210, 210);
		}
		
	}
	
	public ColorTest(){
		add(new MyPanel());
		setSize(250, 250);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new ColorTest();
	}
	
}

 

 

 

 

class FontTest 

 

 

package java11;

import java.awt.Font;
import java.awt.Graphics;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class FontTest extends JFrame{
	private static final long serialVersionUID = 4324231L;

	class MyPanel extends JPanel{

		private static final long serialVersionUID = 1L;
		Font f1, f2,f3, f4, f5,f6;
		
		
		public MyPanel() {
			//new Font("폰트이름" , 옵션, 사이즈)
			f1=new Font("Serif", Font.PLAIN, 20);
			f2=new Font("San Serif", Font.BOLD, 20); //진하게
			f3=new Font("Monspaced", Font.ITALIC, 20);//이탤릭
			//진하게 + 이탤릭
			f4=new Font("Dialog", Font.BOLD | Font.ITALIC, 20);
			f5=new Font("DialogInput", Font.BOLD, 20);
			f6=new Font("휴먼옛체", Font.BOLD, 20);
			
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			super.paintComponent(g);
			g.setFont(f1);//폰트 설정
			//drawString("문자열", x, y) 문자열 출력
			g.drawString("Serif 20", 10, 50);
			
			g.setFont(f2);
			g.drawString("Sanserif 20", 10, 100);
			
			g.setFont(f3);
			g.drawString("Monospaced 20", 10, 150);
			
			g.setFont(f4);
			g.drawString("Dialog 20", 10, 200);
			
			g.setFont(f5);
			g.drawString("DialogInput 20", 10,250);
		
			g.setFont(f6);
			g.drawString("휴먼옛체 20", 10, 300);
		
			
		}
	}
	
	public FontTest() {
		add(new MyPanel());
		setSize(500, 500);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	
	
	public static void main(String[] args) {
		new FontTest();
	}
	
}

 

 

 

 

 

 

 

 class MyFrame

 

package java11;

import java.applet.AudioClip;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.File;
import java.net.URL;

import javax.imageio.ImageIO;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;
import javax.swing.Timer;

//이미지 애니메이션  + 사운드 처리
public class MyFrame extends JFrame {

	class MyPanel extends JPanel implements ActionListener{
		JButton button, button2;
		final int WIDTH =500;//패널의 가로길이
		final int HEIGHT=300;//패널의 세로길이
		final int START_X =0;//이미지의 x좌표
		final int START_Y=250;// 이미지의 y좌표
		BufferedImage image ; //이미지 객체
		Timer timer; //javax.swing.Timer, 타이머 객체
		int x, y;
	
		boolean  sw=true;
		 Clip clip;
		public MyPanel() {
			button=new JButton("Fire");
			button2=new JButton("사운드");
			add(button);//버튼을 패널에 붙임
			add(button2);
			
			setBackground(Color.BLACK); //패널의 배경색
			//패널의 사이즈 설정
			setPreferredSize(new Dimension(WIDTH, HEIGHT));
			File input =new File("a1.jpg"); //이미지 파일 객체
			try{
				image =ImageIO.read(input);//이미지 로딩
				//현재 클래스가 실행중인 경로
				//image =ImageIO.read(getClass().getResource("ship.jpg"));
				
			}catch (Exception e) {
				e.printStackTrace();
			}
			//이미지의 초기좌표
			x=START_X;
			y=START_Y;
			
			button.addActionListener(new ActionListener() {			
				@Override
				public void actionPerformed(ActionEvent e) {
					//타이머 생성
					timer=new Timer(20, MyPanel.this);
					//타이머 시작 요청 => actionPerformed() 가 호출됨
					timer.start();
				}
			});
			
		
			button2.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {
					if(sw ==false)  clip.close();
					
					if(sw){
						try{
							
							
							
							 URL url = new URL(
				                     "http://cfs3.tistory.com/upload_control/download.blog?fhandle=YmxvZzk3OTYyQGZzMy50aXN0b3J5LmNvbTovYXR0YWNoLzAvMDgwMDAwMDAwMDE1LldBVg%3D%3D");
							   clip = AudioSystem.getClip();
							   // getAudioInputStream() also accepts a File or InputStream
							   AudioInputStream ais = AudioSystem.getAudioInputStream( url );
							   clip.open(ais);
							  
							   clip.loop(Clip.LOOP_CONTINUOUSLY);
							   /*SwingUtilities.invokeLater(
									   new Runnable() {
										   
										   
											@Override
											public void run() {
												// TODO Auto-generated method stub
											
												
												JOptionPane.showMessageDialog(null, "Close to exit!");
											}
										   
									   }
								 );
							*/
							   
							   
						}catch (Exception e2) {
								e2.printStackTrace();
						}
						
												
					}
					sw = !sw;
					
				

					
				}
			});
			
			
		}
		
		@Override
		protected void paintComponent(Graphics g) {
			// TODO Auto-generated method stub
			super.paintComponent(g);
			
			g.drawImage(image, x,y, 50,50 ,null);
			
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			System.out.println("action....");
			//이미지의 좌표값 변경
			x++;
			y--;
			if(x > WIDTH){ //범위를 벗어나면 원위치로 이동
				x =START_X;
				y =START_Y;
			}
			
			repaint();//화면 갱신 요청
//repaint() => update() => paint() => paintBorder()
// => paintComponent() => paintChild()
		}
		
	}
	
	public MyFrame() {
		add(new MyPanel());//프레임에 패널을 붙임
		pack();//자동 사이즈
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void main(String[] args) {
		new MyFrame();
	}
}

 

 

 

 

 

 

java

 

about author

PHRASE

Level 60  머나먼나라

제대로 쓰지도 않는 재산을 가지고 있는 것은 결국 한푼도 가지고 있지 않은 것과 다를 바 없다. -이솝

댓글 ( 4)

댓글 남기기

작성