Wątek przeniesiony 2014-04-08 18:31 z Java przez ŁF.

Rysowanie prostokątu po przyciśnięciu przycisku "Rysuj"

0

Witam,
Chciałbym by w moim programie po naciśnięciu przycisku "Rysuj" został narysowany prostokąt, lecz nie mam pomysłu jak to wywołać, proszę o pomoc a to kod:.

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.*;


public class ButtonFrame extends JFrame {
	public ButtonFrame()
	{
		setSize(800,600);
		setTitle("Button");
		
		JButton first = new JButton("Kolor tla");
		JButton secend = new JButton("Rysuj");
		
		buttonPanel = new JPanel();
		
		buttonPanel.add(first);
		buttonPanel.add(secend);
		
		add(buttonPanel);
		
		ColorAction firstAction = new ColorAction();
		SecondAction secendAction = new SecondAction();
		
		first.addActionListener(firstAction);
		secend.addActionListener(secendAction);
		
	 }
	
		class ColorAction implements ActionListener {

			public void actionPerformed(ActionEvent event) {
				Color[] color = {Color.BLUE, Color.CYAN, Color.GRAY,Color.RED};
				int index = new Random().nextInt(color.length);
				buttonPanel.setBackground(color[index]);
			}
			
		}
		 class SecondAction implements ActionListener{
			 public void paintComponent(Graphics g)
			 {
				 Graphics2D g2 = (Graphics2D) g;
				 Rectangle2D rec = new Rectangle2D.Double(200,150,200,300);
				 g2.setPaint(Color.BLUE);
				 g2.fill(rec);
			 }

			public void actionPerformed(ActionEvent event) {
				//nie wiem jak wywolać 
			}
			 
		 }
		 private Color backgroundColor;
		 private JPanel buttonPanel;
} 

Z góry dziękuje :)

dodanie znacznika <code class="java"> i poprawienie tytułu wątku - furious programming

0

To co zrobiłeś zupełnie nie ma sensu, bo czemu ActionListener miałby mieć jakieś paintComponent? o_O Musisz mieć jakiś panel na którym będziesz rysował, przesłać go do actionListenera i rysować na nim.

0

"paintComplements" jest w tej samej klasie co "ActionListener" ale nie w nim. Właśnie nie mam pomysłu jak to napisać. Byłbym wdzięczny gdybyś wrzucił kod.

0
this.setSize(800,600);
this.setTitle("Button");

To nie są metody statyczne, należy ich użyć dla obiektu.

Zmienna buttonPanel nie została zadeklarowana.
Tablice twórz poza metodami; cholernie szkoda na to pamięci.
Wystarczy jeżeli w odpowiedzi na zdarzenie naciśnięcia przycisku zostanie wywołana metoda paintComponent (domyślnie przez metodę repaint()). Metodę paintComponent wyciągnąć z met. obsługującej zdarzenie i dać do klasy komponentu.

0

Zrobiłem tak jak napisałeś, wyciągnąłem "paintComponent" i wywołałem ją za pomocą "repaint" lecz nie działa. Pewnie coś źle napisałem. Był bym bardzo wdzięczny gdy byś napisał kod.

0
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.util.Random;

import javax.swing.*;


public class ButtonFrame extends JFrame {
	
	public void paintComponent(Graphics g)
	{
		Graphics2D g2 = (Graphics2D) g;
		 Rectangle2D rec = new Rectangle2D.Double(200,150,200,300);
		 g2.setPaint(Color.BLUE);
		 g2.fill(rec);
	}
	
	public ButtonFrame()
	{
		setSize(800,600);
		setTitle("Button");
		
		JButton first = new JButton("Kolor tla");
		JButton secend = new JButton("Rysuj");
		
		buttonPanel = new JPanel();
		
		buttonPanel.add(first);
		buttonPanel.add(secend);
		
		add(buttonPanel);
		
		ColorAction firstAction = new ColorAction();
		SecondAction secendAction = new SecondAction();
		
		first.addActionListener(firstAction);
		secend.addActionListener(secendAction);
		
		
	 }
	
		class ColorAction implements ActionListener {

			public void actionPerformed(ActionEvent event) {
				Color[] color = {Color.BLUE, Color.CYAN, Color.GRAY,Color.RED};
				int index = new Random().nextInt(color.length);
				buttonPanel.setBackground(color[index]);
			}
			
		}
		 class SecondAction implements ActionListener{

			public void actionPerformed(ActionEvent event) {
				repaint();
			}
			 
		 }
		 private Color backgroundColor;
		 private JPanel buttonPanel;
}
1

Widzę, że resztę uwag zlekceważyłeś. Sam się dopiero zapoznałem z podstawami swinga, ale przerobiłem twój kod tak, aby wyświetlał losowo zmianę tła. Co do rysowania prostokąta to chciałbym wiedzieć dlaczego nie działa to co napisałem(czyżby prostokąt był rysowany pod panelem?). Może ty to wyjaśnisz, albo ktoś inny.

import java.awt.*;
import java.awt.event.*;
import java.util.Random;

import javax.swing.*;
 

public class ButtonFrame extends JFrame {
	JPanel buttonPanel;
	Color[] color = {Color.BLUE, Color.CYAN, Color.GRAY,Color.RED};
	
    public ButtonFrame(String name){
    	super(name);
        this.setSize(800,600);
 
        JButton first = new JButton("Kolor tła");
        JButton secend = new JButton("Rysuj");
 
        buttonPanel = new JPanel();
        buttonPanel.add(first);
        buttonPanel.add(secend);
 
        add(buttonPanel);
 
        ColorAction firstAction = new ColorAction();
        SecondAction secendAction = new SecondAction();
 
        first.addActionListener(firstAction);
        secend.addActionListener(secendAction);
        this.setVisible(true);
    }
    
    public void paintComponent(Graphics g){
    	//super.paintComponent(g);
    	g.setColor(Color.black);
    	g.fillRect(200, 150, 200, 300);
    }
 
    class ColorAction implements ActionListener{
     	public void actionPerformed(ActionEvent event){
     		int index = new Random().nextInt(color.length);
     		buttonPanel.setBackground(color[index]);
        }
    }
    
    class SecondAction implements ActionListener{
    	 public void actionPerformed(ActionEvent event) {
                repaint();
         }
    } 
    
    /*public static void main(String[] args){
    	EventQueue.invokeLater(new Runnable() {
    		public void run(){
    			new ButtonFrame("Button");
    		}
    	});
    }*/

}
1
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
 
import javax.swing.*;
 
 
public class ButtonFrame extends JFrame {
    JPanel buttonPanel;
    Color[] color = {Color.BLUE, Color.CYAN, Color.GRAY,Color.RED};
    private boolean drawRectangle = false;
 
    public ButtonFrame(String name){
        super(name);
        this.setSize(800,600);
 
        JButton first = new JButton("Kolor tła");
        JButton secend = new JButton("Rysuj");
 
        buttonPanel = new JPanel();
        buttonPanel.add(first);
        buttonPanel.add(secend);
 
        add(buttonPanel,BorderLayout.NORTH);
        add(new Panelik(),BorderLayout.CENTER);
 
        ColorAction firstAction = new ColorAction();
        SecondAction secendAction = new SecondAction();
 
        first.addActionListener(firstAction);
        secend.addActionListener(secendAction);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setVisible(true);
    }
    class Panelik extends JPanel
    {
        public void paintComponent(Graphics g)
        {
            super.paintComponent(g);
            if(drawRectangle)
            {
                g.setColor(Color.black);
                g.fillRect(200, 150, 200, 300);
            }
        }
    }
 
    class ColorAction implements ActionListener{
         public void actionPerformed(ActionEvent event){
             int index = new Random().nextInt(color.length);
             buttonPanel.setBackground(color[index]);
        }
    }
 
    class SecondAction implements ActionListener{
         public void actionPerformed(ActionEvent event) {
            drawRectangle = true;
            repaint();
         }
    } 
 
    public static void main(String[] args){
        EventQueue.invokeLater(new Runnable() {
            public void run(){
                new ButtonFrame("Button");
            }
        });
    }
 
}
1

Tak właśnie myślałem, że trzeba by dziedziczyć po JPanel i dopiero na tym rysować.
@damian0999: Zrandomizujesz sobie jeszcze kolory rysowanego kwadratu, bo chyba taki był pierwotny zamiar, i masz gotowy program.

0

Wielkie dzięki :)

1 użytkowników online, w tym zalogowanych: 0, gości: 1