Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.swing.*;
- import java.awt.*;
- import java.awt.Color;
- import java.awt.event.ActionEvent;
- import java.awt.event.ActionListener;
- import java.awt.event.MouseEvent;
- import java.awt.event.MouseListener;
- public class PS3{
- static JFrame frame;
- static JButton button;
- static JTextField label;
- static CenterPanel panel;
- static int N;
- static boolean buttonPressed = false;
- public static void main (String args[]) {
- PS3 gui = new PS3();
- gui.go();
- }
- public void go(){
- frame = new JFrame();
- button = new JButton("Ingresar numero");
- label = new JTextField();
- panel = new CenterPanel();
- frame.setSize(600,600);
- frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- frame.setLayout(new BorderLayout());
- frame.setVisible(true);
- frame.add(label, BorderLayout.PAGE_START);
- frame.add(panel, BorderLayout.CENTER);
- frame.add(button, BorderLayout.PAGE_END);
- button.addActionListener(new buttonAction());
- panel.addMouseListener(new panelAction());
- }
- class buttonAction implements ActionListener{
- public void actionPerformed(ActionEvent a){
- buttonPressed = false;
- String answer = label.getText();
- try{
- N = Integer.parseInt(answer);
- if(N<1){
- int E = Integer.parseInt("error.");
- }else{
- frame.setTitle("Dibujo "+N+" circulos");
- buttonPressed = true;
- }
- }catch(NumberFormatException ex){
- JOptionPane.showMessageDialog(null,"El numero ingresado no es valido","ERROR!",JOptionPane.INFORMATION_MESSAGE);
- }
- }
- }class panelAction implements MouseListener{
- public void mouseClicked(MouseEvent m){
- if(!buttonPressed){
- JOptionPane.showMessageDialog(null,"No se ha ingresado un numero.","ERROR!",JOptionPane.INFORMATION_MESSAGE);
- }for(int i = 1; i<=N ; i++){
- panel.draw(m,i);
- }
- }public void mouseEntered(MouseEvent m){
- }public void mouseExited(MouseEvent m){
- }public void mousePressed(MouseEvent m){
- }public void mouseReleased(MouseEvent m){
- }
- }public class CenterPanel extends JPanel{
- public void paintComponent(Graphics g){
- super.paintComponent(g);
- this.setBackground(Color.WHITE);
- }public void draw(MouseEvent m,int i){
- super.getGraphics().setColor(Color.BLACK);
- super.getGraphics().drawOval(m.getX(),m.getY(),i*50,i*50);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement