Javaでバーとボールの画面表示方法

このQ&Aのポイント
  • JavaのBlockBreakクラスでバーとボールを画面に表示する方法について質問があります。
  • バーとボールの表示順序を変更すると、表示されるオブジェクトが切り替わる現象が発生しています。
  • 実行順番によってオブジェクトのpaintメソッドが実行されるようです。Java初心者で困っています。
回答を見る
  • ベストアンサー

Paneに画像を二つ貼り付ける

学校の課題でブロック崩しを作っているのですが、まずはバーとボールを画面に表示させようと思い以下の通りに作ったのですが、バーかボールどちらかしか画面に出てきません。 import java.awt.*; import javax.swing.*; class BlockBreak extends JFrame{ Bar br; Ball ba; public static void main(String[] args){ BlockBreak bb = new BlockBreak(); bb.setSize(300,500); bb.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); bb.setVisible(true); } BlockBreak(){ Container c = this.getContentPane(); ba = new Ball(); br = new Bar(); c.add(ba); c.add(br); } } class Ball extends JComponent{ int x,y; int xf,yf; ImageIcon baim; Image baimg; Ball(){ x = 150; y = 390; baim = new ImageIcon("ball.png"); baimg = baim.getImage(); } public void paint(Graphics g){ g.drawImage(baimg,x,y,this); } } class Bar extends JComponent{ int x,xl; final int y = 400; ImageIcon brim; Image brimg; Bar(){ x = 100; xl = 100; brim = new ImageIcon("bar.png"); brimg = brim.getImage(); } public void paint(Graphics g){ g.drawImage(brimg,x,y,this); } } BlockBreakクラスの c.add(ba); c.add(br); という個所の順番を入れ替えると、バーかボールどちらが画面に表示されるかが切り替わり、後に書いた方が表示されるみたいです。というか、前に書いた方のオブジェクトのpaintメソッド自体が実行されてない気がします(System.out.printlnを埋め込んで確かめてみました)。 Javaは本格的に始めてまだ浅いもので、単純ミスでしたら申し訳ありません。困っているのでご回答願います。

  • myr
  • お礼率91% (95/104)
  • Java
  • 回答数1
  • ありがとう数1

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

  • ベストアンサー
  • ngsvx
  • ベストアンサー率49% (157/315)
回答No.1

後から追加されたコンポーネントが、先に追加されたコンポーネントを上書きしているためでしょう。 関連するキーワードとしては、 ・レイアウトマネージャー」 ・コンポーネントの不透明設定 でしょう。 これで、原因を考えてみてください。 自分で考えるという過程が非常に大事です。

myr
質問者

お礼

回答ありがとうございました。 何とか表示させることが出来ました。

