2012年12月23日日曜日

Hello GUI (C言語 for Linux) part 1

C言語でGUIアプリケーションを作成してみようと思う。
使うのはX Window System

概要は以下URLをみてください。
http://xjman.dsl.gr.jp/X11R6/X11/

また、コンパイラはGCC

まだ、勉強している最中で、思考をまとめるために書いているので多数ミスがある可能性が高いので指摘していただきたいです。

ソースコードの内容としては、ただ真っ暗な画面で終了するとエラーが出るものです。

#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include<stdio.h>

int main(){
 Display *display; //参照→http://xjman.dsl.gr.jp/X11R6/X11/CH02.html
 char *display_name;//ハードウェアのディスプレイ名
 GC gc;//グラフィックコンテキスト 参照→http://xjman.dsl.gr.jp/X11R6/X11/CH07.html
 Window window;//参照→http://xjman.dsl.gr.jp/X11R6/X11/CH03.html
 XEvent ev;//参照→http://xjman.dsl.gr.jp/X11R6/X11/CH10.html
 int width;
 int height;
 //Display情報
 display_name=NULL;
 display = XOpenDisplay(display_name);//ハードウェアのディスプレイ名,NULLだとDISPLAY環境変数の値が使われる。
 //横と高さの大きさ
 width = DisplayWidth(display,0)/4;//大きさを取ってくる、ディスプレイサーバとディスプレイの番号。
 height = DisplayHeight(display,0)/4;//大きさは1/4
 //画面作成
 //window=XCreateWindow(display,RootWindow(display,0),0,0,width,height,1,CopyFromParent,InputOutput,DefaultVisual(display, DefaultScreen(display)),0,0);
 //めんどくさい時はこっち
 window=XCreateSimpleWindow(display,RootWindow(display,0),0,0,width,height,1,0,0);
 //タイトル
 XStoreName(display,window,"Hello GUI");
 gc=XCreateGC(display,window,0,NULL);//グラフィック操作
 //本来なら使うマスクだけをいれる
 XSelectInput(display,window,ExposureMask|ButtonPressMask|ButtonReleaseMask|KeyPressMask|ButtonMotionMask|KeyReleaseMask);
 //画面の表示
 XMapWindow(display,window);
 //終了しないためのwhile(1)
 while(1){
  XNextEvent(display,&ev);//イベント待ち
 }
  
 return 0;

}


2012年12月16日日曜日

Qosmio AV Centerにてエラー

Qosmioで8000354dや8000363cのエラーが起きたときにやること。

1つ目は電源プランを高パフォーマンスに。

2つ目は、管理者権限でQosmio AV Centerを起動。

3つ目は、最新のQosmio AV Centerをインストールすること。

たぶんこれでいける

TOSHIBAさんへのリンク
http://dynabook.com/assistpc/download/hist/hist_upd_index.htm

2012年12月6日木曜日

Androidのプログラムを作っていて思った事

苦手意識を持っていても、なんとなく慣れればなんとかなると思う。

xmlとかでレイアウトとかなれなかったけどなせば成るよね。

Eclipseで作成していたのですが、Javaのプログラムをコンパイルするノリで、xmlを最前面に表示してコンパイルをしようとしたら、xmlのエラーがでて焦ったことかな。



C言語でcatっぽいもの


久しぶりにC言語やりました。
入門編をかるく触った程度だからもっと勉強しなくてはいけない反面、奥が深すぎてしんどい。。。

今回、作ったのはUNIXとかLinuxにあるcatコマンドっぽいものを作ろうかなと思いました。
Windowsのプロンプトで操作をしていた時に思わずcat fileとやった時にこれを作ろうと。
ただ未完成だし、めんどくさくなって投げてしまった。

なのになぜ公開したかというと、同学年の同じ学科の学生でC言語の授業を受けたのにかかわらず、
フラグやOR演算などの処理を理解してない人がちらほらいたので、そういう人向けにつくる問題の材料にされたらいいなーって
24行目のfile modeはコマンドライン引数が2個以上渡されたときにフラグがたちます。
cat printfile ow writefireみたいな使い方や
cat printfile e , cat printfile みたいにも使えます。
oは上書き、aは追加、cはコピー、wは手動で文字を書き込む、
eはファイルの中身を表示する時に一行ごとにEnterを押さないと表示しないようにする。

このプログラムで作れる問題は、AND演算を理解しているか、16進数から2進数に変換して考えることができるのか、
if文の基本的な使い方、コマンドライン引数を覚えているかなどを問うことができるので、初級向きだと思います。
また、とてもみにくいソースなので、コメント文を消して穴埋めをさせたりする、基本情報の午後試験の練習にもなるかもしれませんね。


