JTextField + JComboBox + rysowanie komponentu

0

Witam,
mam pewien problem, chciałem napisać program w którym wprowadza się x i y czyli rozmiar prostokąta lub okręgu, stworzyłem też JComboBox gdzie się wybiera prostokąt czy okrąg ma zostać narysowany, lecz nie wiem czemu program mi nie chce narysować.
Oto kod:

import java.awt.*;
import java.awt.event.*;
import java.awt.geom.Rectangle2D;
import javax.swing.*;
import javax.swing.border.*;


@SuppressWarnings("serial")
public class Frame extends JFrame {
	public Frame()
	{
		this.setSize(800, 600);
		this.setTitle("Rysowanie + Parametry");
		this.setLocation(100,100);
		this.setLayout(null);
		
		panel = new JPanel();
		obramowaniePanel = new JPanel();
		wypelnieniePanel = new JPanel();
		textAreaPanel = new JPanel();
		
		//Parametry
		
		x = new JTextField();
		x.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
                            e.consume();
            }
		});

		x.setBounds(150, 25, 100, 30);
		add(x);
		xlabel = new JLabel("X");
		xlabel.setBounds(140, 25, 30, 30);
		add(xlabel);
		
		y = new JTextField();
		y.addKeyListener(new KeyAdapter() {
            public void keyTyped(KeyEvent e) {
                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
                            e.consume();
            }
		});
		y.setBounds(300, 25, 100, 30);
		add(y);
		ylabel = new JLabel("Y");
		ylabel.setBounds(290, 25, 30, 30);
		add(ylabel);
		
		box = new JComboBox<String>();
		box.addItem("Prostokąt");
		box.addItem("Okrąg");
		box.setBounds(450, 25, 100, 30);
		box.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {				
				
				@SuppressWarnings({ "unused", "hiding" })
				class MyDraw extends JPanel{
						
						public void paintComponent(Graphics g)
						{
					
							Graphics2D g2 = (Graphics2D)g;
							if(box.getSelectedItem().equals("Prostokąt"))
							{
								Rectangle2D rec = new Rectangle2D.Double(100, 100,10, 100);
								g2.setPaint(Color.BLUE);
								g2.fill(rec);
							}
				
						}
					}
				}
		});
		add(box);
		
		button = new JButton("Wyczyść");
		button.setBounds(600, 25, 100, 30);
		button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent arg0) {
				
				
			}
			
		});
		add(button);
		
		 matte = BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK);
			title = BorderFactory.createTitledBorder(matte, "Opcje");
			panel.setBorder(title);
			panel.setBounds(20, 2, 760, 80);
			add(panel);
			
			//Wypelnienie
			
			r = new JTextField();
			r.addKeyListener(new KeyAdapter() {
	            public void keyTyped(KeyEvent e) {
	                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
	                            e.consume();
	            }
			});
			r.setBounds(650, 105, 100, 30);
			add(r);
			rlabel = new JLabel("R");
			rlabel.setBounds(640, 105, 30, 30);
			add(rlabel);
			
			g = new JTextField();
			g.addKeyListener(new KeyAdapter() {
	            public void keyTyped(KeyEvent e) {
	                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
	                            e.consume();
	            }
			});
			g.setBounds(650, 155, 100, 30);
			add(g);
			glabel = new JLabel("G");
			glabel.setBounds(640, 155, 30, 30);
			add(glabel);
			
			b = new JTextField();
			b.addKeyListener(new KeyAdapter() {
	            public void keyTyped(KeyEvent e) {
	                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
	                            e.consume();
	            }
			});
			b.setBounds(650, 205, 100, 30);
			add(b);
			blabel = new JLabel("B");
			blabel.setBounds(640, 205, 30, 30);
			add(blabel);
			
			wypelnienie = new JComboBox<String>();
			wypelnienie.addItem("Niebieski");
			wypelnienie.addItem("Zielony");
			wypelnienie.addItem("Różowy");
			wypelnienie.addItem("Czarny");
			wypelnienie.addItem("Biały");
			wypelnienie.addItem("Żółty");
			wypelnienie.addItem("Pomarańczowy");
			wypelnienie.setBounds(650, 255, 100, 30);
			wypelnienie.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent arg0) {
					
					
				}
				
			});
			add(wypelnienie);
			
			wypelnienieMatte = BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK);
			wypelnienieTitle = BorderFactory.createTitledBorder(wypelnienieMatte, "Wypelnienie");
			wypelnieniePanel.setBorder(wypelnienieTitle);
			wypelnieniePanel.setBounds(620, 85, 160, 230);
				add(wypelnieniePanel);
				
				
				//Obramowanie
				r2 = new JTextField();
				r2.addKeyListener(new KeyAdapter() {
		            public void keyTyped(KeyEvent e) {
		                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
		                            e.consume();
		            }
				});
				r2.setBounds(650, 340, 100, 30);
				add(r2);
				rlabel2 = new JLabel("R");
				rlabel2.setBounds(640, 340, 30, 30);
				add(rlabel2);
				
				g2 = new JTextField();
				g2.addKeyListener(new KeyAdapter() {
		            public void keyTyped(KeyEvent e) {
		                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
		                            e.consume();
		            }
				});
				g2.setBounds(650, 390, 100, 30);
				add(g2);
				glabel2 = new JLabel("G");
				glabel2.setBounds(640, 390, 30, 30);
				add(glabel2);
				
				b2 = new JTextField();
				b2.addKeyListener(new KeyAdapter() {
		            public void keyTyped(KeyEvent e) {
		                    if (!(e.getKeyChar() >= '0' && e.getKeyChar() <= '9'))
		                            e.consume();
		            }
				});
				b2.setBounds(650, 440, 100, 30);
				add(b2);
				blabel2 = new JLabel("B");
				blabel2.setBounds(640, 440, 30, 30);
				add(blabel2);
				
				obramowanie = new JComboBox<String>();
				obramowanie.addItem("Niebieski");
				obramowanie.addItem("Zielony");
				obramowanie.addItem("Różowy");
				obramowanie.addItem("Czarny");
				obramowanie.addItem("Biały");
				obramowanie.addItem("Żółty");
				obramowanie.addItem("Pomarańczowy");
				obramowanie.setBounds(650, 490, 100, 30);
				obramowanie.addActionListener(new ActionListener(){
					@Override
					public void actionPerformed(ActionEvent arg0) {
						
						
					}
					
				});
				add(obramowanie);
				
				 obramowanieMatte = BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK);
				 obramowanieTitle = BorderFactory.createTitledBorder(obramowanieMatte, "Obramowanie");
				 obramowaniePanel.setBorder(obramowanieTitle);
				 obramowaniePanel.setBounds(620, 325, 160, 230);
					add(obramowaniePanel);
					
				//Panel Tekstowy
					
				textArea = new JTextArea();
				textArea.setLineWrap(true);
				textArea.setBounds(20, 475, 580, 70);
				add(textArea);
				
				textAreaMatte = BorderFactory.createMatteBorder(3, 3, 3, 3, Color.BLACK);
				textAreaTitle = BorderFactory.createTitledBorder(textAreaMatte, "Dane");
				textAreaPanel.setBorder(textAreaTitle);
				textAreaPanel.setBounds(10, 455, 600, 100);
				add(textAreaPanel);
		
	}
		class MyDraw extends JPanel
		{
			public void paintComponent(Graphics g)
			{
					if((box.getSelectedItem().equals("Pogrubiona")))
					{
						System.out.println(" nieOkrąg");
					}
					if(box.getSelectedItem().equals("Okrąg"))
					{
						System.out.println("Okrąg");
					}
						
			}
		}
		
	private JButton button;
	private JPanel textAreaPanel;
	private TitledBorder textAreaTitle;
	private MatteBorder textAreaMatte;
	private JTextArea textArea;
	private JTextField r2;
	private JTextField g2;
	private JTextField b2;
	private JLabel rlabel2;
	private JLabel glabel2;
	private JLabel blabel2;
	private JComboBox<String> obramowanie;
	private JComboBox<String> wypelnienie;
	private MatteBorder obramowanieMatte;
	private TitledBorder obramowanieTitle;
	private MatteBorder wypelnienieMatte;
	private TitledBorder wypelnienieTitle;
	private JPanel obramowaniePanel;
	private JPanel wypelnieniePanel;
	private JTextField r;
	private JTextField g;
	private JTextField b;
	private JLabel rlabel;
	private JLabel glabel;
	private JLabel blabel;
	private MatteBorder matte;
	private TitledBorder title;
	private JPanel panel;
	private JComboBox<String> box;
	private JTextField x;
	private JTextField y;
	private JLabel xlabel;
	private JLabel ylabel;
}

