레이블이 APPLET인 게시물을 표시합니다. 모든 게시물 표시
레이블이 APPLET인 게시물을 표시합니다. 모든 게시물 표시

2009년 6월 29일 월요일

3D 간단 구현

http://namsuck.cafe24.com/projects/3d/g3d.html

3D구현을 위해 한동안 만지던 소스이다..
버그도 있고 하지만...
나중에 개발자료로 쓰기 위해서 올려 두었다.. 실행시 클릭후... 방향키로 조정 할 수 있다

- 양 남 석 -
 

최단거리(미완성)

http://namsuck.cafe24.com/projects/theLeastDistance/theLeastDistance.html

Djikstra's algorithm (named after its discover, E.W. Dijkstra) solves the problem of finding the shortest path from a point in a graph (the source) to a destination. It turns out that one can find the shortest paths from a given source to all points in a graph in the same time, hence this problem is sometimes called the single-source shortest paths problem.


Djikstra's algorithm을 이용해 구현해 놓은 소스이다..
아직 UI나 예외처리가 부족하다             
-양 남 석-
 

slotmachine

http://namsuck.cafe24.com/projects/slotmachine/SlotMachineApplet.html

기본 알고리즘 까지만 구현해 놓은 소스이다..
            
-양 남 석-

장기 (미완성)

http://namsuck.cafe24.com/projects/janggi/janggi.html

기본 알고리즘 까지만 구현해 놓은 소스이다..
아직 승부 처리 및 여러가지가 필요하다.

이동경로 구현.

-양 남 석-

Othello (미완성)

http://namsuck.cafe24.com/projects/othello/othello.html

기본 알고리즘 까지만 구현해 놓은 소스이다..
아직 승부 처리 및 여러가지가 필요하다.
            
-양 남 석-


----othello.html------------------------------------------------------------------
<html>

<body>

<br>
<br>
<center>
<!--"CONVERTED_APPLET"-->
<!-- HTML CONVERTER -->
<object
    classid = "clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
    codebase = "http://java.sun.com/update/1.6.0/jinstall-6u13-windows-i586.cab#Version=6,0,0,3"
    WIDTH="700" HEIGHT = "500" >
    <PARAM NAME = CODE VALUE = "Othello.class" >
    <PARAM NAME = "codebase" value = "projects/othello/" >
    <param name = "type" value = "application/x-java-applet;version=1.6">
    <param name = "scriptable" value = "false">

    <comment>
<embed
            type = "application/x-java-applet;version=1.6" \
            CODE = "Othello.class" \
            codebase = "projects/othello/" \
            WIDTH="700" HEIGHT = "500"
   scriptable = false
   pluginspage = "http://java.sun.com/products/plugin/index.html#download">
   <noembed>
            
            </noembed>
</embed>
    </comment>
</object>

</center>
</body>

</html>

----Othello.java------------------------------------------------------------------
import java.awt.*;
import java.applet.*;
import java.awt.Image;
import java.awt.event.*;


public class Othello extends Applet implements MouseListener
{

private static final long serialVersionUID = 9721014L;  

private Image           imgOffScr;                      
private Graphics        grpOffScr;  

int sizeH=40;
int sizeW=40;
int partition=8;

int mouseX=0;
int mouseY=0;
int number=1;
int data[][];

    public void init()
    {
        this.setBackground(Color.white);

data=new int[partition][partition];
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
data[i][j]=0;

addMouseListener(this);
    }
    
