-2

I am trying to draw a car as a polygon in swing and move it left and right when clicking button

The problem is that I can't display the buttons on the screen when running the program and can't make them works

And I Don't know how implement the buttons inside of the user interface when using polygon

Here is my code:-

package java2d;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Java2D extends JFrame{

    int xValues[];
    int yValues[];
    private JButton Right, Left;

    public Java2D() {
        super( "Drawing lines, rectangles and ovals" );
        setSize( 500, 300 );
        setVisible( true );
    }

    public void paint( Graphics g ) {
    
        int xValues[] = { 40, 100, 130, 230, 260, 320, 320, 40 };
        int yValues[] = { 120, 120, 40, 40, 120, 120, 170, 170 };
        Polygon polygon1 = new Polygon( xValues, yValues, 8 );
        g.setColor(Color.blue);
        g.drawPolygon( polygon1 );
    
        Right = new JButton("Right");
        Left = new JButton("Left");
    
        Right.setSize(50, 50);
        Left.setSize(50, 50);
    
        Right.setLocation(100, 200);
        Left.setLocation(200, 200);
    
        g.add(Right);
        g.add(Left);
    
    }

    public void actionPerformed(ActionEvent event) {
        if(event.getActionCommand().equals("Right")){
            for (int i=0; i<xValues.length;i++) {
            xValues[i] = xValues[i] + 10;
            yValues[i] = yValues[i] + 10;
            }
        }
    
        if(event.getActionCommand().equals("Left")){
            for (int i=0; i<xValues.length;i++) {
            xValues[i] = xValues[i] + 10;
            yValues[i] = yValues[i] + 10;
            }
        }

    repaint();
    }


    public static void main(String[] args) {
        Java2D application = new Java2D();
        application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
    }
}

THANKS

Omarobaisi
  • 43
  • 5

1 Answers1

1

Many issues:

  1. Variable names should NOT start with an upper case character.

  2. Custom painting is done by overriding the paintComponent() method of a JPanel and then you add the panel to the frame. Read the section from the Swing tutorial on Custom Painting for more information and working examples to get you started. Start with the working code and modify it for your requirement.

  3. Never create Swing components in a painting method. The painting method is continually invoked when Swing determines the component needs to be repainted, so you don't want to keep creating new buttons.

  4. Typically to add buttons to the frame you would create a JPanel and add the buttons to the panel. Then you add the panel to the frame using frame.add(buttonsPanel, BorderLayout.PAGE_START). Then you would add your painting panel to the frame using frame.add(paintingPanel, BorderLayout.CENTER). Read the section from the Swing tutorial on Layout Managers for more information and examples.

  5. Instead of attempting to update the values of the Arrays used to create the Polygon you should be using the translate() method of the Polygon.

Keep a reference to the Swing tutorial handy as it contains examples for most Swing basics.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • 1
    @OP To add to this great answer is a small example of [drawing a polygon on a JPanel](https://stackoverflow.com/questions/15188238/about-drawing-a-polygon-in-java/15188351#15188351), oh and don't extend a `JFrame` unnecessarily. Also you have a `actionPerformed` method where did you implement `ActionListener` and register your `JButton` for the action? From the code shown the method will never be called when a button is clicked – David Kroukamp Dec 19 '20 at 18:24