How I can move up the line in Java Swing? - java

I create this class to draw the line and extend it as a component.
class MyCanvas extends JComponent {
public void paint(Graphics g)
{
g.drawLine(30, 20, 80, 90);
}
}
...
frame.getContentPane().add(new MyCanvas());
After drawing, when adding text fields to frame, it disappears.
frstVectorField = new JTextField("");
frstVectorField.setBounds(600, 50, 160, 30);
frame.add(frstVectorField);
frame.setLayout(null);.

The default content pane is a JPanel with BorderLayout layout manager. When you call method add(Component), of class JPanel, the Component gets placed in the CENTER area of the JPanel. Hence a subsequent call to method add(Component) will replace the Component that was previously added, since the CENTER area can only hold one Component.
I recommend the tutorial Creating a GUI With JFC/Swing

Related

JTextField appears in 2 locations when created inside paintComponent()

I am creating a minesweeper game, and what I want to do is to have a JTextField where the user inputs his name in order for his score to be saved in a file.
My problem is that when I create a JTextField and add it to my Jpanel it appears in 2 locations. Here is an image of what is happening
(https://i.imgur.com/Ao8dRo1.jpg)
This is my code over-simplified. I believe that I don't properly understand something about how the mechanism of the GUI works.
GUI.java
public class GUI extends JFrame {
//..
//some variables here
//...
public GUI() {
this.setTitle("Minesweeper Game");
this.setSize(WIDTH, HEIGHT);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setVisible(true);
this.setResizable(false);
this.setLayout(null);
//This method does not involve any drawing, it only places data in some arrays that I later use in paintComponent() to draw stuff accordingly to what the data is
setMinefield();
Board board = new Board();
this.setContentPane(board);
Click click = new Click();
this.addMouseListener(click);
}
public class Board extends JPanel {
public void paintComponent (Graphics g) {
//...
//Drawing tiles, smiley, counters
//...
//And now I draw the area for the JTextField, and I also create it and add it in the Jpanel
JTextField textField = new JTextField();
textField.setFont(new Font("Tahoma", Font.PLAIN, 35));
textField.setBounds(290, 80, 135, 40); //<-- This correctly places the textField where I want. The second textField seems to appear in the exact center of the X axis of my window
add(textField); //<-- Adding the textField to the Jpanel
} //End of paintComponent()
}//End of Board class
}//End of GUI class
Main.java
public class Main implements Runnable {
GUI gui = new GUI();
public static void main(String[] args) {
new Thread (new Main()).start();
}
#Override
public void run() {
while (true) {
gui.repaint();
}
}
}
I think the problem is that you have overridden paintComponent in your Board class. This method gets called every time the component needs to be drawn so a new text field will be added each time.
It would be better to add the text field in the constructor for your board class.
I use the command textField.setBounds(290, 80, 135, 40); to place it where I want but it doesn't work. Why could this happen?
Swing was designed to used with layout managers. The default layout manager for a JPanel is the FlowLayout. The FlowLayout will ignore the setBounds(...) statement and set the size/location of the text field based on the rules of the FlowLayout.
So don't attempt to use a null layout and don't use setBounds(). Instead let the layout manager do its job.
Also, you should be adding the components to the frame BEFORE you make the frame visible.
I would suggest your code should be something like:
JTextField textField = new JTextField(10);
JPanel top = new JPanel();
top.add( textField );
Board board = new Board();
add(top, BorderLayout.PAGE_START);
add(board, BorderLayout.CENTER);
setResizable( false );
pack();
setVisible( true );
The Board class should override the getPreferredSize() method of the Board to return your desired size so that the pack() method works properly.
The default layout manager for a JFrame is the BorderLayout. So, now the top part of the frame will contain the text field centered and the main part of the frame will contain your Board class. Read the section from the Swing tutorial on How to Use BorderLayout to understand how the above code works.
Also, the MouseListener should be added to the Board, not the JFrame.

How add a jtextfield to a jpanel that has paint component?

I'm making a game and I want to add a JTextField to a JPanel that has Paint Component. I repaint the JPanel every 16 milliseconds.
I add() the textfield to the panel but it show up only for a single frame when i click on it.
Then I tried to repaint() the textfield but now it is flashing.
public class Screen extends JPanel {
public Screen() {
JTextField txt = new JTextField();
txt.setBounds(10, 10, 300, 50);
this.add(txt);
}
#Override
public void paint(Graphics g) {
Graphics2D g2D = (Graphics2D) g;
g2D.setColor(Color.BLACK);
g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
g2D.setColor(Color.WHITE);
g2D.fillRect(0, 0, this.getWidth(), 20);
txt.repaint();
}
}
I want to show the textfield on the top of the panel
JTextField txt = new JTextField();
When you create a JTextField you should use code like:
JTextField txt = new JTextField(10);
Now the text field can calculate its own preferred size.
//txt.setBounds(10, 10, 300, 50);
Don't use setBounds() to give a component a size. Again each Swing component is responsible for determining its own preferred size. Then the layout manager will set the size/location of the component on the panel.
//public void paint(Graphics g) {
public void paintComponent(Graphics g)
{
super.paintComponent(g);
// add custom painting here
}
Don't override paint(). Custom painting is done by overriding the paintComponent() method. And the first statement in the method should be super.paintComopnent(g)
//g2D.setColor(Color.BLACK);
//g2D.fillRect(0, 0, this.getWidth(), this.getHeight());
Don't paint the background of the panel. That is the job of the panel and that is why you need to super.paintComponent(), to make sure the background is painted.
Then in the constructor of your JPanel class you simply use setBackground( Color.BLACK )
//txt.repaint();
Don't ever invoke repaint() on any component in a painting method.
Read the section from the Swing tutorial on Custom Painting for working examples to get you started. Use the demo code as the starting point for you program. Then you simply add a JTextField to the panel, so it will be a single line of code that is needed to display the text field.
It seems like you want to have a JTextField on a black panel. You don't need to set the colour of the panel every time in paint() method. Instead add this to the constructor:
public Screen() {
setOpaque(true);
setBackground(Color.BLACK);
//...
}
and remove paint() method.
Also, if you want to use absolute positioning with setBounds() method then you should set the layout to null setLayout(null) in constructor. If you use absolute positioning you will also need to specify the size of the panel explicitly. However, I would still suggest you use a layout manager that takes care of panel sizing as well.
See this post for more information about absolute positioning.

How to draw rectangle in JPanel from button click event

I am relatively new to Java Graphics. I want to draw 20 x 80 rectangle at (X,Y) coordinates in JPanel when user clicks a JButton. (where 'X' and 'Y' are coming from 2 JTextFields) .
I have read many questions and tutorial, but could not solve a problem. In some cases, I can draw rectangle but cannot draw new rectangle without emptying JPanel.
Here is my code :
public class CustomPanel extends JPanel {
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g); // first draw a clear/empty panel
g.draw3DRect(Integer.parseInt(x.getText()),Integer.parseInt(y.getText()), 20, 80, true);
// then draw using your custom logic.
}
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
//Code for frame
//Code for JTextfields x and y
JButton btnDraw = new JButton("Draw");
btnDraw.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
panel= new CustomPanel();
panel.setBounds(406, 59, 407, 297);
frame.getContentPane().add(panel);
frame.revalidate();
}
});
btnDraw.setBounds(286, 339, 89, 23);
frame.getContentPane().add(btnDraw);
}
You ActionListener code is wrong. You don't want to create a new panel, you want to add a Rectangle to the existing panel.
When you create the GUI you should add two panels to the GUI:
The first panel will be an empty panel that will do your custom painting. You would generally add this to the CENTER of the frame
The second panel will contain the "Draw" button. You would generally add this panel to the PAGE_END. Then when you click the Draw button you invoke a method like addRectangle(...) in your custom painting panel so the panel can paint the Rectangle.
Check out Custom Painting Approaches for the two common ways to do custom painting:
Keep a List of Object to paint and then in the paintComponent() method you iterate the LIst an paint each object.
Create a BufferedImage and then just paint the Rectangle onto the BufferedImage, then you can just paint the BufferedImage either in a JLabel or in your paintComponent() method.

