Javaで四角をつくったのですが・・・

このQ&Aのポイント
  • Javaで四角を作成する方法について
  • 四角を二倍ずつ大きくする方法について
  • 要素を右に進むごとに四角を二倍にする方法
回答を見る
  • ベストアンサー

Javaで四角をつくったのですが・・・

とりあえずこの文をみてください。 import java.applet.Applet; import java.awt.*; public class Face extends Applet { public final static int step = 2; public final static int x0rg = 10; public final static int y0rg = 20; public void paint(Graphics g){ for(int i = 0; i < 3; i++) { for(int j = 0; j < 1; j++) { drawFace(g, x0rg + 18 * step * i, y0rg + 18 * step * j); } } drawFace(g, x0rg, y0rg); } void drawFace(Graphics g, int xStart, int yStart) { drawFrame(g, xStart, yStart); } void drawFrame(Graphics g, int xStart, int yStart) { g.drawRect(xStart, yStart, 16 * step, 16 * step); } } とこのような命令で四角を作ったとき、四角が三つ表示されるのですが、ここから一つ目(一番左の四角)の四角の二倍の大きさの箱を二つ目の四角に、さらに三つ目の四角はその二倍にしたいのですがどのようにすればできるのでしょうか?ようするに右にいくごとに二倍にしていきたいのです。 どうかお願いします。

  • Java
  • 回答数3
  • ありがとう数5

質問者が選んだベストアンサー

  • ベストアンサー
  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.3

とりあえず、顔バージョンにしてみました。 わざわざPointやDimensionを使う必要もないけど、 多少なりとも読みやすくなるかも・ こういう、処理では、基本の処理は相対位置で全て表してやるのが定番です。 import java.applet.Applet; import java.awt.*; /* appletviewer Face.java <APPLET CODE="Face.class" WIDTH="300" HEIGHT="200"></APPLET> */ public class Face extends Applet { public final static Point BasePoint = new Point(10, 20); public final static int FACE_SIZE = 32; //顔の大きさ public final static int GAP = 4; //顔と顔の間の幅 public void paint(Graphics g){ Point pos = new Point(BasePoint); int zoom = 1;//倍率 for(int i = 0; i < 3; i++) { drawFace(g, pos, zoom); pos.translate(FACE_SIZE * zoom + GAP, 0); //描画の基準の位置をずらす zoom *=2; } } void drawFace(Graphics g, Point pos, int zoom){ drawFrame(g, pos, zoom); drawEyeBrow(g, pos, zoom); drawEye(g, pos, zoom); drawNose(g, pos, zoom); drawMouth(g, pos, zoom); } void drawFrame(Graphics g, Point pos, int zoom){ g.drawRect(pos.x, pos.y, FACE_SIZE * zoom, FACE_SIZE * zoom); } void drawEyeBrow(Graphics g, Point pos, int zoom){ Point LPos = new Point(6*zoom , 8*zoom); //左の眉の相対位置 Point RPos = new Point(22*zoom, 8*zoom); //右の眉の相対位置 Dimension size = new Dimension(4*zoom, 0*zoom); //長さ4の真横の眉 LPos.translate(pos.x, pos.y); //基準の位置からの相対位置に移動 g.drawLine(LPos.x , LPos.y, LPos.x+size.width, LPos.y+size.height); RPos.translate(pos.x, pos.y); g.drawLine(RPos.x , RPos.y, RPos.x+size.width, RPos.y+size.height); } void drawEye(Graphics g, Point pos, int zoom){ Point LPos = new Point(6*zoom , 14*zoom); //左の目の相対位置 Point RPos = new Point(22*zoom, 14*zoom); //右の目の相対位置 int size = 4*zoom; //目の大きさ LPos.translate(pos.x, pos.y); //基準の位置からの相対位置に移動 g.fillOval(LPos.x , LPos.y, size, size); RPos.translate(pos.x, pos.y); g.fillOval(RPos.x , RPos.y, size, size); } void drawNose(Graphics g, Point pos, int zoom){ Point NPos = new Point(16*zoom, 18*zoom); //鼻の相対位置 Dimension size = new Dimension(0*zoom, 6*zoom); //長さ6の縦の鼻 NPos.translate(pos.x, pos.y); g.drawLine(NPos.x, NPos.y, NPos.x + size.width, NPos.y + size.height); } void drawMouth(Graphics g, Point pos, int zoom){ Point MPos = new Point(8*zoom, 28*zoom); //口の相対位置 Dimension size = new Dimension(16*zoom, 0*zoom); //長さ16の真横の口 MPos.translate(pos.x, pos.y); g.drawLine(MPos.x, MPos.y, MPos.x + size.width, MPos.y + size.height); } }

bluewing1999
質問者

お礼

ありがとうございます!! まだやってないですが、やってみたいと思います。 というか最強の初心者なので、その分の意味とかわざわざつけてもらいかなりわかりやすくて最高です!! また、わからないことがあったら、BLUEPIXYさん教えてください。必ず点数はあげますので!!

その他の回答 (2)

  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.2

>この場合 結局何がしたいのか意味がよくとれませんでした 下のどれの意味でしょうか? ・顔を3つ描いて、2倍、4倍とする ・顔を描く ・その他

bluewing1999
質問者

お礼

わざわざありがとうございます! とりあえず回答1の命令は私の思ったとおりにできていました!! その四角の中にただ、目、眉毛、鼻、口などのものを追加するといったものです! そう、この四角は輪郭なのです!! 実は最初からこれをやりたかったのですが、文が収まりきらなくて・・・

  • BLUEPIXY
  • ベストアンサー率50% (3003/5914)
回答No.1

こんな感じで良いかと思います。 import java.applet.Applet; import java.awt.*; /* <APPLET CODE="Face.class" WIDTH="300" HEIGHT="200"></APPLET> */ public class Face extends Applet { public final static int xOrg = 10; public final static int yOrg = 20; public final static int SQUARE_BASE_SIZE = 32; //正方形の一辺の基本の長さ public final static int GAP = 4; //正方形と正方形の間の長さ public void paint(Graphics g){ int xPos = xOrg; int zoom = 1; for(int i = 0; i < 3; i++) { drawFace(g, xPos, yOrg, zoom); xPos += SQUARE_BASE_SIZE * zoom + GAP; zoom *=2; } } void drawFace(Graphics g, int xStart, int yStart, int zoom) { g.drawRect(xStart, yStart, SQUARE_BASE_SIZE * zoom, SQUARE_BASE_SIZE * zoom); } }

bluewing1999
質問者

お礼

ありがとうございます。 でも、さらに知りたいことがあって・・・ もしわかったら教えてください!補足にいれておきましたのでみてくれたらうれしいです。

bluewing1999
質問者

補足

ありがとうございます。では私の文で言うとこのような命令を追加するとします。 void drawEyeBrow(Graphics g, int xStart, int yStart) { /// 定義する側 g.drawLine(xStart + 3 * step, yStart + 4 * step, xStart + 5 * step, yStart + 4 * step); g.drawLine(xStart + 11 * step, yStart + 4 * step, xStart + 13 * step, yStart + 4 * step); } void drawEye(Graphics g, int xStart, int yStart) { /// 定義する側 g.fillOval(xStart + 3 * step, yStart + 7 * step, 2 * step, 2 * step); g.fillOval(xStart + 11 * step, yStart + 7 * step, 2 * step, 2 * step); } void drawNose(Graphics g, int xStart, int yStart) { /// 定義する側 g.drawLine(xStart + 8 * step, yStart + 9 * step, xStart + 8 * step, yStart + 12 * step); } void drawMouth(Graphics g, int xStart, int yStart) { /// 定義する側 g.drawLine(xStart + 4 * step, yStart + 14 * step, xStart + 12 * step, yStart + 14 * step); } }//class そうすると、顔ができあがるとおもうのですが、この場合はBLUEPIXYさんの文で行った場合どのようにすればいいのでしょうか?教えてください。

関連するQ&A

  • Java 四角を書くツール

    Javaの初心者です。 下のプログラムで(2)のfを(1)のfに対応付けたいのですが、エラーが出てしまいます。 どなたか判る方いらっしゃいましたら教えてください。 import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.util.*; abstract class Figure { protected int x,y,width,height; protected Color color; public Figure(int x,int y,int w,int h,Color c) { this.x = x; this.y = y; width = w; height = h; color = c; } public void setSize(int w,int h) { width = w; height = h; } public void setLocation(int x,int y) { this.x = x; this.y = y; } abstract public void reshape(int x1,int y1,int x2,int y2); abstract public void paint(Graphics g); } class RectangleFigure extends Figure { public RectangleFigure(int x,int y,int w,int h,Color c) { super(x,y,w,h,c); } public void reshape(int x1,int y1,int x2,int y2) { int newx = Math.min(x1,x2); int newy = Math.min(y1,y2); int neww = Math.abs(x1 - x2); int newh = Math.abs(y1 - y2); setLocation(newx,newy); setSize(neww,newh); } public void paint(Graphics g) { g.setColor(color); g.drawRect(x,y,width,height); } } class DrawApplication { protected Vector<Figure> figures; protected Figure drawingFigure; static Color currentColor; protected DrawPanel drawPanel; public DrawApplication() { figures = new Vector<Figure>(); drawingFigure = null; currentColor = Color.blue; } public void setDrawPanel(DrawPanel c) { drawPanel = c; } public int getNumberOfFigures() { return figures.size(); } public Figure getFigure(int index) { return (Figure)figures.elementAt(index); } public void createFigure(int x,int y) { Figure f = new RectangleFigure(x,y,0,0,currentColor);                 // (1) figures.addElement(f); drawingFigure = f; drawPanel.repaint(); } public void reshapeFigure(int x1,int y1,int x2,int y2) { if (drawingFigure != null) { drawingFigure.reshape(x1,y1,x2,y2); drawPanel.repaint(); } } } class DrawPanel extends JPanel { protected DrawApplication drawApplication; public DrawPanel(DrawApplication app) { setBackground(Color.white); drawApplication = app; drawApplication.setDrawPanel(this); } public void paintComponent(Graphics g) { super.paintComponent(g); for(int i=0; i < drawApplication.getNumberOfFigures(); i++){ f = drawApplication.getFigure(i);           //(2) この部分にエラーが出てしまう f.paint(g); // } } } class DrawMouseListener implements MouseListener,MouseMotionListener { protected DrawApplication drawApplication; protected int dragStartX,dragStartY; public DrawMouseListener(DrawApplication a) { drawApplication = a; } public void mouseClicked(MouseEvent e) { } public void mousePressed(MouseEvent e) { dragStartX = e.getX(); dragStartY = e.getY(); drawApplication.createFigure(dragStartX,dragStartY); } public void mouseReleased(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseDragged(MouseEvent e) { drawApplication.reshapeFigure(dragStartX,dragStartY,e.getX(),e.getY()); } public void mouseMoved(MouseEvent e) { } } class DrawMain { public static void main(String argv[]) { JFrame f = new JFrame("Draw"); ButtonPanel b=new ButtonPanel(); DrawApplication app = new DrawApplication(); JPanel c = new DrawPanel(app); c.addMouseListener(new DrawMouseListener(app)); c.addMouseMotionListener(new DrawMouseListener(app)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.getContentPane().add(c,BorderLayout.CENTER); f.setSize(400,300); f.setVisible(true); } }

    • ベストアンサー
    • Java
  • Javaを説明するには

    4枚の絵をパラパラアニメにするソースなのですが。これをわけ合って説明しなくてはならなくなってしまいました。 薄らぼんやりとはわかるのですが。どう説明したらいいのかさっぱり分かりません、どう説明すれば、分かりやすく正しく伝えられるでしょうか?>< import java.applet.Applet; import java.awt.Graphics; import java.awt.Image; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class DNA extends Applet implements Runnable, ActionListener { Image image[] = new Image[4]; int[] timing = { 400,350,230,350}; Thread t; int index1 = 0; int no = 0; private static final long serialVersionUID = 1L; public void init(){ for(int i = 0; i<4; i++){ image[i] = getImage(getDocumentBase(),"img"+(i)+".gif"); } } public void paint(Graphics g){ g.drawImage(image[index1],0,0,this); } public void update(Graphics g) { paint(g); if(++no==4)no=0; } public void start(){ t = new Thread(this); t.start(); } public void run() { while(true){ index1++; if(index1 == 4){ index1 = 0; } repaint(); try{ Thread.sleep(timing[no]); }catch(InterruptedException e){} }} public void actionPerformed(ActionEvent arg0) { } }

  • javaアプレットでのy=x^2のグラフ

    javaアプレットでy=x^2のグラフを描写したいのですが、現在の状態ではグラフの右側しか画面に現れません。軸の移動をすればよいと思うのですが、どのように修正すればよいのでしょうか? import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; public class test1 extends Applet{ public void paint(Graphics g){ g.setColor(Color.red); int x,yp=100,xp=0; for(x=0;x<200;x++){ int y=(int)(x*x)*(-1)+100; g.drawLine(xp,yp,x,y); xp = x; yp = y; } } }

  • javaのエラーの意味がわかりません、お願いします

    今日は、javaを勉強している初心者です。 以下のコードを書いてやりましたが、「sleep(Graphics)は引数()に適用できません」、「Color.Whiteを解決できません」とエラーがでます、エラーの意味が分かりません。 一体何処が間違っているのでしょうか、宜しくお願いします。 ===================================================================== public class ani_Moving_Ball extends Applet { int x; public void paint(Graphics g) { for(x=0 ; x<180 ; ++x) { clear(g); g.drawOval(x,90,19,19); sleep(); } } public void clear(Graphics g) { g.setColor(Color.White); g.fillRect(0,80,200,40); g.setColor(Color.Black); } public void sleep(Graphics g) { double s=0.0; for (int j=1 ; j<100 ; ++j) { for (int k=1 ; k<100 ; ++k) { s =+ Math.sin((double)j); } } } }

    • ベストアンサー
    • Java
  • javaのことで

    ---MainPanel.java--- import java.awt.Dimension; import java.awt.Graphics; import java.event.MouseEvent; import java.event.MouseListener; import javax.swing.JPanel; import java.util.Random; public class MainPanel extends JPanel implements MouseListener { public static final int WIDTH = 640; public static final int HEIGHT = 480; private static final int NUM_FILE = 4; private File[] file; private int prev; public MainPanel() { setPreferredSize(new Dimension(WIDTH, HEIGHT)); file = new File[NUM_FILE]; for (int i = 0; i < NUM_FILE; i++) { file[i] = new File(i, this); } select(); addMouseListener(this); } private void clear() { for (int i = 0; i < NUM_FILE; i++) { file[i].delete(); } } private boolean check(int a, int b) { return a == b || a == prev || b == prev; } private void select() { Random rand = new Random(); int a, b; do { a = rand.nextInt(NUM_FILE); b = rand.nextInt(NUM_FILE); } while (check(a, b)); file[a].set(0, 0); file[b].set(file[a].getX() + file[a].getWith() + File.SPACE, 0); } public void paintComponent(Graphics g) { super.paintComponent(g); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { file[i].draw(g); } g.drawString("ファイル" + (i + 1) + " … " + file[i].getCnt(), 0, HEIGHT * 3 / 4 + i * 15); } } public void mouseClicked(MouseEvent e) { int x = e.getX(); int y = e.getY(); for (int i = 0; i < NUM_FILE; i++) { if (file[i].isSelected()) { // 表示されていて // 画像内なら if (x > file[i].getX() && x < file[i].getX() + file[i].getWidth() && y < file[i].getY() && y > file[i].getY() + file[i].getHeight()) { file[i].count(); prev = i; clear(); select(); break; } } } repaint(); } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } } フレームを用意してMainPanelを付加し,pack();してください.

  • java リアルタイムでマウスの座標を取得したい

    マウスのリアルタイム座標を取得したいのですが、 以下のソースを作りました。 しかし、リアルタイムどころか、マウスをクリックしても座標が取得できません どこを修正すればいいのでしょうか? import java.applet.Applet; import java.awt.*; import java.awt.event.*; import java.awt.event.MouseEvent; import java.net.URL; import java.awt.Image; /*<applet code="applet001" width="640" height="480"></applet>*/ public class applet001 extends Applet implements MouseListener, MouseMotionListener { Dimension dim; int mouse_x, mouse_y; //アプレットの初期化時呼び出される public void init() { dim = getSize(); addMouseListener( this ); } public void update(Graphics g) //オーバーライドして最低限のことだけをする { paint(g); } public void paint(Graphics g) { g.drawString( "マウス:" + mouse_x + "," + mouse_y, 60, 120 ); g.fillOval(mouse_x,mouse_y,30,30); } // マウスの処理 public void mousePressed( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); } public void mouseClicked( MouseEvent e ) {} public void mouseReleased( MouseEvent e ) {} public void mouseEntered( MouseEvent e ) {} public void mouseDragged( MouseEvent e ) {} // public void mouseExited( MouseEvent e ) {} // public void mouseMoved( MouseEvent e ) { mouse_x = e.getX(); mouse_y = e.getY(); repaint(); } }

    • ベストアンサー
    • Java
  • javaのプログラムについて

    質問させていただきます // 数値積分 class Integral1 { public static void main(String[] args) { final int n = 100; // 区間数 final double a = 0; // 始点 final double b = 1; // 終点 final double h = (b-a) / n; // 区間の幅 double x, y; double S = 0; // Sを0で初期化 int i; // 区間 for(i = 1; i <= n ; i++) { // 区間1からnまで x = a + h * i; // 区分積分 y = Math.sqrt(1-x*x); // yを計算 S += y * h; } System.out.println("S = " + S + " 4S = " + 4*S); } } これは区分積分法で計算する数値積分のプログラムなのですが、 これをシンプソン法に改造したプログラムに直していただきたいのですが・・・ どなたかお願いします

  • Javaのアプレットについて質問です。

    星型の図形をアプレットビューワ上でマウスを押した場所に移動させたいのですが・・・ import java.applet.Applet; import java.awt.Graphics; import java.awt.Color; import java.awt.Font; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; import java.awt.Polygon; public class Enshu1_5 extends Applet implements MouseListener { int px []={100,80,10,70,40,100,160,130,190,120}; int py []={10,75,75,113,190,140,190,113,75,75}; private Polygon poly = new Polygon(px,py,10 ); public void init() { addMouseListener(this); } public void mouseClicked(MouseEvent e) { } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void paint(Graphics g) { g.setColor(Color.red); g.fillPolygon(poly); g.setFont(new Font("serif",Font.BOLD,24)); g.drawString("Star",100,200); } } こんな感じでつくってみたのですが現在位置とクリック位置との差分割り出し方をpublic void mouseClicked(MouseEvent e)の所に書きたいのですが普通にpoly.translate( e.getX(), e.getY() );をしてしまうと原点からの移動になってしまうため、フィールド上にint x=0,y=0をつくりmouseClickedのところで if(e.getX()>x&&e.getY()>y) { int dx=e.getX()-x; int dy=e.getY()-y; x=dx; y=dy; poly.translate(dx,dy); repaint(); }の様な感じで考えられる条件を条件分岐していこうと思ったのですがどうもうまくいきませんしかなりかさばったものになってしまうと思います。何かいい方法はないでしょうか? 教えて下さい><

    • ベストアンサー
    • Java
  • アプレットの配列をテキスト形式で保存する

    ある画像の上をマウスクリックするとそこに赤いマーカーが表示され、さらにそのマーカーの座標をテキスト形式で保存できるプログラムを組みたいと考えています。マーカー表示はできましたが、保存方法がまったくわかりません。35箇所のマーカー座標を保存したいのですが…。 どなたか教えていただけませんでしょうか。 ブラウザ上で動作させたいので、JavaAppletにて組んでいます。 import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class sample extends Applet{ private int[] xpos = new int[35]; private int[] ypos = new int[35]; private int num = 0; Image imgs; public sample(){ addMouseListener(new MyMouseAdapter()); } public void init(){ imgs = getImage(getDocumentBase(),"sample.gif"); } public void paint(Graphics g){ g.drawImage(imgs,0,0,this); g.setColor(Color.red); for(int i = 0; i < num; i++){ g.fillOval(xpos[i]-2, ypos[i]-2, 4, 4); } } class MyMouseAdapter extends MouseAdapter { public void mouseClicked(MouseEvent e){ if(num >= 35){ return; } int x,y; x = e.getX(); y = e.getY(); xpos[num] = x; ypos[num] = y; num++; Graphics g = getGraphics(); g.setColor(Color.red); g.fillOval(x-2, y-2, 4, 4); } } }

  • 対数表のプログラミング java

    どうもです。 以下のプログラミングをjavaで作成したいのですが、どうすればよいでしょうか? x = 1.00, 1.01, 1.02,・・・, 5.47, 5.48, 5.49に対して、log(10)xを示す対数表を作成しなさい 書式指定無し ちなみに自分で書いてみたらこうなりました import java.until.Scanner; class Taisuu { public static void main(String[] args) { final int N = 10; final int N = 100; System.out.println("+0.00%1d", j); System.out.println(); double base = 1; for (int i=0; i<M; ++i) { double x = base + 0.01*i; System.out.printf("%4.2f", x); for (int j=0; j<N; ++j) { double dx = 0.001*j; int log10x = (int)(1000000*Math.log10(x+dx)); System.out.printf("%06d", log10x); } System.out.println(); } } }

専門家に質問してみよう