Reputation: 1
how can I have the "Drawing lines, rectangles and ovals" appear on the botton left side? im currently having it on the top left, and I dont know how to do it ? please help
I also tried and it didnt work something like
JLabel label = new JLabel("Text Label", JLabel.LEFT);
label.setVerticalAlignment(JLabel.BOTTOM);
import java.awt.*;
import javax.swing.*;
public class LinesRectsOvalsJPanel extends JPanel {
public void paintComponent( Graphics g ) {
super.paintComponent( g );
this.setBackground( Color.WHITE );
// x y width height
g.setColor(Color.BLACK);
g.drawLine(5,10,5,30);
g.setColor(Color.BLUE);
g.drawLine(18,70,127,24);
g.setColor(Color.RED);
g.drawLine(25,45,100,38);
g.setColor(Color.YELLOW);
g.drawOval(23,25,23,55);
g.setColor(Color.BLACK);
g.drawOval(15,14,40,78);
g.setColor(Color.CYAN);
g.drawOval(180,102,5,90);
g.setColor(Color.RED);
g.drawOval(21,20,89,11);
g.setColor(Color.BLUE);
g.drawOval(35,87,39,27);
g.setColor(Color.YELLOW);
g.fillRect(87,5,5,60);
g.setColor(Color.GREEN);
g.fillRect(105,15,15,85);
g.setColor(Color.CYAN);
g.fillRect(14,45,76,86);
g.setColor(Color.RED);
g.fillRect(70,79,65,86);
g.setColor(Color.BLUE);
g.fillRect(90,108,5,8);
}
}
import java.awt.*;
import javax.swing.*;
public class LinesRectsOvals {
public static void main( String args[] ) {
JFrame frame =
new JFrame( "Drawing lines, rectangles and ovals");
LinesRectsOvalsJPanel linesRectsOvalsJPanel =
new LinesRectsOvalsJPanel();
linesRectsOvalsJPanel.setBackground( Color.WHITE );
frame.add( linesRectsOvalsJPanel ); // add panel to frame
frame.setSize( 300, 300 ); // set frame size
frame.setVisible( true );
}
}
Upvotes: 0
Views: 2153
Reputation: 36611
I will first go over your code to explain what is happening:
JLabel label = new JLabel("Text Label", JLabel.LEFT);
label.setVerticalAlignment(JLabel.BOTTOM);
The setVerticalAlignment
only affects how the label text is positioned inside the label, and not how the label is positioned inside the parent container. See the javadoc of that method
Sets the alignment of the label's contents along the Y axis.
The fact that your "Drawing lines..." string appears at the top is because of the following code
JFrame frame = new JFrame( "Drawing lines, rectangles and ovals");
This creates a new frame where you specified the title of the frame. Just like for all other programs, the title is shown at the top (as well as the maximize, close and minimize buttons).
If you want to have text at the bottom, you can simply use the appropriate layout manager.
JFrame frame = new JFrame( "Whatever title you want" );
JPanel contentPane = new JPanel( new BorderLayout() );
LinesRectsOvalsJPanel linesRectsOvalsJPanel =
new LinesRectsOvalsJPanel();
contentPane.add( linesRectsOvalsJPanel, BorderLayout.CENTER );
Component statusBar = ...;//probably a JLabel is sufficient
contentPane.add( statusBar, BorderLayout.SOUTH );
More links about layout managers and examples can be found on the Swing info page.
Upvotes: 0
Reputation: 347314
index is right in principle. In practice, you will want to know the height and width of the container you are painting to.
Try this
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
int width = getWidth() - 1;
int height = getHeight() - 1;
// x y width height
g.setColor(Color.BLACK);
g.drawLine(5, height - 30 - 10, 5, 30);
g.setColor(Color.BLUE);
g.drawLine(18, height - 24 - 70, 127, 24);
g.setColor(Color.RED);
g.drawLine(25, height - 38 - 45, 100, 38);
g.setColor(Color.YELLOW);
g.drawOval(23, height - 55 - 25, 23, 55);
g.setColor(Color.BLACK);
g.drawOval(15, height - 78 - 14, 40, 78);
g.setColor(Color.CYAN);
g.drawOval(180, height - 90 - 102, 5, 90);
g.setColor(Color.RED);
g.drawOval(21, height - 11 - 20, 89, 11);
g.setColor(Color.BLUE);
g.drawOval(35, height - 27 - 87, 39, 27);
g.setColor(Color.YELLOW);
g.fillRect(87, height - 50 - 5, 5, 60);
g.setColor(Color.GREEN);
g.fillRect(105, height - 85 - 15, 15, 85);
g.setColor(Color.CYAN);
g.fillRect(14, height - 86 - 45, 76, 86);
g.setColor(Color.RED);
g.fillRect(70, height - 86 - 79, 65, 86);
g.setColor(Color.BLUE);
g.fillRect(90, height - 8 - 108, 5, 8);
}
The important part here is
int width = getWidth() - 1;
int height = getHeight() - 1;
Upvotes: 0
Reputation: 11
The x and y values you are using to draw the lines are relative to the top left of the screen. To refer to the bottom corner use low x and large y values. I hope this helps.
Upvotes: 1