public static void main(String[] args) { DrawingPanel drawing = new DrawingPanel(200, 200); Graphics canvas = drawing.getGraphics(); canvas.setColor(Colors.black); canvas.fillRect(0, 0, drawing.getWidth(), drawing.getHeight()); canvas.setColor(Colors.white); for (int i = 5; i > 0; i--) { canvas.fillRect(i * 20, (5 - i) * 20, 20, 20); } }
public static DrawingPanel background(String meaning) { DrawingPanel panel = new DrawingPanel(780, 560); Graphics g = panel.getGraphics(); panel.setBackground(Color.white); g.setColor(Color.LIGHT_GRAY); g.fillRect(0, 0, 781, bannerHeight); g.fillRect(0, 530, 781, bannerHeight); g.setColor(Color.black); g.drawString(meaning, 0, 16); for (int i = startYear; i <= 2010; i += 10) { g.drawString("" + i, (decadeWidth / 10) * (i - startYear), 552); } return panel; }
/** * Draws the image and the grid. * * @param panel * @param g */ public void draw(DrawingPanel panel, Graphics g) { if (scaleFactor < 1) { g.drawImage( image.getScaledInstance( (int) (scaleFactor * image.getWidth()), (int) (scaleFactor * image.getHeight()), java.awt.Image.SCALE_REPLICATE), panel.getLeftGutter(), panel.getTopGutter(), panel); } else { // g.drawImage(image, 1+panel.xToPix(xmin), 1+panel.yToPix(ymax), panel); g.drawImage(image, panel.getLeftGutter(), panel.getTopGutter(), panel); } }
public ControlPanel(DrawingPanel canvas) { // instance variable draw has been assigned to canvas this.draw = canvas; // adds new Button this.color = new JButton("Add Color"); // assigns a listener with the button this.color.addActionListener(new ColorButton()); // adds button to the JPanel this.add(this.color); // asigns currentColor to a new JPanel this.currentColor = new JPanel(); // adds currentColor to JPanel this.add(this.currentColor); // sets background to default color currentColor.setBackground(draw.getColor()); // adds a new Button this.circle = new JButton("Add Circle"); // asigns a listener to the button this.circle.addActionListener(new CircleButton()); // adds button to the panel this.add(this.circle); // adds new button this.square = new JButton("Add Square"); // asigns this button to a listener this.square.addActionListener(new SquareButton()); // adds button to the panel this.add(this.square); }
{ // Set the layout to the frame in order to show the panels, BorderLayout used. setLayout(new BorderLayout()); // program will stop running when "X" is pressed. setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE); // set the title for the program. this.setTitle("Financial Modeller - B00540772"); // add the two frames to the frame show that they will show in the correct areas. this.add(drawing, BorderLayout.NORTH); this.add(control, BorderLayout.SOUTH); // add the instructions panel into the frame. this.add(instructions, BorderLayout.CENTER); // set the size of the two panels. drawing.setPreferredSize(new Dimension(900, 350)); control.setPreferredSize(new Dimension(900, 250)); instructions.setPreferredSize(new Dimension(900, 80)); // set the background of the DrawingPanel to highlight the two different panels. drawing.setBackground(Color.WHITE); // call the line wrap method to improve the layout of the text area. instructions.setLineWrap(true); // insert the instructions for the user. instructions.append( "Please use the control panel to enter your financial" + " information. When the information has been entered press 'Start'" + " to run the simulation. The textual output will show each" + " input and output as it happens, and will give you a balance update." + " The graphical output will give you a representation of the changing" + " weekly balance, each second represents" + " one week. Please be aware that if the balance goes above £16000" + " or the duration goes above 52 weeks the graphical output will stop," + " if this happens you can continue to use the textual output." + " If you do not wish to use all of the threads please leave amount AND" + " frequency as '0' and that thread will not start."); // set visible and pack to ensure it will run correctly. pack(); setVisible(true); }
// Creates a window and draws a traffic light inside it private void DrawTrafficLight() { // Instantiates a Drawing panel DrawingPanel panel = new DrawingPanel(130, 290); panel.setTitle("Traffic Light"); // Get graphics from panel Graphics draw = panel.getGraphics(); // Draw light housing drawRect(draw, 20, 20, 90, 250, Color.lightGray); // Draw red light drawCircle(draw, 30, 30, 70, Color.red); // Draw yellow light drawCircle(draw, 30, 110, 70, Color.yellow); // Draw green light drawCircle(draw, 30, 190, 70, new Color(0, 127, 0)); }
// Draws nested shapes from the lab private void drawNestedShapes() { // Instantiates a Drawing panel DrawingPanel panel = new DrawingPanel(250, 250); panel.setTitle("Nested Shapes"); // Get graphics from panel Graphics draw = panel.getGraphics(); // Draw green square drawRect(draw, 25, 25, 200, 200, Color.green); // Draw cyan circle drawCircle(draw, 25, 25, 200, Color.magenta); // Draw cyan diamond draw.setColor(Color.cyan); draw.fillPolygon(new int[] {125, 224, 124, 25}, new int[] {25, 124, 224, 125}, 4); // Draw outline around diamond draw.setColor(Color.black); draw.drawPolygon(new int[] {125, 224, 124, 25}, new int[] {25, 124, 224, 125}, 4); }
/** * Gets the dimension of the lattice in pixel units. * * @param panel * @return the dimension */ public Dimension getInterior(DrawingPanel panel) { float availableWidth = panel.getWidth() - panel.getLeftGutter() - panel.getRightGutter() - 1; float availableHeight = panel.getHeight() - panel.getTopGutter() - panel.getBottomGutter() - 1; scaleFactor = Math.min(availableWidth / dimension.width, availableHeight / dimension.height); if (scaleFactor > 1) { scaleFactor = 1; return dimension; } return new Dimension((int) (scaleFactor * ncol), (int) (scaleFactor * nrow)); }
public static void bars(DrawingPanel panel, String nameInfo) { Graphics g = panel.getGraphics(); Scanner s = new Scanner(nameInfo); s.next(); // skips name String gender = s.next(); for (int i = startYear; i <= 2010; i += 10) { g.setColor(Color.BLACK); int rank = s.nextInt(); int x = ((decadeWidth / 10) * (i - startYear)); int y = 30 + (rank / 2); if (rank == 0) y = 560 - bannerHeight; g.drawString("" + rank, x, y); if (gender.equals("f")) g.setColor(Color.pink); else g.setColor(Color.blue); g.fillRect(x, y, decadeWidth / 2, 560 - bannerHeight - y); } s.close(); }
public void centerAxes(DrawingPanel panel) { xaxis.setLocation((panel.getYMax() + panel.getYMin()) / 2); yaxis.setLocation((panel.getXMax() + panel.getXMin()) / 2); }
/** * Draws the axes in the drawing panel. * * @param panel * @param g */ public void draw(DrawingPanel panel, Graphics g) { if (!visible) { return; } if (interiorColor != null) { g.setColor(interiorColor); int gw = panel.getLeftGutter() + panel.getRightGutter(); int gh = panel.getTopGutter() + panel.getBottomGutter(); g.fillRect( panel.getLeftGutter(), panel.getTopGutter(), panel.getWidth() - gw, panel.getHeight() - gh); g.setColor(Color.lightGray); g.drawRect( panel.getLeftGutter(), panel.getTopGutter(), panel.getWidth() - gw, panel.getHeight() - gh); } xaxis.draw(panel, g); yaxis.draw(panel, g); titleLine.setX((panel.getXMax() + panel.getXMin()) / 2); if (panel.getTopGutter() > 20) { titleLine.setY(panel.getYMax() + 5 / panel.getYPixPerUnit()); } else { titleLine.setY(panel.getYMax() - 25 / panel.getYPixPerUnit()); } titleLine.draw(panel, g); }