Swing 1 examples class
The examples in this exercise are designed to increase confidence in layout in Swing programs – i.e. how can I make things go where I want them to go in a window. We will be dealing with panels, but the same principles hold when placing other components in a window (i.e. buttons, textAreas etc.)
A. Jazzy Wallpaper
This part of the exercise leads to a JFrame window that looks like Fig. 1 below:
Copyright By PowCoder代写 加微信 powcoder
Fig 2: FillPanel
Fig 1: Wallpaper
1. Below is Java code for a component that consists of a panel containing a solid block of colour surrounded by a border in a different colour (see Fig.2 above). Note that we don¡¯t specify the overall size of the component, just the size of the border.
import java.awt.BorderLayout; import java.awt.Color;
import javax.swing.BorderFactory; import javax.swing.JPanel; import javax.swing.border.Border;
public class FilledPanel extends JPanel{
public FilledPanel(int nBorder, int wBorder, int sBorder,
int eBorder,Color borderColor, Color fillColor)
this.setLayout(new BorderLayout()); this.setBackground(borderColor); Border emptyBorder =
BorderFactory.createEmptyBorder(nBorder,wBorder,sBorder,eBorder); this.setBorder(emptyBorder);
JPanel innerPanel = new JPanel(); innerPanel.setBackground(fillColor); this.add(innerPanel);
(a) What is the layout used for the component?
(b) What is the layout for the panel (innerPanel) containing the block that has colour
fillColour?
(c) On a piece of paper, construct a FilledPanel component step-by-step by
following the code (i.e. start off with a rectangle, add the border etc.)
Now I want to create a component similar to the previous one, but with a black line around the edge of component. i.e. I want a component that looks like Fig. 3 below:
FilledPanelWithLine
If I try to just add a black line border to the panel I can¡¯t also have the empty border that I used before. So I need to put everything onto another internal panel like so:
//import statments omitted
public class FilledPanelWithLine extends JPanel{
public FilledPanelWithLine(int nBorder, int wBorder, int sBorder, int eBorder,Color borderColor, Color fillColor)
this.setLayout(new BorderLayout());
Border blackline = BorderFactory.createLineBorder(Color.black);
this.setBorder(blackline);
// need another panel for the inner border
JPanel mainPanel = new JPanel(new BorderLayout()); mainPanel.setBackground(borderColor);
Border emptyBorder =
BorderFactory.createEmptyBorder(nBorder,wBorder,sBorder,eBorder); mainPanel.setBorder(emptyBorder);
JPanel innerPanel = new JPanel(); innerPanel.setBackground(fillColor); mainPanel.add(innerPanel);
this.add(mainPanel);// attach new panel to container
(a) Compare the two classes and list the differences between them
(b) Similar to before, on a piece of paper, construct a FilledPanelWithLine component step-by-step by following the code (i.e. start off with a rectangle, add the black line border etc.)
3. To create the Wallpaper frame I just need to use a gridLayout and add appropriate filledPanel components:
// import statements omitted
public class Wallpaper1 extends JFrame {
public Wallpaper1(){
// the usual JFrame stuff
final int UNIT = 20;
this.setLocation(20,20); this.setTitle(“Wallpaper1”);
this.setSize(30*UNIT, 30*UNIT); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new GridLayout(3,2));
// add FilledPanels to each position in the grid
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.red,Color.white));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.white,Color.green));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.green,Color.blue));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.blue,Color.yellow));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.yellow,Color.cyan));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.cyan,Color.red));
public static void main(String[] args){ Wallpaper1 gui = new Wallpaper1(); gui.setVisible(true);
(a) Note how I have set a constant UNIT and stated all other measurements in terms of that value. How could I easily rescale my JFrame window so that it was, say, four fifths of the size?
(b) How would I amend my Wallpaper1 code (to Wallpaper2) so that each of the coloured rectangles is in a black line border? i.e. so that my wallpaper looks like Fig. 4 below.
(c) How would I amend it so that there was a horizontal and vertical gap of 1/2 unit between each panel (as in Fig 5)?
Fig. 4: Wallpaper with black lines
Fig. 5: Wallpaper with black lines and gaps between panels
B. Battleships
In the sketch below (Fig.6), I have planned out a simplified game of battleships. Write a program, Battleships.java to reproduce the picture within a JFrame window (see Fig. 7). You should just adapt the Wallpaper1 program from part A. Note that the dashed lines in Fig. 3 are just for guidance and should be omitted, and you can use colours gray and DARK_GRAY for the background and ships respectively.
Hint: 1 square = 1 unit!
Fig.6. Battleships sketch
Fig.7. Battleships JFrame window
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com