I'm using Java Swing and I need to display a matrix with square brackets (normal square bracket like the one we use in math that spans more than one line), the matrix size is not fixed, it depends on the input.
Here is the code I'm using to display the matrix:
public static void printMatrix(String[][] matrix) {
String output = "";
for (int x = 0; x < matrix.length; x++) {
output += Arrays.toString(matrix[x]) + "\n";
}
JOptionPane.showMessageDialog(null, output, "Matrix",
JOptionPane.INFORMATION_MESSAGE);
}
The output:
But I need to have one big connected square bracket as follows:
So I'm searching on how to do this and I found this link https://docs.oracle.com/javase/tutorial/uiswing/components/border.html but it doesn't contain the brackets that I need and also found this https://team.mumie.net/mumie/mathletfactory_lib_apidocs/net/mumie/mathletfactory/display/noc/matrix/MatrixBorder.html#MatrixBorder%28java.awt.Component,%20int%29 but I didn't find any examples on how to use it.
Based on nIcE cOw's answer on one of my above comments, you need to create your own CustomBorder class that extends AbstractBorder and override its paintBorder() method to draw each part of the brackets.
In this case I divided this task in 3 parts, the top / bottom / left & right part of both brackets.
The internalGap variable is the space that should be between the content and the border
Here are some screenshots of how the output looks like:
With 2, 6 and 10 elements
The code that produces the above outputs is:
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Component;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import java.awt.Insets;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.AbstractBorder;
public class EquationMatrixBorder {
private JPanel pane;
private CustomBorder customBorder;
private static final int ROWS_AND_COLS = 1;
private void displayGUI() {
JFrame frame = new JFrame("Custom Border Example");
customBorder = new CustomBorder(Color.RED, 15, 10);
pane = new JPanel();
pane.setLayout(new GridLayout(ROWS_AND_COLS, ROWS_AND_COLS, 15, 15));
//Used to fill the grid, not relevant to question
Random random = new Random();
for (int i = 0; i < ROWS_AND_COLS; i++) {
for (int j = 0; j < ROWS_AND_COLS; j++) {
int r = 0;
if (j % 2 == 0) {
r = random.nextInt(2);
} else {
r = random.nextInt(2) - 1;
}
pane.add(new JLabel(String.valueOf(r)));
}
}
pane.setBorder(customBorder);
frame.add(pane);
frame.pack();
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
Runnable runnable = new Runnable() {
#Override
public void run() {
new EquationMatrixBorder().displayGUI();
}
};
EventQueue.invokeLater(runnable);
}
}
class CustomBorder extends AbstractBorder {
private Color color;
private int gap;
private int bracketsTopAndBottom = 10;
private int internalGap;
public CustomBorder(Color color, int gap, int internalGap) {
this.color = color;
this.gap = gap;
this.internalGap = internalGap;
}
#Override
public void paintBorder(Component c, Graphics g, int x, int y, int width, int height) {
super.paintBorder(c, g, x, y, width, height);
Graphics2D g2d = null;
if (g instanceof Graphics2D) {
g2d = (Graphics2D) g;
g2d.setColor(color);
g2d.setStroke(new BasicStroke(3));
//top part of brackets
g2d.drawLine(x + gap, y + gap, x + gap + bracketsTopAndBottom, (y + gap));
g2d.drawLine(width - x - gap - bracketsTopAndBottom, y + gap, width - gap - x, (y + gap));
//bottom part of brackets
g2d.drawLine(x + gap, height - gap, x + gap + bracketsTopAndBottom, height - gap);
g2d.drawLine(width - x - gap - bracketsTopAndBottom, height - gap, width - gap - x, height - gap);
//left and right part of brackets
g2d.drawLine(x + gap, y + gap, x + gap, height - gap);
g2d.drawLine(width - x - gap, y + gap, width - x - gap, height - gap);
}
}
#Override
public Insets getBorderInsets(Component c) {
return getBorderInsets(c, new Insets(gap, gap, gap, gap));
}
#Override
public Insets getBorderInsets(Component c, Insets insets) {
insets.left = insets.top = insets.right = insets.bottom = gap + internalGap;
return insets;
}
}
Note
I haven't done rows and cols numbers shown in desired output of OP, I'm leaving that out as this question is only related to the square brackets
Related
I have to draw n + 1 amount of circles horizontally and vertically in a GUI. Which I have successfully done as shown below. With this, a 2D array of strings will be printed between them, centralised.
How it currently stands
Now I want to draw numbers in a "square" of the dots.
How I want the final result
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
canvas.drawCircle( (j + 1) * 125, (i + 1) * 125, 15, Color.white);
}
}
for (int r = 0; r < size; r++) {
for (int c = 0; c < size; c++) {
canvas.drawString(events[r][c], (r + 1) * 150, (c + 1) * 150, Color.green );
}
}
Width in this case is 4, so basically (n-1) dots/circles in the picture.
Size is 3 which is just the length of the 2d array, as there are 4 circles in this case there will be 3 numbers between each one
Events is the 2D array with the data containing the numbers
The drawCircle method's signature is
(x, y, radius, color)
The drawString method's signature is
(text, x, y color)
I believe part of the problem is also the drawing of circles. Basically I think it has to do with the rubbish formula I have for determining the x, y coords for both circles and the text. Any help is appreciated, thank you.
Provided is some code which I believe does what you want. Some of the constants may be tweaked to fit your final requirements. I used a 2D array of numbers and converted to strings during paint. It also allows for the following:
Changing the ball diameter or number of balls will still provide a
correct graph (although it will change its location within the
panel).
The String numbers track along with the size of the balls
Anti-aliasing was turned on to smooth out the graphics.
FontMetrics was used to fine tune the location of the numbers.
One final note: Because this is not resource intensive, the coordinates are calculated within the paintComponent method. A more optimum solution would be to adopt the Flyweight design pattern and precalculate as much as possible before entering paintComponent.
import java.awt.*;
import javax.swing.*;
public class SwingMain extends JPanel {
final static int WIDTH = 700;
final static int HEIGHT = 700;
final static int SEPARATION = 100;
final static int DIAMETER = 25;
final static int NBALLS = 4;
final static int XSTART = WIDTH / (NBALLS + 2);
final static int YSTART = HEIGHT / (NBALLS + 2);
JFrame frame = new JFrame();
int[][] numbers = new int[NBALLS - 1][NBALLS - 1];
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new SwingMain().start());
}
public void start() {
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setPreferredSize(new Dimension(WIDTH, HEIGHT));
frame.add(this);
setBackground(Color.RED);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
// populate numbers in 2D array.
for (int r = 0; r < NBALLS - 1; r++) {
for (int c = 0; c < NBALLS - 1; c++) {
numbers[r][c] = r * (NBALLS - 1) + c + 1;
}
}
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
// Allow smoothing of the graphics.
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setColor(Color.white);
// Iterating by the number of balls is more consistent than
// by x and y due to round of errors.
int y = YSTART;
for (int r = 0; r < NBALLS; r++) {
int x = XSTART;
for (int c = 0; c < NBALLS; c++) {
g2d.fillOval(x, y, DIAMETER, DIAMETER);
x += SEPARATION;
}
y += SEPARATION;
}
// This is the same as above except that the start begins
// halfway between the first row and column
// have the size of the font track with the diameter of the balls
g2d.setFont(new Font("ARIAL", Font.BOLD, DIAMETER));
FontMetrics fm = g2d.getFontMetrics();
y = YSTART + SEPARATION / 2;
for (int r = 0; r < NBALLS - 1; r++) {
int x = XSTART + SEPARATION / 2;
for (int c = 0; c < NBALLS - 1; c++) {
String number = Integer.toString(numbers[r][c]);
// Do some final position adjustment with the font metrics to
// center the number
int strWidth = fm.stringWidth(number);
int strHeight = fm.getAscent();
g2d.drawString(number,
x - strWidth / 2 + DIAMETER / 2,
y + strHeight);
x += SEPARATION;
}
y += SEPARATION;
}
}
}
You could store the coordinates of the circles in a 2D array as well, and use that to find the location of the strings. One thing to note is that the drawCircle method for some reason wont draw the circle with the given center (the coordinates you give will be the top left corner actually).
Point[][] circleCoords = new Point[width][width]; //suppose Point class has x and y coords
for (int i = 0; i < width; i++) {
for (int j = 0; j < width; j++) {
//the -15 actually centers the circle to the coordianates
circleCoords[i][j] = new Point((j + 1) * 125 - 15, (i + 1) * 125 -15);
canvas.drawCircle(circleCoords[i][j].x , circleCoords[i][j].y, 15, Color.white);
}
}
for (int r = 0; r < width-1; r++) {
for (int c = 0; c < width-1; c++) {
//calculate coords from circleCoords array: halfway between them
int xCoord = (circleCoords[r][c].x + circleCoords[r+1][c].x)/2;
int yCoord = (circleCoords[r][c].y + circleCoords[r][c+1].y)/2;
//wont be out of bounds, becouse this runs to width-1
canvas.drawString(events[r][c], xCoord, yCoord, Color.green );
}
}
This still wont actually be perfectly centered, because the drawString will use the coordinates for top left point as well, and not centerpoint. Maybe I miscalculated something, but this should give you the idea: instead of calculating the coordinates independently, re-use the circle coordinates.
Consider a class that represents a single square. The value is represented by a JLabel which is placed at the bottom using straight-forward layout manager (BorderLayout in this example). You can change or manipulate the layout manager to change to position of the JLabel.
class Square extends JPanel{
private static int WIDTH = 100, HEIGHT = 100, DAIMETER = 30, GAP = 5;
private final JLabel label;
Square(int value) {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new BorderLayout());
setBackground(Color.red);
setOpaque(true);
label = new JLabel(String.valueOf(value), JLabel.RIGHT);
label.setForeground (Color.white);
Border margin = new EmptyBorder(GAP,GAP,GAP,2*GAP); //top left bottom right
label.setBorder(margin);
add(label, BorderLayout.PAGE_END);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.fillOval(WIDTH/2 - DAIMETER/2, HEIGHT/2-DAIMETER/2, DAIMETER, DAIMETER);
}
}
Now add 16 instances of this Square to panel representing a board. Its layout manager is set to GridLayout:
class Board extends JPanel{
private static int ROWS = 4, COLS = 4;
Board(){
setLayout(new GridLayout(ROWS, COLS));
for(int index = 0; index < ROWS * COLS; index ++){
add(new Square(index)); //index is used as value for demonstration purposes
}
}
}
Putting it all together (this is a one-file mcve. Copy paste the entire code into SwingMain.java and run )
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
public class SwingMain {
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocation(400,250);
frame.add(new Board());
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
}
}
class Board extends JPanel{
private static int ROWS = 4, COLS = 4;
Board(){
setLayout(new GridLayout(ROWS, COLS));
for(int index = 0; index < ROWS * COLS; index ++){
add(new Square(index)); //index is used as value for demonstration purposes
}
}
}
class Square extends JPanel{
private static int WIDTH = 100, HEIGHT = 100, DAIMETER = 30, GAP = 5;
private final JLabel label;
Square(int value) {
setPreferredSize(new Dimension(WIDTH, HEIGHT));
setLayout(new BorderLayout());
setBackground(Color.red);
setOpaque(true);
label = new JLabel(String.valueOf(value), JLabel.RIGHT);
label.setForeground (Color.white);
Border margin = new EmptyBorder(GAP,GAP,GAP,2*GAP); //top left bottom right
label.setBorder(margin);
add(label, BorderLayout.PAGE_END);
}
#Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(Color.white);
g2d.fillOval(WIDTH/2 - DAIMETER/2, HEIGHT/2-DAIMETER/2, DAIMETER, DAIMETER);
}
}
having a 2d array of characters that is supposed to represent a map
char test[][]={{'A','B','C'},{'D','E','F'},{'G','H','I'};
how would I create a 3x3 grid using the java.awt library that has each character at the center of each cell?
The code below gives me a a grid but I am still not sure how to put each character at the center of each cell.
int sideLength=115;
int distance=sideLength*3;
int i=0;
int j=0;
for ( int x=90; x<=distance; x+=sideLength )
{
for( int y=30; y<=distance; y+=sideLength )
{
g.drawRect(x,y,sideLength, sideLength);
g.drawString(Character.toString(test[i][j]), sideLength/2, sideLength/2);
}
i++;
j++;
}
This code gives me this output
Nothing but smoke and mirrors...
Okay, JLabels and Borders
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;
import javax.swing.border.EmptyBorder;
import javax.swing.border.MatteBorder;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
public TestPane() {
setBorder(new EmptyBorder(10, 10, 10, 10));
setLayout(new GridLayout(0, 3));
String test[][]={{"A","B","C"},{"D","E","F"},{"G","H","I"}};
Border paddingBorder = new EmptyBorder(50, 50, 50, 50);
for (int row = 0; row < 3; row++) {
for (int col = 0; col < 3; col++) {
JLabel label = new JLabel(test[row][col]);
label.setVerticalAlignment(JLabel.CENTER);
label.setHorizontalAlignment(JLabel.CENTER);
Border edgeBorder = new MatteBorder(
1,
1,
row == 2 ? 1 : 0,
col == 2 ? 1 : 0,
Color.BLACK);
label.setBorder(new CompoundBorder(edgeBorder, paddingBorder));
add(label);
}
}
}
}
}
There's lots of variations on this subject, but it represents the simplest approach
Custom Paint Route...
If, you want to follow a custom paint route, then you're going to need to have a better understanding of how fonts actually work.
I would recommend starting with Working with Text APIs to gain a basic understanding.
Next, you're incrementing your i and j properties correctly. j needs to be reset before you render each column and should increment for each row.
Personally, I'd do it the other way round, where you used the index for each row/column to get the value from the array and then calculate the x/y position based on the current row/column index, but that's just me.
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Test {
public static void main(String[] args) {
new Test();
}
public Test() {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
JFrame frame = new JFrame();
frame.add(new TestPane());
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
});
}
public class TestPane extends JPanel {
String test[][] = {{"A", "B", "C"}, {"D", "E", "F"}, {"G", "H", "I"}};
public TestPane() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(90 + (115 * 4), 30 + (115 * 4));
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
int sideLength = 115;
int distance = sideLength * 3;
int i = 0;
for (int x = 90; x <= distance; x += sideLength) {
int j = 0;
for (int y = 30; y <= distance; y += sideLength) {
g.drawRect(x, y, sideLength, sideLength);
FontMetrics fm = g.getFontMetrics();
int yPos = y + ((sideLength - fm.getHeight()) / 2) + fm.getAscent();
int xPos = x + ((sideLength - fm.stringWidth(test[i][j])) / 2);
g.drawString(test[i][j], xPos, yPos);
j++;
}
i++;
}
}
}
}
You're close, but I would refactor your loops slightly. Instead of iterating on distance, iterate on the index in the array. Use the array index to then derive the positions.
char[][] test = {{'A','B','C'}, {'D','E','F'}, {'G','H','I'}};
int sideLength=115;
for (int i = 0; i < test.length; i++) {
int x = 90 + sideLength * i;
for (int j = 0; j < test[j].length; j++) {
int y = 30 + sideLength * j;
g.drawRect(x, y, sideLength, sideLength);
g.drawString(
Character.toString(test[i][j]),
x + sideLength / 2,
y + sideLength / 2);
}
}
as you see, 5x5 window is contiguous to 0,0 coordinates. I want to move them like 10,10. How can I do it ?
I think the problem is in my BorderLayout. North and East is freee. How can i fill them with 10,10 dimension
import java.awt.*;
import javax.swing.*;
public class Main {
public static void main(String[] args) {
SOS game = new SOS(5);
// JPanel for sos template
SOSCanvas window = new SOSCanvas(game);
// JPanel which includes info panel, time and exit panel
SOSGUIPanel info = new SOSGUIPanel(window, "Cihangir", "Mercan");
JFrame jf = new JFrame();
jf.setTitle( "SOS game");
jf.setSize(340, 450);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.setLocationRelativeTo(null);
jf.setLayout( new BorderLayout() );
jf.add(window, BorderLayout.CENTER );
jf.add(info, BorderLayout.SOUTH );
}
}
The grid is provided with this class;
import java.awt.*;
import javax.swing.*;
public class SOSCanvas extends JPanel {
// PROPERTIES
protected SOS game;
// CONSTRUCTORS
public SOSCanvas( SOS game )
{
this.game= game;
}
#Override
public void paintComponent(Graphics g)
{
super.paintComponent(g);
int dimension = game.getDimension();
g.drawRect (0, 0, 300, 300);
// drawing horizontal lines
for ( int i = 1; i < dimension; i++ )
{
g.drawLine(0, i * (300 / dimension), 300, i * (300 / dimension) );
}
// drawing vertical lines
for ( int i = 1; i < dimension; i++ )
{
g.drawLine( i * (300 / dimension), 0, i * (300 / dimension), 300);
}
}
}
The first thing you need to do, is stop relying on magic numbers, you need to know the current size of the component.
Instead of using g.drawRect (0, 0, 300, 300);, you need to get the component's current width and height. From this you can make decisions about how you might handle the differences in your expectations and the actual width and height of the component.
For example, the following is a scalable grid, which can grow and shrink based on the available space, but will always remain square. It will always attempt to center itself within the available space as well...
public class SOSCanvas extends JPanel {
public SOSCanvas() {
}
#Override
public Dimension getPreferredSize() {
return new Dimension(300, 300);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int dimension = 5;//game.getDimension();
int width = getWidth() - 1;
int height = getHeight() - 1;
int cellSize = Math.min(width, height);
int xOffset = (width - cellSize) / 2;
int yOffset = (height - cellSize) / 2;
g.drawRect(xOffset, yOffset, cellSize, cellSize);
// drawing horizontal lines
for (int i = 1; i < dimension; i++) {
g.drawLine(
xOffset,
yOffset + (i * (cellSize / dimension)),
xOffset + cellSize,
yOffset + (i * (cellSize / dimension)));
}
// drawing vertical lines
for (int i = 1; i < dimension; i++) {
g.drawLine(
xOffset + (i * (cellSize / dimension)),
yOffset,
xOffset + (i * (cellSize / dimension)),
yOffset + cellSize);
}
}
}
Even if you wanted a static grid of (lets say) 300x300, the x offset you would need to center the grid horizontally would be calculated using something like xOffset = (getWidth() - 300) / 2, then all you horizontal drawing would need to start from this offset...
I am new to programming and this is my first time posting to this site. I am currently working on an assignment which will allow a user to input a number between 1 and 20, and then will create a brick wall based upon the number of brick layers the user entered into the JTextField. I was able to create one row, but I don't really understand nesting statements, and all of the examples I've looked at on the web just confuse me more, so I was wondering if someone could possibly help me to better understand nesting for-loops.
//Package List
import java.awt.*;
import java.applet.*;
import javax.swing.*;
import java.util.*;
import java.awt.event.*;
public class Wall extends JApplet implements KeyListener {
//Component declaration
JLabel directions;
JTextField input;
//Variable declaration
int startX = 50;
int startY = 650;
int width = 50;
int height = 20;
int spacing = 2;
//Method declaration
public void init()
{
getContentPane().setBackground(new Color (128, 128, 128));//Changes backround of JApplet to black
//Set JTextField and JLabel
setLayout (new FlowLayout( ));
directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
input = new JTextField ( 10 );
add (directions );
add (input);
//Key listener
addKeyListener( this );
setFocusable( true );
}
//Method declaration
public void paint(Graphics g)
{
super.paint (g);
for (int col=1; col<= 8; col++)
{
// for (int row; row<=20; row++)
{ g.setColor (Color.RED);
g.fillRect (startX, startY, 50, 20);
startX = startX + spacing + width;
}
}
}
//Key event methods
public void keyReleased( KeyEvent ke ){}
public void keyTyped (KeyEvent ke ) {}
public void keyPressed ( KeyEvent ke )
{
int key = ke.getKeyCode ( );
if (key == KeyEvent.VK_ENTER)
{
}
}
You need to calculate the x/y position of the brick based on the current row/col. Now, you could simply initialise these values at the start of each loop and increment as required, or you could use a little maths...
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import javax.swing.JApplet;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class AnotherBrickInTheWall extends JApplet {
//Component declaration
JLabel directions;
JTextField input;
//Variable declaration
int startX = 0;
int startY = 50;
int width = 50;
int height = 20;
int spacing = 2;
//Method declaration
public void init() {
getContentPane().setBackground(new Color(128, 128, 128));//Changes backround of JApplet to black
//Set JTextField and JLabel
setLayout(new FlowLayout());
directions = new JLabel("Enter in any number between 1 and 20 and then press Enter on your keyboard.");
input = new JTextField(10);
add(directions);
add(input);
}
//Method declaration
#Override
public void paint(Graphics g) {
super.paint(g);
for (int row = 0; row < 8; row++) {
int y = startY + (row * (height + spacing));
for (int col = 0; col < 8; col++) {
int x = startX + (col * (width + spacing));
System.out.println(x + "x" + y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}
}
}
Now, you should never paint on a component which has components added to, this will cause the painting to appear over the top of the components and end up with all sorts of painting issues.
Instead, create a custom component, extending from something like JPanel and override it's paintComponent method instead and place your custom painting here.
You could then add your controls another JPanel and using a BorderLayout on your applet, add these two panels, the fields in the NORTH position and wall in the CENTER position
Updated
If you wanted to get "really" fancy, you could even adjust the x position based on the current row, for example...
int xOffset = 0;
for (int row = 0; row < 8; row++) {
int y = startY + (row * (height + spacing));
if (row % 2 == 0) {
xOffset = width / 2;
} else {
xOffset = 0;
}
for (int col = 0; col < 8; col++) {
int x = xOffset + (startX + (col * (width + spacing)));
System.out.println(x + "x" + y);
g.setColor(Color.RED);
g.fillRect(x, y, width, height);
}
}
Also, JTextField is has an ActionListener which is triggered when the field is actioned, typically by the user pressing Enter. This means that you shouldn't need the KeyListener.
See How to Use Text Fields for more details.
int x=num //1-20
int step=num/8;
int last_row=num%8;
for(int j=1;j<=step;j++){
int nos=8;
if(j == step && last_step ! = 0)
nos = last_step;
for (int col=1; col<= nos; col++)
{
g.setColor (Color.RED);
g.fillRect (startX, startY, 50, 20);
startX = startX + spacing + width;
startY = startY*j;
}
}
Try this.
I'm a beginner whit Java experience so I hope you can help me.
I want to read the number of steps from a JTextfield. But how can you make the stairs variable?
Graphics g = textP.getGraphics();
int x = 5;
int y = 20;
int width = 10;
int height = 10;
For loop 1 - whil give a drawRect of 6 pieces
for (int i = 0; i < 6; i++) {
g.drawRect(x, y * i, width, height);
}
For loop 2 - whil give a drawRect of 5 pieces
for (int i = 1; i < 6; i++) {
g.drawRect(x + 15, y * i, width, height);
}
For loop 3 - whil give a drawRect of 4 pieces
for (int i = 2; i < 6; i++) {
g.drawRect(x + 30, y * i, width, height);
}
For loop 4 - whil give a drawRect of 3 pieces
for (int i = 3; i < 6; i++) {
g.drawRect(x + 45, y * i, width, height);
}
For loop 5 - whil give a drawRect of 2 pieces
for (int i = 4; i < 6; i++) {
g.drawRect(x + 60, y * i, width, height);
}
For loop 6 - whil give a drawRect of 1 pieces
for (int i = 5; i < 6; i++) {
g.drawRect(x + 75, y * i, width, height);
}
Output (must by an rect ipv *):
*
**
***
****
*****
******
You can do it this way.
JTextField stairs = new JTextField("Enter no. of Stairs");
String noOfStairsStr = stairs.getText();
int noOfStairs = Integer.parseInt(noOfStairsStr);
...
for (int i = 0; i < noOfStairs; i++) { // Use that in the for loop.
Are you expecting something like this
for (int i = 0,k=15; i < 6; i++) {
g.drawRect( ( x+(i*k) ), y * i, width, height);
}
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...)
To get start I would suggest you take a look at
Creating a GUI with Swing
Performing custom painting
2D Graphics
To cover the basics.
In order to paint steps, we need to know (at least) three things.
The number of steps to be painted...
The width of each step
The height of each step.
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.
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 > 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();
}
}
}