java


How to Draw some shape on JPanel using ActionListener?


I'm practising to draw a shape on a JPanel by clicking on a Jbutton, but I cannot. It's been five hours that I'm surfing the web, but I cannot find the way to do it.
This is what I want to do: if I click on "Rectangle" button a rectangle appears under the buttons and if I click on "Circle" button a circle appears.
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Shape extends JFrame {
JButton rec, circle;
static String botSelected;
Shape (){
frameSet ();
}
void frameSet(){
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600,300);
rec = new JButton ("Rectangle");
circle = new JButton("Circle");
JPanel panel = new JPanel();
frame.add(panel);
panel.add(rec);
panel.add(circle);
Click clk = new Click();
rec.addActionListener(clk);
circle.addActionListener(clk);
}
public void paint (Graphics g){
super.paint(g);
if (botSelected.equals("Rectangle"))
g.fillRect(50,50,50,50);
else if (botSelected.equals("Circle"))
g.fillOval(50,50,50,50);
}
public static void main (String [] arg){
Shape s = new Shape();
}
}
class Click implements ActionListener{
public void actionPerformed (ActionEvent e){
Shape.botSelected = e.getActionCommand();
}
}
The first thing I would do is have a read through Painting in Swing and Performing custom painting to better understand how the painting process works.
Next you need to understand that JFrame is a bad choice for painting to. Why? Because it's multilayered.
A JFrame contains a JRootPane, which contains a JLayeredPane the contentPane, glassPane and the JMenuBar and in your example, it also contains a JPanel.
With the (default) exception of the glassPane, all these components are opaque.
While it's possible to have something drawn in the paint method show it, if any of the other components paint themselves, it will be wiped clean - this is because Swing components can actually be painted independently of each other, with having to have the parent paint itself first.
A better solution is to start by extending from JPanel and override its paintComponent method.
For simplicity, I'd also encourage you to implement the ActionListener against this class as well, it will allow the actionPerformed method to access the properties of the component and, in your case, call repaint to trigger a paint cycle when you want to update the UI.
Here is a derived example from your code.
As #MadProgrammer said, don't extend JFrame.
In the following example, here are the major changes :
give a non-null value to botSelected, or the first calls to paintComponent will give you a NullPointerException
the class now extends JPanel, and overrides paintComponent for custom painting
the ActionListener is an anonymous class, because you don't need a separate class, and it has direct access to the fields from Shape
botSelected is no longer static (see above point)
.
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
class Shape extends JPanel {
JButton rec, circle;
String botSelected = "";// don't let it be null, it would make paintComponent crash on startup
Shape() {
frameSet();
}
void frameSet() {
JFrame frame = new JFrame();
frame.setVisible(true);
frame.setSize(600, 300);
rec = new JButton("Rectangle");
circle = new JButton("Circle");
frame.add(this);
this.add(rec);
this.add(circle);
// anonymous class, has access to fields from the outer class Shape
ActionListener clk = new ActionListener() {
public void actionPerformed(final ActionEvent e) {
botSelected = e.getActionCommand();
repaint();
}
};
rec.addActionListener(clk);
circle.addActionListener(clk);
}
//custom painting of the JPanel
#Override
public void paintComponent(final Graphics g) {
super.paintComponent(g);
if (botSelected.equals("Rectangle")) {
g.fillRect(50, 50, 50, 50);
} else if (botSelected.equals("Circle")) {
g.fillOval(50, 50, 50, 50);
}
}
public static void main(final String[] arg) {
Shape s = new Shape();
}
}

Related Links

JFreeChart Java - How to create it is a lot of series in a cycle
My hadoop code never enters a test on key value
NumberFormatException while reading CSV file
How to precalculate valid number of combinations instead of using while loop?
Changed JRE, now standard libraries aren't found?
Selenium giving problems with NoSuchFrameException
Java: Button isn't showing up
Making image available via url on tomcat
Store multiple values in HashMap and get it later
Java retrieving of data in ms access “Column not found” error
Encode UIImage in iOS7 and Decode it in Java (Base64)
Java NetBeans: Why does my string array terminate prematurely?
java.lang.RuntimeException: java.sql.SQLException: Field 'UserName' doesn't have a default value
Use java classes in liferay theme?
String Tokenizer separation
Does a LDAP Connection which successfully “connects” but fails to “bind”, requires to be disconnected?

Categories

HOME
java
date
testng
coq
single-sign-on
stock
electron
tesseract
jxls
acquia
azure-storage-tables
bs4
virtualization
cloudkit
php-7.1
visual-studio-2005
iron-router
designer
serverless-framework
seaborn
pc
excel-vba-mac
lldb
functional-testing
bootstrap-tour
language-agnostic
crystal-reports-2010
jaxb2
vision
google-cloud-nl
bpel
google-rich-snippets
directx-10
create-table
http-get
fog
pim
phpfreechat
uiswipegesturerecognizer
mixture-model
lumberjack
mesos-chronos
heightmap
angular2-meteor
fault
react-scripts
jlink
carrot
nsarray
mako
synchronous
time-and-attendance
parallel-data-warehouse
log4c
gstreamer-0.10
simplewebrtc
actionbardrawertoggle
bgp
independentsoft
pyke
responsive-images
web-controls
network-interface
cctv
qcodo
code-access-security
php-5.4
responsive-slides
ceil
tld
fluentautomation
yorick
intentservice
apc
bulkloader
yui-compressor
android-hardware
online-compilation
xdomainrequest
html-input
dmx512
suppress
asp.net-profiles
web-architecture
microsoft-virtualization
ti-dsp
nerddinner
signal-handling
rtml

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App