関連するQ&A

  • tabにdrawImageで画像を描画したい

    tabにdrawImageで画像を描画したい 以下のソースでtabbedpaneに対して、ImageIconを指定して画像をのせるではなく、 drawImageメソッドで描画したいです。 ご存知の方がおられましたら教えてください。 import javax.swing.*; import java.awt.BorderLayout; import java.awt.Graphics; import java.awt.Graphics2D; public class JTabbedPaneTest4 extends JFrame { Zoom zoom = null; public static void main(String[] args) { JTabbedPaneTest4 frame = new JTabbedPaneTest4(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setBounds(10, 10, 300, 200); frame.setVisible(true); } JTabbedPaneTest4() { JTabbedPane tabbedpane = new JTabbedPane(); JPanel tabPanel1 = new JPanel(); tabPanel1.add(new JButton("button1")); ImageIcon icon1 = new ImageIcon("img1.jpg"); zoom = new Zoom(icon1, 0, 0, 50, 50); //tabbedpane.addTab("tab1", icon1, tabPanel1);//ok //tabbedpane.add(zoom, tabPanel1);//ng(パネルに書き込まれてしまう) getContentPane().add(tabbedpane, BorderLayout.CENTER); } class Zoom extends JComponent { private static final long serialVersionUID = 1L; private ImageIcon icon = null; private int x = 0; private int y = 0; private int h = 0; private int w = 0; private double scale = 1.0d; public Zoom(ImageIcon icon, int x, int y, int w, int h) { super(); this.icon = icon; this.x = x; this.y = y; this.w = w; this.h = h; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(scale, scale); g2.drawImage(icon.getImage(), x, y, w, h, this); } } }

    • ベストアンサー
    • Java
  • JButtonの画像をactionPerformedメソッド内で再描画

    JButtonの画像をactionPerformedメソッド内で再描画したい。 以下のソースのようにして、再描画したいのです。 setIconメソッドではなく、 JButtonに対して描画したものに対して再描画したいです。 Graphics2DクラスについてJAVA APIで調べましたが、 仕組の理解に至りませんでした。 仕組みと方法を教えて頂きたいです。 よろしくお願いいたします。 import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; public class test extends JFrame implements ActionListener{ JButton b= new JButton(); public static void main(String a[]) { new test(); } public test() { super(); this.setSize(100,100); b.addActionListener(this); b.add(new Zoom(new ImageIcon("img1.jpg"),0,0,50,50)); this.add(b); this.setVisible(true); } class Zoom extends JComponent { private static final long serialVersionUID = 1L; private ImageIcon icon = null; private int x = 0; private int y = 0; private int h = 0; private int w = 0; private double scale = 1.0d; public Zoom(ImageIcon icon, int x, int y, int w, int h) { super(); this.icon = icon; this.x = x; this.y = y; this.w = w; this.h = h; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(scale, scale); ////////////////////////////////// //画僧を再描画したい。 //g2.clearRect(0, 0, 80, 80);//× g2.drawImage(icon.getImage(), x, y, w, h, this); } } public void actionPerformed(ActionEvent e) { if(e.getSource()==b){ System.out.print("ok"); //this.repaint();//× //b.repaint();//× b.add(new Zoom(new ImageIcon("img2.jpg"),0,0,50,50));//(再描画できない) //b.setIcon(new ImageIcon("img2.jpg"));//ok(再描画出来る) } } }

    • ベストアンサー
    • Java
  • JButtonの座標(0,0)にアイコンをセット

    JButtonに以下のように、画像を書込む際、画像を一番は端(左上角)から乗せたいです。 つまり、ボタンの右上角(0,0)から乗せたいのです。 現状では、左端に隙間があいてしまいます。 この方法がご存知の方がおられましたら教えてください。 よろしくお願いいたします。 import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import javax.swing.ImageIcon; import javax.swing.JButton; import javax.swing.JComponent; import javax.swing.JFrame; public class Zoom extends JComponent { private static final long serialVersionUID = 1L; private ImageIcon icon = null; private int x = 0; private int y = 0; private int h = 0; private int w = 0; private double scale = 1.0d; public Zoom(ImageIcon icon, int x, int y, int w, int h) { super(); this.icon = icon; this.x = x; this.y = y; this.w = w; this.h = h; } public void paintComponent(Graphics g) { Graphics2D g2 = (Graphics2D) g; g2.scale(scale, scale); g2.drawImage(icon.getImage(), x, y, w, h, this); } public static void main(String[] args) { ImageIcon icon = new ImageIcon("cherry.jpg"); Zoom zoom = new Zoom(icon,0,0,100,100); JButton b = new JButton(); JFrame f = new JFrame(); b.add(zoom); b.setPreferredSize(new Dimension(icon.getIconHeight(), icon.getIconWidth())); f.add(b); f.pack(); f.setVisible(true); } }

    • ベストアンサー
    • Java
  • 複素数の計算するクラスを足せる人がもしいたら

    このJavaプログラムはAdd(足し算)とSud(引き算)を計算するプログラムです。 import Java.io.*; class INT { public int Add(int x,int y){ return x+y; } public int Sub(int x,int y){ return x-y; } } class REAL extends INT { public double Add( double x,double y){ return x+y; } public double Sub( double x,double y){ return x-y; } } class CAL extends REAL { } … public class Cale { public static void main(String args[]) { CAL c = new CAL(); System.out.println( c.Add(1,2) ); System.out.println( c.Add(1.0,2.0)); System.out.println( c.Sub( 1,2)); System.out.println( c.Sub( 1.0,2.0)); } } このプログラムに「複素数の足し算と引き算をするクラス」をクラスの仕様も併せて 作成することが出来る人がもしいたら宜しくおねがいします☆

  • 初心者です。 コンパイルのエラー

    import java.awt.*; import javax.swing.*; public class R11Sample1 extends JFrame { Rect r1 = new Rect(Color.red, 100, 100, 80, 60); Rect r2 = new Rect(new Color (0.5f, 1f, 0f, 0.7f), 150, 120, 60, 90); Oval = new Oval(Color.blue, 60, 50, 10, 10); JPanel panel = new JPanel() { public void paint(Graphics g) { Graphics2D g2 = (Graphics2D)g; r1.draw(g2); r2.draw(g2); } }; public R11Sample1() { setSize(400, 350); setDefaultCloseOperation(EXIT_ON_CLOSE); getContentPane().add(panel); } public static void main(String[] args) { new R11Sample1().setVisible (true); } } class Rect { Paint pat; int xpos, ypos, width, height; public Rect(Paint p, int x, int y, int w, int h) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillRect(xpos-width/2, ypos-height/2, width, height); } } class Oval { Paint pat; int xpos, ypos, radius; public Oval(Paint p, int x, int y, int width, int height) { pat = p; xpos = x; ypos = y; width = w; height = h; } public void draw(Graphics2D g) { g.setPaint(pat); g.fillOval(xpos-width/2, ypos-height/2, width, height); } } これでコンパイルすると、 Identifierがありません といわれました。 どこを直せばいいのでしょうか。 また、全体的に間違ったところがあったら教えてください。

    • ベストアンサー
    • Java
  • Jave Swing 画像表示わからない

    java swingでGUIをプログラミングしている者です。 JLabelにImageIconの画像を貼り付けるプログラムです。↓ //画像を表示するプログラム //インポート import javax.swing.*; import java.awt.*; import java.awt.event.*; class gazopro extends JFrame{ JLabel l1; JButton b1; JPanel p; public static void main(String args[]) { //ウィンドウを作成また設定 gazopro frame = new gazopro(); frame.setBounds(300,200,700,400); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setVisible(true); } gazopro(){ //コンポーネントを作成 p = new JPanel(); add(p); b1 = new JButton("画像を表示"); p.add(b1); ImageIcon icon = new ImageIcon("image.jpg"); l1 = new JLabel(icon); p.add(l1); } }   わからないところは、ImageIcon icon = new ImageIcon("image.jpg");のところです。 ImageIconの引数は、プログラミングを行っているパソコン(OS は Windows)のマイコンピューター→ Cドライブ→ MyJava→ image.jpgを指定しましたがこれで合ってますか? このプログラムでは、画像が表示されませんでした。 どうすればいいでしょうか? 解説と、ご指導宜しくお願いします。

    • ベストアンサー
    • Java
  • Jlabel?の上に2つ画像を載せクリックを識別

    JLabelを使えばいいのかよくわからないのですが掲示した 画面のように背景色に薄い赤を指定して2つの矢印画像を 表示します。その矢印の中央に年月を表示したいと思っています。 また、右の矢印をクリックしたら翌月の年月が表示され 左の矢印をクリックしたら前月の年月が表示したいです。 これをswingで完成したいと思っています。 また各コンポーネントは setBounds(int x,int y,int width,int height)で出来ると助かります。 ImageIcon leftIcon = new ImageIcon("./img/left.gif"); ImageIcon rightIcon = new ImageIcon("./img/right.gif"); JLabel label = new JLabel(); label.setText("2010年10月"); label.setIcon(leftIcon); label.setLocation(20,0); label.setSize(170, 20); label.setBackground(Color.white); label.setOpaque(true); ここから先がわかりません どうしても1つの画像と1つのテキストしか表示できません。

    • ベストアンサー
    • Java
  • シンボルを見つけられません

    javaについて勉強しているのですが、円の中に四角を作るというプログラムを作ろうとしたところ tes.java:43:シンボルを見つけられません。 シンボル:メソッド drawSquare(int,int,int,java.awt.Graphics) 場所:Assortのクラス a.drawSquare(x,y,w,g); ^ というエラーが出てコンパイルできません。drawCircleからdrawSquareを実行するようにしたいのですが、どうしたらよいか教えていただけないでしょうか。 import javax.swing.*; import java.awt.*; import java.awt.event.*; public class tes { static MyCanvas mc; public static void main(String[] args) { tes ac_listener = new tes(); JFrame jf = new JFrame("tes"); JPanel p = new JPanel(); mc = new MyCanvas(); jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); mc.setPreferredSize(new Dimension(700,700)); jf.getContentPane().add(mc); p.add(mc); jf.getContentPane().add(p); jf.pack(); jf.setVisible(true); } } abstract class Assort{ double x,y; } class Square extends Assort{ public void drawSquare(int x, int y, int w, Graphics g) { double k=Math.sqrt(2); double _w=w/2/k; g.drawRect(x-(int)_w/2, y-(int)_w/2, (int)_w, (int)_w); } } abstract class Circle extends Assort{ } class Circle1 extends Circle { Assort a; Circle1(Assort _a) { a = _a; } void drawCircle(int x, int y, int w, Graphics g){ g.drawOval(x-w/4,y-w/4,w/2,w/2); a.drawSquare(x,y,w,g); } } class MyCanvas extends JPanel { public void paintComponent( Graphics g ) { super.paintComponent(g); Circle1 d1 = new Circle1 ( new Square() ); d1.drawCircle(350,350,700,g); } }

    • ベストアンサー
    • Java
  • 指摘してください!!!

    JAVA APPLETの初歩的な質問ですが… クリックするとマウスの座標を表示するプログラムなのですが、以下のプログラムでは正しく実行しないのだそうです。 正しく実行するにはいくつかのコードを削除しなければならないのですが…。 それはどのコードなのでしょうか? プログラムは以下の通りです。 import java.applet.*; import vava.awt.*; public class Test extends Applet{   int iX,iY;   public void paint(Graphics g){    int iX,iY;    string c = new String();    g.drawString("X座標 = "+c.valueOf(iX)+"Y座標 = "+ c.valueOf(iY),10,20); } public boolean mouseDown(Event e,int x,int Y){  int iX,iY;  iX = x;  iY = y;  repaint();  return true; } } 宜しくお願いします。

    • ベストアンサー
    • Java
  • Swing の実装でどうしてもエラーになります。

    初心者ですみませ。 次のリストがどうしてコンパイルを通っても実行時にエラーになってしまいます。どなたか、判る方原因を教えてください。 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 figures; protected Figure drawingFigure; protected Color currentColor; protected DrawPanel drawPanel; public DrawApplication() { figures = new Vector(); drawingFigure = null; currentColor = Color.red; } 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); 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; } public void paintComponent(Graphics g) { super.paintComponent(g); // //[すべてのFigureをpaintする] // Figure f = new RectangleFigure(0,0,0,0,drawApplication.currentColor); for(int i=0;i<drawApplication.getNumberOfFigures();i++){ f = drawApplication.getFigure(i); 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"); // //[DrawApplicationとDrawPanelとDrawMouseListenerを作って組み立てる] // 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