I'm trying to draw shapes in a JPanel extending a JComponent and then putting the component inside the panel but it won't work

I'm trying to draw shapes in a JPanel extending a JComponent and then putting the component inside the panel but it won't work. I've got a JFrame (500, 500) and I need the right half of it to have things drown inside. I'm clearly doing something wrong though!
This is the code I've used:
public class Componente extends JComponent{
public void paint(Graphics g){
Graphics2D g2 = (Graphics2D)g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Shape linea = new Rectangle2D.Float(50, 50, 50, 50);
Shape cerchio = new Ellipse2D.Double(100, 100, 50, 50);
g2.setPaint(Color.BLUE);
g2.draw(linea);
g2.draw(cerchio);
}
}
public class PannelloDx extends JPanel{
PannelloDx(){
this.setBackground(Color.CYAN);
this.setSize(Esercitazione_Berni1703.finestra.getWidth()/2, Esercitazione_Berni1703.finestra.getHeight());
this.setLocation(Esercitazione_Berni1703.finestra.getWidth()/2, 0);
this.add(new Componente());
}
}
Now, the output shows the Cyan panel into the JFrame in the right half as it's supposed to. It won't show anything though!
By default a JPanel uses a FlowLayout and a FlowLayout respects the size of the component added to it. Your custom component has a preferred size of (0, 0) so there is nothing to paint.
You need to override the getPreferredSize() method of your custom panel to return the appropriate size.
Read the section from the Swing tutorial on Custom Painting for more information and examples. Check out the rest of the tutorial for Swing basics as well.

How do I call a graphical class in Java?

I've been coding a simulation for a traffic flow network in Java, and the class that is supposed to graphically model the network looks as follows:
public class Map extends JPanel {
BufferedImage truck1;
public Map() throws IOException{
truck1 = ImageIO.read(getClass().getResource("Truck.png"));
}
protected void paintcomponent (Graphics g) {
super.paintComponent(g);
g.drawImage(truck1, 50, 100, 300, 300, this);
}
}
In my main() function, I instance the object as follows at the very beginning of the function:
Frame F1 = new Frame();
F1.setLayout(new FlowLayout());
F1.setSize(500,500);
F1.setVisible(true);
Map map = new Map();
map.setOpaque(true);
F1.add(map);
F1.setVisible(true);
However, when I run the program, the only output is a blank window with a slightly darker grey small square exactly in the middle at the top of the window. I've added Truck.png to the project, and I can't see any reason why it shouldn't display properly. What am I doing wrong?
Components should be added to the frame before the frame is made visible.
You are using a FlowLayout for your frame. A FlowLayout respects the preferred size of all components. Your Map class doesn't have a preferred size so the size defaults to (0, 0) so there is nothing to paint. Override the getPreferredSize() method of the Map class to return the appropriate size for the component.

Categories

Resources