    public void start()
{
imgOffScr = createImage(getWidth(),getHeight());
        grpOffScr = imgOffScr.getGraphics();
}
public void update(Graphics g)
{
        paint(g);
}

public void paint(Graphics g)
    {
   grpOffScr.setColor(Color.white);            
        grpOffScr.fillRect(0, 0, getWidth(),getHeight());  

///////////////판에 무늬넣기/////////////////////////////////////////////////
for (int i=0;i<partition;i++)
{
for (int j=0;j<partition;j++)
{
if((i+j)%2==0)grpOffScr.setColor(new Color(200,200,200));
else grpOffScr.setColor(new Color(220,220,220));
grpOffScr.fillRect(i*sizeH,j*sizeW,sizeH,sizeW);
}
}

///////////////판에 줄그리기/////////////////////////////////////////////////
grpOffScr.setColor(Color.black);
for (int i=0;i<partition+1;i++ )              
{
grpOffScr.drawLine(0,i*sizeH, sizeH*partition,i*sizeW);    //세로줄
grpOffScr.drawLine(i*sizeW,0, i*sizeW,sizeH*partition);   //가로줄
}
///////////////바둑알그리기/////////////////////////////////////////////////
for(int i=0;i<partition;i++)                            
for(int j=0;j<partition;j++)
if(data[i][j]==1 || data[i][j]==2)
{
if(data[i][j]==1)grpOffScr.setColor(new Color(100,100,200));
else if(data[i][j]==2)grpOffScr.setColor(new Color(200,100,100));
grpOffScr.fillOval(i*sizeH+3,j*sizeW+3,sizeH-6,sizeW-6);  //속이 찬 원그리기
grpOffScr.setColor(Color.black);                                    
grpOffScr.drawOval(i*sizeH+3,j*sizeW+3,sizeH-6,sizeW-6);  //원 윤각테두리
}

///////////////상황창보이기/////////////////////////////////////////////////

if(number==1)grpOffScr.setColor(new Color(80,80,200));
else if(number==2)grpOffScr.setColor(new Color(200,80,80));
grpOffScr.fillOval(partition*sizeH+100,0,sizeH-6,sizeW-6);


grpOffScr.setColor(new Color(200,200,200));
grpOffScr.fillRect(partition*sizeH+20,10,60,30);
grpOffScr.setColor(Color.black);
grpOffScr.drawRect(partition*sizeH+20,10,60,30);
grpOffScr.drawString("다시",partition*sizeH+70,10);

grpOffScr.setColor(Color.black);
grpOffScr.drawString("박스크기X:"+Integer.toString(getHeight()),350,50);
grpOffScr.drawString("박스크기Y:"+Integer.toString(getWidth()),350,80);
grpOffScr.drawString("칸X:"+Integer.toString(mouseX),350,110);
grpOffScr.drawString("칸Y:"+Integer.toString(mouseY),350,140);
grpOffScr.drawString("파란돌수:"+Integer.toString(bluecount()),350,170);
grpOffScr.drawString("빨간돌수:"+Integer.toString(redcount()),350,200);
grpOffScr.drawString(win(),350,230);
///////////////버퍼에 있는 이미지 출력///////////////////////////////////////
g.drawImage(imgOffScr, 0, 0, this);    
    }



public void mousePressed(MouseEvent e)
{
if(e.getX()<=sizeW*partition && e.getY()<=sizeH*partition )
{
mouseX=(e.getX()/sizeH);
mouseY=(e.getY()/sizeW);
algorithm();
}
}
public void mouseReleased(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}

/*
    public void run()
    {
    }

    
    public void stop()
    {
    }

    public void destroy()
    {
    }
*/

public void change_Data(String d,int select)   //d는 좌표값이 들어있는 항상 XY짝수이다.
{
for(int i=0;i<d.length()/2;i++)  //좌표의 갯수
{
  data[Integer.parseInt(d.substring(i*2,i*2+1))][Integer.parseInt(d.substring(i*2+1,i*2+2))]=select;  //좌표를 저장
}
}


public int bluecount()
{
int count=0;
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
if(data[i][j]==1)count++;
return count;
}

public int redcount()
{
int count=0;
for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
if(data[i][j]==2)count++;
return count;
}

public String win()
{
int countb=0;
int countr=0;

for(int i=0;i<partition;i++)
for(int j=0;j<partition;j++)
{
if(data[i][j]==1)countb++;
if(data[i][j]==2)countr++;
}
if(countb>countr)return "파란색이 많음";
else if(countb<countr)return "빨간색이 많음";
else return "파란색과 빨간색 갯수 같음";
}


public void algorithm()
{
if(data[mouseX][mouseY]==0)
{
//선택 3시쪽 돌 변경
String temp="";
for(int i=mouseX+1;i<partition && data[i][mouseY]!=0;i++)    
if(data[i][mouseY]!=number)temp+=Integer.toString(i)+Integer.toString(mouseY);
else{change_Data(temp,number);break;}
//선택 9시쪽 돌 변경
temp="";
for(int i=mouseX-1;i>=0 && data[i][mouseY]!=0;i--)    
if(data[i][mouseY]!=number)temp+=Integer.toString(i)+Integer.toString(mouseY);
else {change_Data(temp,number);break;}
//선택 12시쪽 돌 변경
temp="";
for(int i=mouseY-1;i>=0 && data[mouseX][i]!=0;i--)    
if(data[mouseX][i]!=number)temp+=Integer.toString(mouseX)+Integer.toString(i);
else {change_Data(temp,number);break;}
//선택 6시쪽 돌 변경
temp="";
for(int i=mouseY+1;i<partition && data[mouseX][i]!=0;i++)    
if(data[mouseX][i]!=number)temp+=Integer.toString(mouseX)+Integer.toString(i);
else {change_Data(temp,number);break;}
//선택 1시반쪽 돌 변경
temp="";
for(int i=mouseX+1,j=mouseY-1;i<partition && j>=0  && data[i][j]!=0;i++,j--)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 7시반쪽 돌 변경
temp="";
for(int i=mouseX-1,j=mouseY+1;i>=0 && j<partition  && data[i][j]!=0;i--,j++)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 4시반쪽 돌 변경
temp="";
for(int i=mouseX+1,j=mouseY+1;i<partition && j<partition  && data[i][j]!=0;i++,j++)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}
//선택 11시반쪽 돌 변경
temp="";
for(int i=mouseX-1,j=mouseY-1;i>=0 && j>=0  && data[i][j]!=0;i--,j--)    
if(data[i][j]!=number)temp+=Integer.toString(i)+Integer.toString(j);
else {change_Data(temp,number);break;}



// 선택한 돌 놓고... 차례 바꾸기
if(number==1)
{
data[mouseX][mouseY]=1;
number=2;
}
else  if(number==2)
{
data[mouseX][mouseY]=2;
number=1;
}

// 화면 다시 그리기
repaint();
}
}

}

