1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
zlopas [31]
2 years ago
6

In java

Computers and Technology
1 answer:
Kisachek [45]2 years ago
7 0

Answer:

import javax.swing.JPanel;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

import javax.swing.JLabel;  

import javax.swing.JTextField;

import javax.swing.border.Border;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import javax.swing.BorderFactory;

import javax.swing.JButton;

import java.awt.BorderLayout;

import java.awt.Color;

import java.awt.FlowLayout;

import java.awt.Graphics;

import java.awt.Rectangle;

public class DrawingPanel{

 

private static final int width = 800;

private static final int heigth = 500;

private static JFrame mainFrame;

private static JPanel mainPanel, inputPanel, drawPanel;

private static JLabel xLabel, yLabel;

private static JTextField xField, yField;

private static JButton drawButton;

 

public static void main(String[] args)

{

mainFrame = new JFrame("Draw Path");

mainPanel = new JPanel(new BorderLayout());

 

inputPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

xLabel = new JLabel("X Coordinate: ");

xField = new JTextField(5);

yLabel = new JLabel("Y Coordinate: ");

yField = new JTextField(5);

drawButton = new JButton("Draw");

inputPanel.add(xLabel);

inputPanel.add(xField);

inputPanel.add(yLabel);

inputPanel.add(yField);

inputPanel.add(drawButton);

 

Border border = BorderFactory.createLineBorder(Color.black);

drawPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));

drawPanel.setBorder(border);

 

mainPanel.add(inputPanel, BorderLayout.NORTH);

mainPanel.add(drawPanel, BorderLayout.CENTER);

 

mainFrame.add(mainPanel);

mainFrame.setSize(PANEL_WIDTH, PANEL_HEIGHT);

mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

mainFrame.setLocationRelativeTo(null);

mainFrame.setResizable(false);

mainFrame.setVisible(true);

 

// action listener for draw button

drawButton.addActionListener(new ActionListener() {

(use the at sign here)Override

public void actionPerformed(ActionEvent e) {

if(xField.getText().equals("") || yField.getText().equals(""))

JOptionPane.showMessageDialog(null, "Please enter the x and y coordinates!");

else

{

int x2 = Integer.parseInt(xField.getText().trim());

int y2 = Integer.parseInt(yField.getText().trim());

if(x2 > WIDTH || y2 > HEIGHT)

JOptionPane.showMessageDialog(null, "x and y coordinates should be less than "

+ width + " and " + height + " respectively!");

else

drawPath(x2, y2);

}

}

});

}

 

public static void drawPath(int x2, int y2)

{

int x1 = (int)drawPanel.getBounds().getCenterX();

int y1 = (int)drawPanel.getBounds().getCenterY();

 

Graphics g = drawPanel.getGraphics();

Rectangle r = drawPanel.getBounds();

g.setColor(drawPanel.getBackground());

g.fillRect(r.x, r.y, r.width, r.height);

 

g.setColor(Color. blue);

g.drawLine(x1, y1, x2, y2);

}

}

Explanation:

The class DrawingPanel is a Java class that gets the user input for the coordinate of a path with predetermined heigth and width (defined in the class). The methods of the class are; getGraphics(), getBounds(), getBackground(), setColor() and fillRect(). The result is a rectangular path GUI on the screen.

You might be interested in
Need help on this it’s the last one I need
BaLLatris [955]
Design a ringtone like it says
8 0
3 years ago
A bulb has a resistance of 15Ω and is rated at 3A. What is the maximum voltage that can be applied across the bulb?
Anastasy [175]

Answer:Wheres the option choice??????????????????????

Explanation:

7 0
2 years ago
--------------communication involves one speaker andaudience.
dalvyx [7]

Answer:

Person-to-Group

Explanation:

Person-to-Group communication involves one speaker and audience.

6 0
3 years ago
In which area is composing for games different from composing for movies?
kondaur [170]

Answer: royalties earned

Explanation:

In areas such as the looping scores, temp tracks and adaptive scores, there are similarities when composing for games and when composing for movies.

The area where there is difference when composing for games and composing for movies is the royalties earned. The interest that is earned on royalties for both cases are different.

8 0
3 years ago
How did these social media platforms affect or change the way you communicate with other people?
amid [387]

Answer:

The importance of social media in communication is a constant topic of discussion. Online communication has brought information to people and audiences that previously could not be reached. It has increased awareness among people about what is happening in other parts of the world.

Social media networks allow us the opportunity to share opinions with a far wider audience. Another big change that has occurred is that there is now no filter on the way we speak. In the past, unless you spoke to people directly, you had no way to get your message across regardless of your freedom of speech.

Explanation:

<h2>be happy</h2>
8 0
2 years ago
Other questions:
  • Margins can be modified in the page layout or by using__.
    15·1 answer
  • Yellow and blue light are projected on a white screen. what color will the screen appear to be?
    6·2 answers
  • How neural networks impact our life??
    7·1 answer
  • How do you change the text style on brainly mobile ?
    12·1 answer
  • William wants to create a document using a DTP<br> what time saving techniques can he use?
    9·1 answer
  • Most C++ catastrophe vulnerabilities rely on uninitialized function pointers in a class.
    13·1 answer
  • Yeah help me please......................
    9·1 answer
  • How will you identify a file type on your computer?
    5·1 answer
  • Which requires large computer memory?
    15·1 answer
  • Constructive criticism is intended as a possible solution.
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!