Swing 1 examples class solutions
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 on sheet). 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;
Copyright By PowCoder代写 加微信 powcoder
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);
What is the layout used for the component? Why do you think that this layout was Used?
Answer: BorderLayout is used. This is so that the contents of the panel fill the space available (hence allowing us to produce the block of colour).
(b) What is the layout for the panel (innerPanel) containing the block that has colour fillColour?
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.)
Answer: just draw it!
Now I want to create a component similar to the previous one, but with a black line
Answer: as no layout is specified it is FlowLayout (the default for a JPanel).
around the edge of component. I.e. I want a component that looks like Fig. 3 on the sheet.
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
Answer: Firstly the component JPanel is given a black line border (rather than an empty one). This means that to get the coloured “empty” border we have to create another panel to add to this one, namely mainPanel. We then do all the things we did to the component panel in FilledPanel but do it to m ainPanel here. Finally we add mainPanel to this.
(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.)
Answer: again – just draw it!
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?
Answer: just replace the declaration of UNIT so that it has size 16.
(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 on the sheet.
Answer: Just add FilledPanelWithLine components rather than FilledPanel components in the code above.
(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 on sheet)?
Answer: change the setLayout statement to: this.setLayout(new GridLayout(3,2,UNIT/2,UNIT/2));
B. Battleships
In the sketch on the sheet (Fig.6), I have planned out a simplified game of battleships (which I will explain in the class). Write a program, Battleships.java to reproduce the picture within a JFrame window (see Fig. 7 on the sheet). 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!
Answer: Here is my Battleships.java code. For example, notice that in the first grid of my
GridLayout there will be a border of size 6,3,1 and 3 units (starting from the North direction
and working anticlockwise)
import java.awt.Color; import java.awt.GridLayout; import javax.swing.JFrame;
public class Battleships extends JFrame {
public Battleships(){
final int UNIT = 20;
this.setLocation(20,20);
this.setTitle(”Battleships”);
this.setSize(30*UNIT, 30*UNIT); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setLayout(new GridLayout(3,3));
this.add(new FilledPanel(6*UNIT,3*UNIT,1*UNIT,3*UNIT, Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(2*UNIT,4*UNIT,1*UNIT,5*UNIT, Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(2*UNIT,3*UNIT,2*UNIT,UNIT, Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(UNIT,UNIT,7*UNIT,8*UNIT, Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(UNIT,UNIT,UNIT,UNIT, Color.gray,Color.DARK_GRAY)); this.add(new FilledPanel(UNIT,6*UNIT,6*UNIT,2*UNIT,
Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(4*UNIT,2*UNIT,4*UNIT,UNIT,
Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(7*UNIT,7*UNIT,UNIT,UNIT,
Color.gray,Color.DARK_GRAY));
this.add(new FilledPanel(1*UNIT,2*UNIT,4*UNIT,2*UNIT,
Color.gray,Color.DARK_GRAY));
public static void main(String[] args){ Battleships gui = new Battleships();
gui.setVisible(true); }
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com