2009년 6월 28일 일요일

applet 과 servlet 통신하기

import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;

/**
 * Simple demonstration for an Applet <-> Servlet communication.
 */
public class EchoApplet extends Applet {
 private TextField inputField = new TextField();
 private TextField outputField = new TextField();
 private TextArea exceptionArea = new TextArea();

 /**
  * Setup the GUI.
  */
 public void init() {
  // set new layout
  setLayout(new GridBagLayout());

  // add title  
  Label title = new Label("Echo Applet", Label.CENTER);
  title.setFont(new Font("SansSerif", Font.BOLD, 14));
  GridBagConstraints c = new GridBagConstraints();
  c.gridwidth = GridBagConstraints.REMAINDER;
  c.weightx = 1.0;
  c.fill = GridBagConstraints.HORIZONTAL;
  c.insets = new Insets(5, 5, 5, 5);
  add(title, c);

  // add input label, field and send button
  c = new GridBagConstraints();
  c.anchor = GridBagConstraints.EAST;
  add(new Label("Input:", Label.RIGHT), c);
  c = new GridBagConstraints();
  c.fill = GridBagConstraints.HORIZONTAL;
  c.weightx = 1.0;
  add(inputField, c);
  Button sendButton = new Button("Send");
  c = new GridBagConstraints();
  c.gridwidth = GridBagConstraints.REMAINDER;
  add(sendButton, c);
  sendButton.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    onSendData();
   }
  });

  // add output label and non-editable field
  c = new GridBagConstraints();
  c.anchor = GridBagConstraints.EAST;
  add(new Label("Output:", Label.RIGHT), c);
  c = new GridBagConstraints();
  c.gridwidth = GridBagConstraints.REMAINDER;
  c.fill = GridBagConstraints.HORIZONTAL;
  c.weightx = 1.0;
  add(outputField, c);
  outputField.setEditable(false);

  // add exception label and non-editable textarea
  c = new GridBagConstraints();
  c.anchor = GridBagConstraints.EAST;
  add(new Label("Exception:", Label.RIGHT), c);
  c = new GridBagConstraints();
  c.gridwidth = GridBagConstraints.REMAINDER;
  c.weighty = 1;
  c.fill = GridBagConstraints.BOTH;
  add(exceptionArea, c);
  exceptionArea.setEditable(false);
 }

 /**
  * Get a connection to the servlet.
  */
 private URLConnection getServletConnection()
  throws MalformedURLException, IOException {

  // Connection zum Servlet ?fnen
  URL urlServlet = new URL(getCodeBase(), "echo");
  URLConnection con = urlServlet.openConnection();

  // konfigurieren
  con.setDoInput(true);
  con.setDoOutput(true);
  con.setUseCaches(false);
  con.setRequestProperty(
   "Content-Type",
   "application/x-java-serialized-object");

  // und zur?kliefern
  return con;
 }

 /**
  * Send the inputField data to the servlet and show the result in the outputField.
  */
 private void onSendData() {
  try {
   // get input data for sending
   String input = inputField.getText();

   // send data to the servlet
   URLConnection con = getServletConnection();
   OutputStream outstream = con.getOutputStream();
   ObjectOutputStream oos = new ObjectOutputStream(outstream);
   oos.writeObject(input);
   oos.flush();
   oos.close();

   // receive result from servlet
   InputStream instr = con.getInputStream();
   ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
   String result = (String) inputFromServlet.readObject();
   inputFromServlet.close();
   instr.close();

   // show result
   outputField.setText(result);

  } catch (Exception ex) {
   ex.printStackTrace();
   exceptionArea.setText(ex.toString());
  }
 }
}

 