#include 
#include 
//'12 - 12 - 01
//@auther Aki
//Emulate cat of unix
//this version : 0.1

int printnState(int nState);
void errCheck(int nErr,int nState);
void readFileAndPrintFile(int *pnState,int *pnErr,char *pcFname);

int main(int argc, char *argv[]){
	int nState=0x0;
	int nErr=0x0;
	if(argc==1){
		printf("open text viewer\n");
		printf("cat filename\n");
		nErr|=0x1;
	}
	if(argc>=2)
		nState|=0x2;//input file name 
	if(argc>=3)
		nState|=0x4;//option flag
	//file mode
	if(nState&0x4){
		if(strchr(argv[2],'o')!=NULL)
			nState|=0x8;//over
		if(strchr(argv[2],'a')!=NULL)
			nState|=0x10;//add
		if(strchr(argv[2],'c')!=NULL)
			nState|=0x20;//copy
		if(strchr(argv[2],'w')!=NULL)
			nState|=0x40;//write
		if(strchr(argv[2],'e')!=NULL)
			nState|=0x400;//line
		if(!((nState&0x8)||(nState&0x10)||(nState&0x20)||(nState&0x40)||(nState&0x400)))
			nErr|=0x80;
	}
	
	if((nState&0x2)&&!(nState&0x4)||(nState&0x2)&&(nState&0x400)&&nErr==0x0)
		readFileAndPrintFile(&nState,&nErr,argv[1]);
	if(((nState&0x2)&&(nState&0x40))&&nErr==0x0)
		printf("write process\n");
	if(nState&0x18&&nErr==0x0){
		if(nState&0x8)
			printf("over ");
		else if(nState&0x10)
			printf("add ");
		if(nState&0x20)
			printf("copy\n");
		else if(nState&0x40)
			printf("write\n");
	}

	errCheck(nErr,nState);
	return nErr;
}
void readFileAndPrintFile(int *pnState,int *pnErr,char *pcFname){
	char cBuf[512];
	FILE *fo;
	int enter=0;
	if((fo=fopen(pcFname,"r"))!=NULL)
			*pnState|=0x100;
		else
			*pnErr|=0x200;

	if(*pnState&0x100){
		while(fgets(cBuf,512,fo)!=NULL){
			if(*pnState&0x400){
				if(enter)
					getchar();
				else
					enter++;
			}
			printf("%s",cBuf);
		}
		fclose(fo);
	}
	return;
}
/*nState list
0x1:input size==0
0x2:input filename ok
0x4:option flag ok
0x8:over flag ok
0x10:add  flag ok
0x20:copy flag ok
0x40:write flag ok
0x80:option flag err
0x100:0x100 Can read
0x200:0x100 Can't read
0x400:0x100 Can't read
***********************/
int printnState(int nState){
	printf("--- print nState ---\nnState code 0x%X\n",nState);
	if(nState&0x1)
		printf("0x1 input size==0");
	if(nState&0x2)
		printf("0x2 input file name ok\n");
	if(nState&0x4)
		printf("0x4 option flag ok\n");
	if(nState&0x8)
		printf("0x8 over write flag ok\n");
	if(nState&0x10)
		printf("0x10 add write flag ok\n");
	if(nState&0x20)
		printf("0x20 copy flag err\n");
	if(nState&0x40)
		printf("0x40 write flag ok\n");
	if(nState&0x80)
		printf("0x80 option flag err\n");
	if(nState&0x100)
		printf("0x100 Can read\n");
	if(nState&0x200)
		printf("0x200 Can't read\n");
	if(nState&0x400)
		printf("0x400 line view\n");

	return nState;
}
//err list
//0x1:input size
//0x40:option flag err
//0x200:Can't read
void errCheck(int nErr,int nState){
	if(nErr>0){
		printf("--- err check ---\nerr code : 0x%X\n",nErr);
		printnState(nErr|nState);
	}
	return;
}

2012年11月17日土曜日

Javaで等速円運動 Demo

GUIをこれから勉強しようとしている人へ


import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

/** 球の回転(座標軸の回転) */
public class BallRoll extends JFrame {
 /* costant */
 static final int BALL_W = 100;
 static final int BALL_H = 100;
 /* field */
 private Point axis;// 軸
 private double varX;// 変動値
 private double varY;// 変動値
 private double r;// 軸からボールまでの距離
 private Timer t;// アニメーションタイマー
 private double degrees;// 角度

