Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>A Graphic environment is complex thing. No longer do you have the safety of fixed character width and heights, now you need to start dealing with a wider range of environmental factors (such as the width and height of the area you have to paint in...)</p> <p>To get start I would suggest you take a look at</p> <ul> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/" rel="nofollow noreferrer">Creating a GUI with Swing</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/uiswing/painting/" rel="nofollow noreferrer">Performing custom painting</a></li> <li><a href="http://docs.oracle.com/javase/tutorial/2d/" rel="nofollow noreferrer">2D Graphics</a></li> </ul> <p>To cover the basics.</p> <p>In order to paint steps, we need to know (at least) three things.</p> <ol> <li>The number of steps to be painted...</li> <li>The width of each step</li> <li>The height of each step.</li> </ol> <p>This example calculates the width and height of the steps as a factor of the width and height of the container it's painting on.</p> <p><img src="https://i.stack.imgur.com/Enu0e.png" alt="enter image description here"><img src="https://i.stack.imgur.com/lvrTh.png" alt="enter image description here"><img src="https://i.stack.imgur.com/VDGqM.png" alt="enter image description here"></p> <pre><code>import java.awt.BorderLayout; import java.awt.Color; import java.awt.Dimension; import java.awt.EventQueue; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.UIManager; import javax.swing.UnsupportedLookAndFeelException; public class SimpleSteps { public static void main(String[] args) { new SimpleSteps(); } public SimpleSteps() { EventQueue.invokeLater(new Runnable() { @Override public void run() { try { UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) { ex.printStackTrace(); } JFrame frame = new JFrame("Testing"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add(new TestPane()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } }); } public class TestPane extends JPanel { private JTextField steps; private StairPane stairPane; public TestPane() { setLayout(new BorderLayout()); JPanel options = new JPanel(); steps = new JTextField(10); JButton create = new JButton("Create"); stairPane = new StairPane(); options.add(new JLabel("Step count: ")); options.add(steps); options.add(create); add(stairPane); add(options, BorderLayout.SOUTH); create.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String value = steps.getText(); try { int stepCount = Integer.parseInt(value); stairPane.setStepCount(stepCount); } catch (NumberFormatException exp) { JOptionPane.showMessageDialog(TestPane.this, value + " is not a valid step count", "Error", JOptionPane.ERROR_MESSAGE); } } }); } } public class StairPane extends JPanel { private int stepCount = 0; public StairPane() { } @Override public Dimension getPreferredSize() { return new Dimension(200, 200); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (stepCount &gt; 0) { Graphics g2d = (Graphics2D) g.create(); int stepHeight = getHeight() / stepCount; int stepWidth = getWidth() / stepCount; for (int step = stepCount; step != 0; step--) { int width = stepWidth * step; int y = (stepHeight * step) - stepHeight; g2d.fillRect(0, y, width, stepHeight); } g2d.dispose(); } } private void setStepCount(int stepCount) { this.stepCount = stepCount; repaint(); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload