Painting multiple rectangles to a JPanel - java

How can I paint multiple rectangles to a JPanel?
This code here only allows me paint one rectangle at a time. It deletes the previously made rectangle once I start a new one. I need to write a program that paints more than 1 rectangle so it would probably look like a collage of rectangles of different colors.
public class Rectangles extends JFrame implements ActionListener {
int x1, y1, x2, y2;
JPanel main;
JPanel right;
JButton color1;
JButton color2;
JButton color3;
JButton color4;
JButton color5;
JButton color6;
JButton color7;
JButton color8;
JButton color9;
JButton color10;
Canvas left;
public Rectangles() {
main = new JPanel(new BorderLayout(5, 5));
main.setBorder(new EmptyBorder(20, 20, 20, 20));
main.setBackground(new Color(0, 168, 165));
right = new JPanel(new GridLayout(5, 2, 3, 3));
right.setPreferredSize(new Dimension(150, 250));
right.setBackground(new Color(0, 168, 165));
color1 = new JButton();
color1.setBackground(Color.LIGHT_GRAY);
color1.addActionListener(this);
color1.setBorder(new LineBorder(Color.BLACK, 5, false));
color2 = new JButton();
color2.setBackground(Color.BLUE);
color2.addActionListener(this);
color2.setBorder(new LineBorder(Color.BLACK, 5, false));
color3 = new JButton();
color3.setBackground(Color.CYAN);
color3.addActionListener(this);
color3.setBorder(new LineBorder(Color.BLACK, 5, false));
color4 = new JButton();
color4.setBackground(Color.DARK_GRAY);
color4.addActionListener(this);
color4.setBorder(new LineBorder(Color.BLACK, 5, false));
color5 = new JButton();
color5.setBackground(Color.GRAY);
color5.addActionListener(this);
color5.setBorder(new LineBorder(Color.BLACK, 5, false));
color6 = new JButton();
color6.setBackground(Color.GREEN);
color6.addActionListener(this);
color6.setBorder(new LineBorder(Color.BLACK, 5, false));
color7 = new JButton();
color7.setBackground(Color.YELLOW);
color7.addActionListener(this);
color7.setBorder(new LineBorder(Color.BLACK, 5, false));
color8 = new JButton();
color8.setBackground(Color.MAGENTA);
color8.addActionListener(this);
color8.setBorder(new LineBorder(Color.BLACK, 5, false));
color9 = new JButton();
color9.setBackground(Color.PINK);
color9.addActionListener(this);
color9.setBorder(new LineBorder(Color.BLACK, 5, false));
color10 = new JButton();
color10.setBackground(Color.RED);
color10.addActionListener(this);
color10.setBorder(new LineBorder(Color.BLACK, 5, false));
right.add(color1);
right.add(color2);
right.add(color3);
right.add(color4);
right.add(color5);
right.add(color6);
right.add(color7);
right.add(color8);
right.add(color9);
right.add(color10);
left = new Canvas();
left.setPreferredSize(new Dimension(500, 250));
left.setBackground(Color.WHITE);
left.setColor(Color.WHITE);
left.setBorder(new LineBorder(Color.BLACK, 5, false));
main.add(left, BorderLayout.CENTER);
main.add(right, BorderLayout.EAST);
this.add(main);
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setLocation(450,75);
this.setTitle("MARQUEZ Rectangles");
this.setResizable(false);
this.setVisible(true);
}
public static void main(String[] args) {
Rectangles r = new Rectangles();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource() == color1) {
left.setColor(Color.LIGHT_GRAY);
}
else if(e.getSource() == color2) {
left.setColor(Color.BLUE);
}
else if(e.getSource() == color3) {
left.setColor(Color.CYAN);
}
else if(e.getSource() == color4) {
left.setColor(Color.BLACK);
}
else if(e.getSource() == color5) {
left.setColor(Color.GRAY);
}
else if(e.getSource() == color6) {
left.setColor(Color.GREEN);
}
else if(e.getSource() == color7) {
left.setColor(Color.YELLOW);
}
else if(e.getSource() == color8) {
left.setColor(Color.MAGENTA);
}
else if(e.getSource() == color9) {
left.setColor(Color.PINK);
}
else {
left.setColor(Color.RED);
}
}
}
This class paints the rectangle.
class Canvas extends JPanel implements MouseListener,MouseMotionListener {
private int x,y,x1,y1;
Color color;
public Canvas() {
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
public void setColor(Color color) {
this.color = color;
}
public void mouseEntered(MouseEvent e) {}
public void mouseClicked(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public void mouseMoved(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {
x = e.getX();
y = e.getY();
}
public void mouseDragged(MouseEvent e) {
x1 = e.getX();
y1 = e.getY();
revalidate();
repaint();
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(color);
g.fillRect(x, y, Math.abs(x-x1), Math.abs(y-y1));
}
}

The paint operations we do are 'transient' and will be destroyed the next time the component is painted. To get around that, there are two common techniques.
Store a list of shapes (images, gradients and other painting operations), add the current drawing to the list, and when requested, paint them all.
Draw to a BufferedImage that is painted when requested/needed. See this answer for an example.

Your canvas only paints the most recent rectangle, swing painting works like that, once a paint is requested the previous buffer is cleared. What you need is a List<Rectangle>, every time you select a rectangle with the mouse, add it to the list and in the canvas draw every rectangle in the list. You will also need to save the color of the previous rectangles, either by making a a wrapper class for the rectangle that has color as a field or save that in a list too.

Related

Having troubles rendering rectangle on button press

I am trying to add a Rectangle in the panel by button press and remove it with another one. It should work but it doesn't render anything, and I absolutely don't know why.
Can someone explain what I am doing wrong and give me some nice tips what I can improve with my code?
public class GUI extends JPanel {
public static boolean isRecVisible = false;
public static void main(String[] args) {
createGui();
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g);
g2d.drawRect(10, 10, 200, 200);
}
public static void createGui() {
int frameWidth = 550;
int frameHeight = 400;
int buttonWidth1 = 250;
int buttonHeight1 = 30;
int buttonWidth2 = 500;
int buttonHeight2 = 30;
int displayWidth = frameWidth - 20;
int displayHeight = frameHeight - 105;
GUI drawRec = new GUI();
JFrame f = new JFrame("Rectangle");
JPanel p = new JPanel();
JPanel display = new JPanel();
JButton addRec = new JButton("Add Rectangle");
JButton removeRec = new JButton("Remove Rectangle");
JButton colorRec = new JButton("Color Rectangle");
f.add(p);
p.add(addRec);
p.add(removeRec);
p.add(colorRec);
p.add(display);
display.setBackground(Color.LIGHT_GRAY);
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
display.setBounds(frameWidth / 2 - displayWidth / 2, 10, displayWidth, displayHeight);
addRec.setBounds(frameWidth / 2 - buttonWidth1 / 2 - 250 / 2, frameHeight - 85, buttonWidth1, buttonHeight1);
removeRec.setBounds(frameWidth / 2 - buttonWidth1 / 2 + 250 / 2, frameHeight - 85, buttonWidth1, buttonHeight1);
colorRec.setBounds(frameWidth / 2 - buttonWidth2 / 2, frameHeight - 60, buttonWidth2, buttonHeight2);
addRec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (isRecVisible == false) {
isRecVisible = true;
display.add(drawRec);
display.repaint();
System.out.println("Rectangle has been drawn!");
} else {
System.out.println("Rectangle has already been drawn!");
return;
}
}
});
removeRec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
if (isRecVisible == true) {
isRecVisible = false;
System.out.println("Rectangle has been removed!");
} else {
System.out.println("Rectangle has already been removed");
return;
}
}
});
}
}
display.add(drawRec);
display.repaint();
When you add (or remove) components to a visible frame then the basic logic is:
display.add(...);
display.revalidate();
display.repaint(); // sometimes needed
The revalidate() is the key method because it invokes the layout manager so the size/location of the component can be set.
However, that still won't fix the problem because your custom panel doesn't have a preferred size, so there is nothing to paint for your component.
You need to override the getPreferredSize() method of your custom panel to return the preferred size of your custom component. So in your case you might set the preferred size to be (220, 220) so the rectangle is centered in the panel.
Read the section from the Swing tutorial on Custom Painting for more information and complete working examples.
Note: the tutorial example will also show you how to better structure your code to make sure the GUI is created on the Event Dispatch Thread.
Rather than adding or removing components, it would make more sense here to add the custom painted panel on construction, and use the isRecVisible boolean as a flag to test for drawing the rectangle.
Something like shown here:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel {
public static boolean isRecVisible = false;
public static void main(String[] args) {
createGui();
}
#Override
protected void paintComponent(Graphics g) {
Graphics2D g2d = (Graphics2D) g;
super.paintComponent(g);
if (isRecVisible) {
g2d.drawRect(10, 10, 200, 200);
}
}
#Override
public Dimension getPreferredSize() {
return new Dimension(600,400);
}
public static void createGui() {
int frameWidth = 550;
int frameHeight = 400;
GUI drawRec = new GUI();
drawRec.setBackground(Color.LIGHT_GRAY);
JFrame f = new JFrame("Rectangle");
JPanel p = new JPanel();
JButton addRec = new JButton("Add Rectangle");
JButton removeRec = new JButton("Remove Rectangle");
JButton colorRec = new JButton("Color Rectangle");
f.add(p, BorderLayout.PAGE_START);
p.add(addRec);
p.add(removeRec);
p.add(colorRec);
f.add(drawRec);
f.setSize(frameWidth, frameHeight);
f.setLocationRelativeTo(null);
f.setResizable(false);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setVisible(true);
addRec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
isRecVisible = true;
drawRec.repaint();
}
});
removeRec.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
isRecVisible = false;
drawRec.repaint();
}
});
}
}

JTextField horizontal scrolling java swing

I have a JTextField inside a JPanel . The user can type text as big as she wants in the textfield , however horizontal scrolling should appear when the text goes beyond the screen of the textfield. At this point if the user backspace the text and shrink the text within the screen of the textfield , the scrolling should disappear. So basically scrolling should appear only when its needed. How to do it ? Help please.
If you don't like the behavior of the JScrollPane + JTextField, you need to use JScrollBar#setModel(BoundedRangeModel) + JTextField#getHorizontalVisibility():
import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.BasicScrollBarUI;
public class TextFieldScrollBarTest {
private static final String TEXT = "javascript:(function(){var l=location,m=l.href.match('^(https?://)(.+)(api[^+]+|technotes[^+]+)');if(m)l.href=m[1]+'docs.oracle.com/javase/8/docs/'+decodeURIComponent(m[3]).replace(/\\+.*$/,'').replace(/\\[\\]/g,':A').replace(/, |\\(|\\)/g,'-');}());";
public JComponent makeUI() {
JScrollPane scroll = new JScrollPane(new JTextField(TEXT));
scroll.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
JTextField textField = new JTextField(TEXT);
JScrollBar scroller = new JScrollBar(Adjustable.HORIZONTAL) {
#Override public void updateUI() {
//super.updateUI();
setUI(new ArrowButtonlessScrollBarUI());
}
#Override public Dimension getPreferredSize() {
Dimension d = super.getPreferredSize();
d.height = 5;
return d;
}
};
scroller.setModel(textField.getHorizontalVisibility());
Box box = Box.createVerticalBox();
box.add(scroll);
box.add(Box.createVerticalStrut(50));
box.add(textField);
box.add(Box.createVerticalStrut(2));
box.add(scroller);
box.add(Box.createVerticalGlue());
JPanel p = new JPanel(new BorderLayout());
p.setBorder(BorderFactory.createEmptyBorder(20, 20, 20, 20));
p.add(box, BorderLayout.NORTH);
return p;
}
public static void main(String... args) {
EventQueue.invokeLater(() -> {
JFrame f = new JFrame();
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
f.getContentPane().add(new TextFieldScrollBarTest().makeUI());
f.setSize(320, 240);
f.setLocationRelativeTo(null);
f.setVisible(true);
});
}
}
class ZeroSizeButton extends JButton {
private static final Dimension ZERO_SIZE = new Dimension();
#Override public Dimension getPreferredSize() {
return ZERO_SIZE;
}
}
class ArrowButtonlessScrollBarUI extends BasicScrollBarUI {
private static final Color DEFAULT_COLOR = new Color(220, 100, 100);
private static final Color DRAGGING_COLOR = new Color(200, 100, 100);
private static final Color ROLLOVER_COLOR = new Color(255, 120, 100);
#Override protected JButton createDecreaseButton(int orientation) {
return new ZeroSizeButton();
}
#Override protected JButton createIncreaseButton(int orientation) {
return new ZeroSizeButton();
}
#Override protected void paintTrack(Graphics g, JComponent c, Rectangle r) {
//Graphics2D g2 = (Graphics2D) g.create();
//g2.setPaint(new Color(100, 100, 100));
//g2.fillRect(r.x, r.y, r.width - 1, r.height - 1);
//g2.dispose();
}
#Override protected void paintThumb(Graphics g, JComponent c, Rectangle r) {
JScrollBar sb = (JScrollBar) c;
if (!sb.isEnabled()) {
return;
}
BoundedRangeModel m = sb.getModel();
int iv = m.getMaximum() - m.getMinimum() - m.getExtent() - 1; // -1: bug?
if (iv > 0) {
Graphics2D g2 = (Graphics2D) g.create();
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
Color color;
if (isDragging) {
color = DRAGGING_COLOR;
} else if (isThumbRollover()) {
color = ROLLOVER_COLOR;
} else {
color = DEFAULT_COLOR;
}
g2.setPaint(color);
g2.fillRect(r.x, r.y, r.width - 1, r.height - 1);
g2.dispose();
}
}
}

Rectangle Color Chooser program

I am having two problems here. I'm making a simple program to test, basically when you click the button JColorChooser will pop up and you can choose what color you want your rectangle to be. And the second problem is i cannot position my buttons at BorderLayout.SOUTH or BorderLayout.NORTH Or anywhere. These are my code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class GUI extends JPanel {
private Color a = (Color.WHITE);
private Color b = (Color.WHITE);
private final JPanel panel;
private final JButton ab;
private final JButton bb;
private int x = 1;
private int y = 1;
public GUI() {
panel = new JPanel();
panel.setBackground(Color.WHITE);
ab = new JButton("Choose your first Rectangle color");
ab.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
a = JColorChooser.showDialog(null, "Pick a Color", a);
x = 2;
}
});
bb = new JButton("Choose your second Rectangle color");
bb.addActionListener(new ActionListener() {
#Override
public void actionPerformed(ActionEvent event) {
b = JColorChooser.showDialog(null, "Pick a Color", b);
y = 2;
}
});
add(ab, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(bb, BorderLayout.SOUTH);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
this.setBackground(Color.WHITE);
if (x == 2)
g.setColor(a);
g.fillRect(50, 50, 100, 20);
if (y == 2)
g.setColor(b);
g.fillRect(50, 200, 100, 20);
}
}
And the second problem is i cannot position my buttons at BorderLayout.SOUTH or BorderLayout.NORTH Or anywhere.
JPanel uses a FlowLayout by default, try adding setLayout(new BorderLayout()) before you add any components
setLayout(new BorderLayout());
add(ab, BorderLayout.NORTH);
add(panel, BorderLayout.CENTER);
add(bb, BorderLayout.SOUTH);
I'm making a simple program to test, basically when you click the button jcolorchooser will pop up and you can choose what color you want your rectangle to be
Okay, I'm "guessing" that once you've selected a color, it's not changing the rectangle color.
Simply add a call to repaint after you've changed the color
ab.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
a = JColorChooser.showDialog(null, "Pick a Color", a);
x = 2;
repaint();
}
}
);
bb = new JButton("Choose your second Rectangle color");
bb.addActionListener(
new ActionListener() {
public void actionPerformed(ActionEvent event) {
b = JColorChooser.showDialog(null, "Pick a Color", b);
y = 2;
repaint();
}
}
);

Shapes disappearing

Alright, I figured out everything that I got but now I am really stuck. Every time you choose a different shape the previously selected one disappears. How do I make it so they don't disappear and stay on the screen until you exit?
import java.awt.*;
import java.awt.event.*;
import java.awt.geom.*;
import java.util.*;
import javax.swing.*;
public class ShapeStamper extends JFrame{
Random rand = new Random();
public int x;
public int y;
private JPanel panel1, panel2;
private JButton button1, button2, button3, button4;
private int option = 0;
public ShapeStamper(){
super("Shape Stamper!");
panel1 = new JPanel();
button1 = new JButton("Circle");
button2 = new JButton("Square");
button3 = new JButton("Rectangle");
button4 = new JButton("Oval");
button1.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 1;
}
}
);
button2.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 2;
}
}
);
button3.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 3;
}
}
);
button4.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent ae){
option = 4;
}
}
);
panel2 = new JPanel();
panel2.setBackground(Color.WHITE);
MouseHandler mouse = new MouseHandler();
setVisible(true);
addMouseListener(mouse);
addMouseMotionListener(mouse);
add(panel2);
panel1.add(button1);
panel1.add(button2);
panel1.add(button3);
panel1.add(button4);
add(panel1, BorderLayout.SOUTH);
setSize(500,500);
setVisible(true);
}
private class MouseHandler extends MouseAdapter implements MouseMotionListener{
#Override
public void mousePressed(MouseEvent e){
x = e.getX();
y = e.getY();
repaint();
}
}
public void paint(Graphics g){
super.paintComponents(g);
Graphics2D g2d = (Graphics2D) g;
if(option == 0){
g.setFont(new Font("Serif", Font.BOLD, 32));
g.drawString("Shape Stamper!", 150, 220);
g.setFont(new Font("Serif", Font.ITALIC, 16));
g.drawString("Programmed by: Chris", 150, 230);
}
if(option == 1){
Color randColor1 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor1);
g2d.drawOval(50, 50, 100, 100);
}
if(option == 2){
Color randColor2 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor2);
g2d.drawRect(50, 50, 100, 100);
}
if(option == 3){
Color randColor3 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor3);
g2d.draw(new Rectangle2D.Double(75,50,150,100));
}
if(option == 4){
Color randColor4 = new Color(rand.nextInt(256), rand.nextInt(256), rand.nextInt(256));
g2d.setPaint(randColor4);
g2d.draw(new Ellipse2D.Double(50, 25, 100, 50));
}
}
public static void main(String[] args) {
ShapeStamper application = new ShapeStamper();
application.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
}
You should not be overriding paint of a top level container and then trying to painting over the top of the components you have already added.
The basic problem you will encounter is, the paint system is clever enough that it may not ever call paint of the frame, but simply update the child components directly instead.
Instead, create yourself a custom component, extending from something like JPanel and override it's paintComponent method and perform your custom painting there. Then add this component to your frame.
You will also find the the paint updates are cleaner and won't flicker when updated.
You should also make sure you are calling repaint on this custom component when ever you change one it's options to ensure that changes are painted back to the component
Take a look at Performing Custom Painting for more details.
Also, just to be clear, you should not be calling super.paintComponents from paint (or in fact anywhere except for when you override paintComponents...which there really should be a need to do...)

Draw in an image inside panel

I have a panel with two buttons. I'm trying to insert an image inside the panel and I want to draw lines inside the image after clicking on a button. I have used the below code but this doesn't seem to work.
public class Try_Panel extends JFrame {
// start attributes
private JPanel jPanel1 = new JPanel(null, true);
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
// end attributes
public Try_Panel(String title) {
// Frame-Init
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 300;
int frameHeight = 300;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
setResizable(false);
Container cp = getContentPane();
cp.setLayout(null);
// start components
jPanel1.setBounds(48, 24, 209, 145);
jPanel1.setOpaque(false);
cp.add(jPanel1);
jButton1.setBounds(88, 208, 75, 25);
jButton1.setText("jButton1");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
cp.add(jButton1);
jButton2.setBounds(184, 208, 75, 25);
jButton2.setText("jButton2");
jButton2.setMargin(new Insets(2, 2, 2, 2));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2_ActionPerformed(evt);
}
});
cp.add(jButton2);
// end components
setVisible(true);
} // end of public Try_Panel
// start methods
public void jButton1_ActionPerformed(ActionEvent evt) {
BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
JLabel l=new JLabel(new ImageIcon(image));
Graphics graphics = image.getGraphics();
Graphics2D g = (Graphics2D) graphics;
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLUE);
g.drawLine(0, 0, 300, 400);
jPanel1.add(l);
} // end of jButton1_ActionPerformed
public void jButton2_ActionPerformed(ActionEvent evt) {
// TODO add your code here
} // end of jButton2_ActionPerformed
// end methods
public static void main(String[] args) {
new Try_Panel("Try_Panel");
} // end of main
} // end of class Try_Panel
The biggest problem is the same code worked in my other class.
Try wrapping the image inside the ImageIcon AFTER you have updated it. Also, you should also call Graphics#dispose when you are finished rendering to the graphics context.
public void jButton1_ActionPerformed(ActionEvent evt) {
BufferedImage image=new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLUE);
g.drawLine(0, 0, 300, 400);
g.dispose();
JLabel l=new JLabel(new ImageIcon(image));
jPanel1.add(l);
}
You should also rely on the layout managers rather the trying to do it yourself, it will simply make your life easier.
Personally, I think it would easier to paint directly to a custom component, like JPanel. Check out Performing Custom Painting for more details
UPDATED with example
Basically, I changed your example to
Use layout managers
Load the UI within the context of the EDT
revalidate the jPanel1
public class BadLabel extends JFrame {
// start attributes
private JPanel jPanel1 = new JPanel(new BorderLayout(), true);
private JButton jButton1 = new JButton();
private JButton jButton2 = new JButton();
// end attributes
public BadLabel(String title) {
// Frame-Init
super(title);
setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
int frameWidth = 300;
int frameHeight = 300;
setSize(frameWidth, frameHeight);
Dimension d = Toolkit.getDefaultToolkit().getScreenSize();
int x = (d.width - getSize().width) / 2;
int y = (d.height - getSize().height) / 2;
setLocation(x, y);
// setResizable(false);
Container cp = getContentPane();
// cp.setLayout(null);
// start components
// jPanel1.setBounds(48, 24, 209, 145);
jPanel1.setOpaque(true);
jPanel1.setBackground(Color.RED);
cp.add(jPanel1);
JPanel buttons = new JPanel();
// jButton1.setBounds(88, 208, 75, 25);
jButton1.setText("jButton1");
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
buttons.add(jButton1);
// jButton2.setBounds(184, 208, 75, 25);
jButton2.setText("jButton2");
jButton2.setMargin(new Insets(2, 2, 2, 2));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2_ActionPerformed(evt);
}
});
buttons.add(jButton2);
// end components
cp.add(buttons, BorderLayout.SOUTH);
setVisible(true);
} // end of public BadLabel
// start methods
public void jButton1_ActionPerformed(ActionEvent evt) {
BufferedImage image = new BufferedImage(jPanel1.getWidth(), jPanel1.getHeight(), BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g = image.createGraphics();
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.setColor(Color.BLUE);
g.drawLine(0, 0, 300, 400);
g.dispose();
JLabel l = new JLabel(new ImageIcon(image));
l.setBorder(new LineBorder(Color.BLUE));
jPanel1.add(l);
jPanel1.revalidate();
} // end of jButton1_ActionPerformed
public void jButton2_ActionPerformed(ActionEvent evt) {
// TODO add your code here
} // end of jButton2_ActionPerformed
// end methods
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
} catch (Exception exp) {
}
new BadLabel("BadLabel");
}
});
} // end of main
} // end of class BadLabel}
Something like this:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import javax.swing.border.EmptyBorder;
public class Try_Panel {
// start attributes
private JButton jButton1 = new JButton("jButton1");
private JButton jButton2 = new JButton("jButton2");
private JLabel imageView;
BufferedImage image;
int x = 300;
int y = 50;
// end attributes
public Component getGui() {
JPanel gui = new JPanel(new BorderLayout(5, 5));
gui.setBorder(new EmptyBorder(3, 3, 3, 3));
image = new BufferedImage(300, 100, BufferedImage.TYPE_INT_RGB);
imageView = new JLabel(new ImageIcon(image));
gui.add(imageView, BorderLayout.CENTER);
JPanel buttons = new JPanel(new FlowLayout(FlowLayout.CENTER, 15, 15));
gui.add(buttons, BorderLayout.PAGE_END);
jButton1.setMargin(new Insets(2, 2, 2, 2));
jButton1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton1_ActionPerformed(evt);
}
});
buttons.add(jButton1);
jButton2.setMargin(new Insets(2, 2, 2, 2));
jButton2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent evt) {
jButton2_ActionPerformed(evt);
}
});
buttons.add(jButton2);
// end components
return gui;
}
public static void main(String[] args) {
Runnable r = new Runnable() {
public void run() {
// Frame-Init
JFrame f = new JFrame("Try Panel");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setLocationByPlatform(true);
Container cp = f.getContentPane();
cp.setLayout(new BorderLayout(3, 3));
Try_Panel tp = new Try_Panel();
cp.add(tp.getGui());
f.pack();
f.setMinimumSize(f.getSize());
f.setVisible(true);
}
};
SwingUtilities.invokeLater(r);
} // end of public Try_Panel
// start methods
public void jButton1_ActionPerformed(ActionEvent evt) {
Graphics graphics = image.getGraphics();
Graphics2D g = (Graphics2D) graphics;
g.setColor(Color.RED);
g.drawLine(0, 0, x, y);
x -= 4;
y += 2;
g.dispose();
imageView.repaint();
} // end of jButton1_ActionPerformed
public void jButton2_ActionPerformed(ActionEvent evt) {
// TODO add your code here
} // end of jButton2_ActionPerformed
} // end of class Try_Panel

Categories

Resources