import java.io.*;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Simple demonstration for an Applet <-> Servlet communication.
 */
public class EchoServlet extends HttpServlet {
 /**
  * Get a String-object from the applet and send it back.
  */
 public void doPost(
  HttpServletRequest request,
  HttpServletResponse response)
  throws ServletException, IOException {
  try {
   response.setContentType("application/x-java-serialized-object");

   // read a String-object from applet
   // instead of a String-object, you can transmit any object, which
   // is known to the servlet and to the applet
   InputStream in = request.getInputStream();
   ObjectInputStream inputFromApplet = new ObjectInputStream(in);
   String echo = (String) inputFromApplet.readObject();

   // echo it to the applet
   OutputStream outstr = response.getOutputStream();
   ObjectOutputStream oos = new ObjectOutputStream(outstr);
   oos.writeObject(echo);
   oos.flush();
   oos.close();

  } catch (Exception e) {
   e.printStackTrace();
  }
 }

}

 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<title> Plugin example </title>
<body bgcolor="white">
<jsp:plugin type="applet" code="appletsrc.AppletToServer2.class"
codebase="appletArchive"
jreversion="1.5"
width="100%"
height="100%"
archive="jfreechart-1.0.9.jar,jcommon-1.0.12.jar,jwork.jar"
>
<jsp:params>
 <jsp:param name="paramval" value="Hello!! jsp:param" />
 <jsp:param name="servlet" value="http://203.245.121.43:8888/gsgroup/jwork/groupTest2.do" />
 <jsp:param name="title" value="나의 차트 테스트" />
 <jsp:param name="domainAxisLabel" value="LABEL 1" />
 <jsp:param name="rangeAxisLabel" value="LABEL 2" />
 <jsp:param name="orientation" value="H" />
 
</jsp:params>
</jsp:plugin>
</body>
</html>