Proszę o pomoc :)
Z góry dziękuje

0

użyj funkcji repaint(); żeby narysować to co w paintComponent.

0

Niestety też nie działa :(

                         public void paintComponent(Graphics g)
						{
							if(box.getSelectedItem().equals("Prostokąt"))
							{
								Graphics2D g2 = (Graphics2D)g;
								Rectangle2D rec = new Rectangle2D.Double(300, 300,10, 100);
								g2.setPaint(Color.BLUE);
								g2.fill(rec);
								repaint();
							}
				
						}
					}
0

Ale nie wewnątrz tej funkcji. Nie zaglądałem szczegółowo do kodu bo jest słabo czytelny, ale generalnie: musisz nadpisać funkcję paintComponent(Graphics g), (tak jak to zrobiłeś) w niej umieścić to co chcesz narysować. Nie należy bezpośrednio wywoływać tej funkcji. Aby narysować to co w paintComponent, używasz funkcji repaint(). http://stackoverflow.com/questions/15544549/how-does-paintcomponent-work
Pozdrawiam!

0

No nie wiem gdzie mam wstawić to "repaint" mógłbyś napisać gdzie. Będe bardzo wdzięczny

0

Teraz to wygląda tak:

		class MyDraw extends JPanel{
						
						public void paintComponent(Graphics g)
						{
							if(box.getSelectedItem().equals("Prostokąt"))
							{
								super.paintComponent(g);
								Graphics2D g2 = (Graphics2D)g;
								Rectangle2D rec = new Rectangle2D.Double(300, 300,10, 100);
								g2.setPaint(Color.BLUE);
								g2.fill(rec);
							}
							
							if(box.getSelectedItem().equals("Okrąg"))
							{
								super.paintComponent(g);
								Graphics2D g2 = (Graphics2D)g;
								Rectangle2D rec = new Rectangle2D.Double(300, 300,10, 100);
								g2.setPaint(Color.BLUE);
								g2.fill(rec);
							}
				
						}
					}
				}
		});
		add(box);
		
		button = new JButton("OK");
		button.setBounds(600, 25, 100, 30);
		button.addActionListener(new ActionListener(){
			@Override
			public void actionPerformed(ActionEvent event) {
				repaint();
				
			}
			
		});

I dalej nie działa, nie mam pojęcia gdzie to mam dać

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