 /* constructor */
 public BallRoll() {
  super("Ball Roll");
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  // パソコンの画面サイズ取得
  Rectangle screen = getGraphicsConfiguration().getBounds();
  setSize(screen.width / 2, screen.width / 2);
  // windowの位置
  setLocation(screen.x + screen.width / 2 - getWidth() / 2, screen.y
    + screen.height / 2 - getHeight() / 2);
  // variable
  axis = new Point(getWidth() / 2, getHeight() / 2);
  degrees = 0;
  r = getWidth() / 2 - BALL_W - BALL_H;
  // listener
  t = new Timer(5, new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    degrees += 0.1;
    rotation(degrees);
    if (degrees >= 360)
     degrees = 0;
    repaint();
   }
  });
 }

 /* Rotation */
 public void rotation(double degrees) {
  double rad = degrees * (Math.PI / 180F);
  varX = r * Math.cos(rad) - BALL_W / 2;
  varY = r * Math.sin(rad) - BALL_H / 3;
 }

 public void paint(Graphics g) {
  super.paint(g);
  boolean xRegular = varX + BALL_W / 2 > 0;
  boolean yRegular = varY + BALL_H / 3 > 0;
  int split = 7;
  int wLength = getWidth() / split;
  int hLength = getHeight() / split;
  // 格子
  for (int i = 1; i < split; i++) {
   g.drawLine(0, hLength * i, wLength * split, hLength * i);
   g.drawLine(wLength * i, 0, wLength * i, hLength * split);
  }
  // 座標によって色を変える
  if (xRegular && yRegular)
   g.setColor(Color.RED);
  else if (xRegular && !yRegular)
   g.setColor(Color.BLUE);
  else if (!xRegular && yRegular)
   g.setColor(Color.GREEN);
  else if (!xRegular && !yRegular)
   g.setColor(Color.YELLOW);
  // 球の表示
  g.fillOval((int) (this.axis.x + varX), (int) (this.axis.y + varY),
    BALL_W, BALL_H);
  if (!t.isRunning())
   t.start();
 }

 public static void main(String[] args) {
  new BallRoll().setVisible(true);
 }
}

最近、プログラミングをしていて、、、

CでもJavaでも、アルゴリズムを考えるにあたり、数学や物理の知識があることの大切さを改めて気づいた。

いままでのらりくらりと交わしてきたのだが、そろそろ勉強しなくては。。。

極限あたりからやりなおそうかねえ。

2012年11月10日土曜日

Javaで等速運動 Demo

初めてJAVAでGUIを組むときのサンプル

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.Timer;

public class UMotion extends JFrame {
 /* field */
 private Timer doAct;
 //Oval
 private double x_p,y_p;//coordinate
 private int o_wid=30,o_hei=30;//size
 //para
 private double velocity;
 private double time;
 //direction
 private int d=1;
 public UMotion(double s_velocity,double s_time) {
  //スーパークラスのコンストラクタ
  super("Uniform motion");
  //終了のオプション
  setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  //Frameの大きさ
  setSize(600, 200);
  //Computerの画面の大きさ
  Rectangle screen = getGraphicsConfiguration().getBounds();
  //画面の位置
  setLocation(screen.x + screen.width / 2 - getSize().width / 2, screen.y
    + screen.height / 2 - getSize().height / 2);
  
  //10は10/1000秒間毎に実行と言う意味
  doAct=new Timer(10, new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    //端っこに行ったら向きを変える
    //switch
    if((d==1&&x_p>getWidth()-o_wid))
     d=-1;
    else if((d==-1&&x_p<0))
     d=1;
    //移動距離を足していくと移動したことになる
    x_p+=d*velocity*time;
    repaint();
   }
  });
  //coordinate
  x_p=0;
  y_p=120;
  
  this.time=s_time;
  this.velocity=s_velocity;
 }
 //表示
 public void paint(Graphics g ){
  super.paint(g); 
  g.setColor(Color.RED);
  //円,->x座標,y座標,横幅,縦幅
  g.fillOval((int)x_p, (int)y_p-80,o_wid, o_hei);
  //円,->x座標,y座標,横幅,縦幅
  g.fillOval((int)x_p, (int)y_p-40,o_wid, o_hei);

  g.setColor(Color.BLUE);
  //円,->x座標,y座標,横幅,縦幅
  g.fillOval((int)x_p, (int)y_p,o_wid, o_hei);
  //タイマーが動いているかの確認
  if(!doAct.isRunning())
   doAct.start();//タイマーを動作させる
 }
 
 public static void main(String[] args) {
  new UMotion(10,0.05).setVisible(true);//JFrameの表示
 }

}