简体   繁体   中英

Add multiple swing/awt shapes to frame

Ive created a game of Craps which is a dice based game. Ive created my game logic but am trying to implement a GUI. Ive created my Dice and Dice Component for both dice. If i add my Dice to the frame one at a time they both work, but as soon as i try and add both to the frame at the same time and run my Craps game, the frame is empty.

CrapsGame code:

package crapsgame;
import java.awt.FlowLayout;
import javax.swing.*;


public class CrapsGameTester 
{

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    JFrame frame = new JFrame("Craps-Game");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setResizable(false);

    final int FRAME_WIDTH = 400;
    final int FRAME_HEIGHT = 600;

    frame.setSize(FRAME_HEIGHT, FRAME_WIDTH);

    CrapsGame game = new CrapsGame();

    JPanel panel = new JPanel(new FlowLayout());
    frame.add(panel);

    Dice1Component dice1 = new Dice1Component(6);
    Dice2Component dice2 = new Dice2Component(6);
    panel.add(dice1);
    panel.add(dice2);

    String message = game.doFirstRoll();
    System.out.println(message);

    while(!game.isOver())
    { 
        message = game.rollAgain();
        System.out.println(message);
    }

    if(game.isWon())
        System.out.println("You win, big man!");
    else
        System.out.println("Loser ...");

    frame.setVisible(true);
}

}

Dice1 code:

package crapsgame;

import java.awt.*;

public class Dice1 
{
int sides;
int xCoord = 150;
int yCoord = 130;

public Dice1(int s)
{
    sides = s;
}

/**
 * 
 * @param g2 Graphics2D object used by draw method
 */
public void draw(Graphics2D g2)
{
    g2.setColor(Color.BLACK);
    g2.fillRect(xCoord, yCoord, 100, 100);
    g2.setColor(Color.WHITE);

    switch (sides)
    {
        case 1:
            g2.fillOval(xCoord + 40, yCoord + 40, 20, 20);
            break;
        case 2:
            g2.fillOval(xCoord + 15, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 65, 20, 20);
            break;
        case 3:
            g2.fillOval(xCoord + 15, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 40, yCoord + 40, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 65, 20, 20);
            break;
        case 4:
            g2.fillOval(xCoord + 15, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 15, yCoord + 65, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 65, 20, 20);
            break;
        case 5:
            g2.fillOval(xCoord + 15, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 15, yCoord + 65, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 15, 20, 20);
            g2.fillOval(xCoord + 65, yCoord + 65, 20, 20);
            g2.fillOval(xCoord + 40, yCoord + 40, 20, 20);
            break;
        case 6: 
            g2.fillOval(xCoord + 20, yCoord + 20, 20, 20);
            g2.fillOval(xCoord + 20, yCoord + 45, 20, 20);
            g2.fillOval(xCoord + 20, yCoord + 70, 20, 20);
            g2.fillOval(xCoord + 60, yCoord + 20, 20, 20);
            g2.fillOval(xCoord + 60, yCoord + 45, 20, 20);
            g2.fillOval(xCoord + 60, yCoord + 70, 20, 20);
            break;
    }
}
}

Dice1Component code:

package crapsgame;
import java.awt.*;
import javax.swing.*;


public class Dice1Component extends JComponent
{
private int sides;

public Dice1Component(int s)
{
    sides = s;
}

public void setSide(int s)
{
    sides = s;
}

@Override
public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);

    Dice1 dice1 = new Dice1(sides);
    dice1.draw(g2);
}
}

I read that to add more than one component to a frame you must use a JPanel but this doesn't seem to work for me either.

There are a couple of issues (in addition to those mentioned in comments), but these should make the code work:

  • You should override the preferred size in Dice1Component so that the layout manager can reserve it the correct space

     @Override public Dimension getPreferredSize() { return new Dimension(100, 100); } 
  • remove xCoord and yCoord from Dice1 . The coordinates used are relative to the component, so a large offset will try to draw outside the component area
  • Take a look at the tutorial how you should create and access components only in the event dispatch thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM