When I try to repaint a transparent window, and draw a rectangle on it, the previous rectangle will stay. The goal is to select an area on your screen by clicking and moving your mouse. It'll look like this if you move your mouse for a while
By removing the transparency it works just fine.
I tried everything I could find on Stack Overflow about this topic, But I wasn't able to get it working on both Windows and Linux.
Main class
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
public class Main {
private JFrame frame;
private boolean pressing = false;
private boolean selected = false;
private ScreenSelectPanel p;
public Main() {
Dimension dim = Toolkit.getDefaultToolkit().getScreenSize();
frame = new JFrame("ScreenSelection");
frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
frame.setSize(dim);
frame.setUndecorated(true);
frame.setContentPane(p = new ScreenSelectPanel());
registerListeners();
frame.getContentPane().setBackground(new Color(255, 255, 255, 0));
frame.setBackground(new Color(255, 255, 255, 0));
frame.setLayout(new BorderLayout());
frame.setAlwaysOnTop(true);
frame.setVisible(true);
}
private void registerListeners() {
p.setFocusable(true);
p.requestFocusInWindow();
p.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
if (selected)
return;
setLoc(e);
p.repaint();
}
#Override
public void mouseMoved(MouseEvent e) {
if (selected)
return;
setLoc(e);
if (!pressing)
setStartLoc(e);
p.repaint();
}
});
p.addMouseListener(new MouseAdapter() {
#Override
public void mouseClicked(MouseEvent e) {
setLoc(e);
setStartLoc(e);
p.repaint();
}
#Override
public void mouseEntered(MouseEvent e) {
setLoc(e);
setStartLoc(e);
p.repaint();
}
});
}
public void setStartLoc(MouseEvent e) {
p.mouseStartX = e.getX();
p.mouseStartY = e.getY();
}
public void setLoc(MouseEvent e) {
p.mouseX = e.getX();
p.mouseY = e.getY();
}
public static void main(String[] args) {
new Main();
}
}
ScreenSelectPanel class
import javax.swing.*;
import java.awt.*;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;
public class ScreenSelectPanel extends JPanel {
public int mouseX = 0;
public int mouseY = 0;
public int mouseStartX = 0;
public int mouseStartY = 0;
private Color borderColor;
public ScreenSelectPanel() {
setOpaque(false);
borderColor = Color.BLACK;
}
public void setBorderColor(Color c) {
this.borderColor = c;
}
#Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D) g;
g2d.setColor(borderColor);
Rectangle rect = new Rectangle();
rect.setFrameFromDiagonal(new Point2D.Float(mouseStartX, mouseStartY), new Point2D.Float(mouseX, mouseY));
Stroke dashed = new BasicStroke(3, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0, new float[]{9}, 0);
g2d.setStroke(dashed);
g2d.drawRect(rect.x, rect.y, rect.width, rect.height);
g2d.dispose();
}
}
Thanks :)
You can't use transparency with Swing components. A transparent background causes these types of painting problems. A Swing component is either opaque or non-opaque.
Check out Backgrounds With Transparency for more information on this problem. However in this cause it is not the problem because you are trying to use full transparency on the Swing panel.
When I try to repaint a transparent window, and draw a rectangle on it, the previous rectangle will stay.
The code you posted does anything (at least on Windows). When you set a frame to be completely transparent then the MouseEvents are no longer handled by Swing and instead are handled by the application below the frame.
I made the following changes to your code and it seems to work for me:
//frame.getContentPane().setBackground(new Color(255, 255, 255, 0));
//frame.setBackground(new Color(255, 255, 255, 0));
frame.setBackground(new Color(255, 255, 255, 10));
Related
package drop_the_bit_4;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class DropTheBeat extends JFrame {
private Image screenImage;
private Graphics screenGraphic;
private ImageIcon exitButtonEnteredImage = new ImageIcon(Main.class.getResource("../images/exitButtonEntered.png"));
private ImageIcon exitButtonBasicImage = new ImageIcon(Main.class.getResource("../images/exitButtonBasic.png"));
private Image introBackground = new ImageIcon(Main.class.getResource("../images/introBackgroundTitle.jpg"))
.getImage();
private JLabel menuBar = new JLabel(new ImageIcon(Main.class.getResource("../images/menuBar.png")));
private JButton exitButton = new JButton(exitButtonBasicImage);
private int mouseX, mouseY;
public DropTheBeat() {
setUndecorated(true);
setTitle("Dynamic Beat");
setSize(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
setResizable(false);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
setBackground(new Color(0, 0, 0, 0));
setLayout(null);
exitButton.setBounds(1245, 0, 30, 30);
exitButton.setBorderPainted(false);
exitButton.setContentAreaFilled(false);
exitButton.setFocusPainted(false);
[enter image description here][1]exitButton.addMouseListener(new MouseAdapter() {
#Override
public void mouseEntered(MouseEvent e) {
exitButton.setIcon(exitButtonEnteredImage);
exitButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
Music buttonEnteredMusic = new Music("buttonEnteredMusic.mp3", false);
buttonEnteredMusic.start();
}
#Override
public void mouseExited(MouseEvent e) {
exitButton.setIcon(exitButtonBasicImage);
exitButton.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
}
#Override
public void mousePressed(MouseEvent e) {
Music buttonEnteredMusic = new Music("buttonPressedMusic.mp3", false);
buttonEnteredMusic.start();
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
System.exit(0);
}
});
add(exitButton);
menuBar.setBounds(0, 0, 1280, 30);
menuBar.addMouseListener(new MouseAdapter() {
#Override
public void mousePressed(MouseEvent e) {
mouseX = e.getX();
mouseY = e.getY();
}
});
menuBar.addMouseMotionListener(new MouseMotionAdapter() {
#Override
public void mouseDragged(MouseEvent e) {
int x = e.getXOnScreen();
int y = e.getYOnScreen();
setLocation(x - mouseX, y - mouseY);
}
});
add(menuBar);
Music introMusic = new Music("introMusic.mp3", true);
introMusic.start();
}
public void paint(Graphics g) {
screenImage = createImage(Main.SCREEN_WIDTH, Main.SCREEN_HEIGHT);
screenGraphic = screenImage.getGraphics();
screenDraw(screenGraphic);
g.drawImage(screenImage, 0, 0, null);
}
public void screenDraw(Graphics g) {
g.drawImage(introBackground, 0, 0, null);
paintComponents(g);
this.repaint();
}
}
So this is the code.
Before I make the menubar with exit icons, my background image file appears correctly. But now, everything work except the background image. It appears black like this enter image description here
But when I work on windows, everything is ok. Is there any difference between MAC and Win about the paint method?
public void paint(Graphics g) {
Don't override paint() on a JFrame. Custom painting is done by overriding paintComponent() on a JPanel and then you add the panel to the frame. Read the Section from the Swing tutorial on Custom Painting for more information and working examples to get you started.
setBackground(new Color(0, 0, 0, 0));
Don't use transparent Colors for backgrounds. Swing doesn't paint them properly.
For full transparency in Swing you just use setOpaque(false) on the component
For partial transparency, see Backgrounds With Transparency for more information and a solution.
setLayout(null);
Don't use a null layout. Swing was designed to be used with layout managers. The Swing tutorial also has a section on Layout Managers.
Keep a link to the Swing tutorial handy for Swing basics.
I want to draw a disc that changes colors twice a second. The disk is drawn on a DrawPanel which extends a JPanel and in the main method the DrawPanel is added to a frame.
For the colorchanging I use a timer which works when I'm trying to change the background of the DrawPanel in the main method (what i commented out).
Can someone tell me why it doesn't work for the Graphics g object or any other suggestions?
I just copied the code from the main method and added it into the paintComponent() method, but here it doesn't work.
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
public class DrawPanel extends JPanel{
public GridBagLayout gbl;
//position and dimension
int x = 0, y = 0, width = 200, height = 200;
public DrawPanel(){
repaint();
}
public DrawPanel(GridBagLayout gridBagLayout) {
this.gbl = gridBagLayout;
}
public void paintComponent(Graphics g){
//Overwriting of old picture
super.paintComponent(g);
ActionListener action = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Random gen = new Random();
Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
//Draw color disk
g.setColor(color);
g.fillArc(x, y, width, height, 0, 360);
}
};
Timer t = new Timer(500, action);
t.setRepeats(true);
t.setInitialDelay(0);
t.start();
//Draw boundary of circle
g.setColor(Color.BLACK);
g.drawArc(x, y, width, height, 0, 360);
}
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(300, 300);
final DrawPanel panel = new DrawPanel();
panel.setOpaque(true);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
// ActionListener action = new ActionListener() {
// #Override
// public void actionPerformed(ActionEvent e) {
// Random gen = new Random();
// Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
// panel.setBackground(color);
// }
// };
//
// Timer t = new Timer(500, action);
// t.setRepeats(true);
// t.setInitialDelay(0);
// t.start();
}
}
The Graphics object is transient, so you should not cache it even if the compiler allows that. Instead establish the timer in the constructor of the class, set the BG of the panel, then call for a repaint. E.G.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Random;
public class DrawPanel extends JPanel {
Random gen = new Random();
//position and dimension
int x = 0, y = 0, width = 200, height = 200;
Color drawColor = Color.BLACK;
public DrawPanel() {
repaint();
ActionListener action = new ActionListener() {
#Override
public void actionPerformed(ActionEvent e) {
Color color = new Color(gen.nextInt(256), gen.nextInt(256), gen.nextInt(256));
//Draw color disk
drawColor = color;
DrawPanel.this.repaint();
}
};
Timer t = new Timer(500, action);
t.setRepeats(true);
t.setInitialDelay(0);
t.start();
}
#Override
public void paintComponent(Graphics g) {
//Overwriting of old picture
super.paintComponent(g);
//Draw boundary of circle
g.setColor(drawColor);
g.drawArc(x, y, width, height, 0, 360);
}
public static void main(String[] args) {
final JFrame frame = new JFrame();
frame.setSize(300, 300);
final DrawPanel panel = new DrawPanel();
panel.setOpaque(true);
frame.getContentPane().add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
Graphics objects are only valid for that one draw
It would be better to instead to tell the JPanel to change it's current color (with a variable) and then tell it to repaint
Add variable discColor to your JPanel
Change your drawing code to not use timers and instead make it simple, just draw the disc based off of discColor
With a timer, update the discColor variable and then call the panel's repaint() method
I am using a quite basic setup with a class extending JPanel, which I add to a JFrame.
import java.awt.*;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;
import java.io.*;
import javax.imageio.ImageIO;
public class PinTestMCVE extends JPanel implements ActionListener{
BufferedImage loadedImage;
JButton calcButton;
public static void main(String[] args) {
new PinTestMCVE();
}
public PinTestMCVE() {
loadedImage = getTestImage();
JPanel toolbarPanel = new JPanel();
calcButton = new JButton("calcButton...");
toolbarPanel.add(calcButton);
calcButton.addActionListener(this);
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.getContentPane().setLayout(new BorderLayout());
jf.getContentPane().add(toolbarPanel, BorderLayout.NORTH);
jf.getContentPane().add(this, BorderLayout.CENTER);
jf.setSize(1250, 950);
jf.setVisible(true);
}
public void paintComponent(Graphics g) {
g.drawImage(loadedImage, 0, 0, this);
}
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent " + e.getActionCommand());
if(e.getSource().equals(calcButton)){
this.repaint();
}
}
//Please ignore the inner workings of this
public static BufferedImage getTestImage(){
BufferedImage image = new BufferedImage(500, 500, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(Color.GRAY);
g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.gray);
int x = 5;
int y = 7;
GradientPaint redtowhite = new GradientPaint(x, y, Color.red, 200, y, Color.blue);
g2d.setPaint(redtowhite);
g2d.fill(new RoundRectangle2D.Double(x, y, 200, 200, 10, 10));
return image;
}
}
What happens is that INITIALLY the window is painted properly, but once paintComponent is called, a strip of the old image (with the same height as the toolbar panel) is visible below the newly painted images - similar to playing card sticking out from a deck. But then, if I manually resize the window by for instance dragging the border, the background is grayed out as it should.
What is going on and how do I fix this?
As outlined here, you need to pack() the frame before calling setVisible(). You can override getPreferredSize() to specify a suitable initial Dimension. Also consider using a Border. See also Initial Threads.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.*;
public class PinTestMCVE extends JPanel implements ActionListener{
private static final int SIZE = 200;
BufferedImage loadedImage;
JButton calcButton;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
new PinTestMCVE();
}
});
}
public PinTestMCVE() {
loadedImage = getTestImage();
JPanel toolbarPanel = new JPanel();
calcButton = new JButton("calcButton...");
toolbarPanel.add(calcButton);
calcButton.addActionListener(this);
JFrame jf = new JFrame();
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jf.add(toolbarPanel, BorderLayout.NORTH);
jf.add(this, BorderLayout.CENTER);
jf.pack();
jf.setLocationRelativeTo(null);
jf.setVisible(true);
}
#Override
public Dimension getPreferredSize() {
return new Dimension(SIZE, SIZE);
}
#Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
g.drawImage(loadedImage, 0, 0, this);
}
#Override
public void actionPerformed(ActionEvent e) {
System.out.println("ActionEvent " + e.getActionCommand());
if(e.getSource().equals(calcButton)){
this.repaint();
}
}
//Please ignore the inner workings of this
public static BufferedImage getTestImage(){
BufferedImage image = new BufferedImage(SIZE, SIZE, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(Color.GRAY);
g2d.fillRect ( 0, 0, image.getWidth(), image.getHeight() );
g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2d.setPaint(Color.gray);
GradientPaint redtowhite = new GradientPaint(5, 5, Color.red, SIZE, 5, Color.blue);
g2d.setPaint(redtowhite);
g2d.fill(new RoundRectangle2D.Double(5, 5, SIZE - 10, SIZE - 10, 10, 10));
return image;
}
}
I am currently writing a Java painting program using the Swing libraries and Graphics2D to paint. My main GUI class extends JComponent, and I am trying to put it inside a JPanel, and the JPanel inside a JFrame, in order to show it on the screen. When starting up the program though, the JComponent appears to be just a black line (the border, which is set to be a black line around the component). I cannot see why this is happening, and I've been debugging it for hours. If somebody could find the error in this program, I'd be very happy. Thanks in advance.
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PaintGUI extends JComponent {
private static final long serialVersionUID = 1L;
JButton red, green, blue, clear;
Image img;
Graphics2D gfx;
JFrame drawFrame;
JPanel btnPan, drawPan;
MyListener ml;
Action act;
int x, y, prevX, prevY;
public PaintGUI(){
//Initialisering av panel, frame og content
drawFrame = new JFrame("IFIPaint");
drawFrame.setLayout(new BorderLayout());
btnPan = new JPanel();
drawPan = new JPanel();
btnPan.setLayout(new FlowLayout());
drawPan.setLayout(new BorderLayout());
this.setEnabled(true);
//Setter størrelser
btnPan.setPreferredSize(new Dimension(30, 60));
btnPan.setMinimumSize(new Dimension(30, 60));
btnPan.setMaximumSize(new Dimension(30, 60));
//Ordner knappene
red = new JButton("Rød");
green = new JButton("Grønn");
blue = new JButton("Blå");
clear = new JButton("Slett alt");
//Putter knappene på panelet
btnPan.add(red);
btnPan.add(green);
btnPan.add(blue);
btnPan.add(clear);
//Legger på action listeners
act = new Action();
red.addActionListener(act);
green.addActionListener(act);
blue.addActionListener(act);
clear.addActionListener(act);
//Fullfører vindu og setter synlighet
drawFrame.setSize(500, 500);
drawPan.setBounds(0, 0, 400, 400);
this.setBounds(0, 0, 400, 400);
drawPan.add(this);
this.setBackground(Color.RED);
drawFrame.add(drawPan, BorderLayout.NORTH);
drawFrame.add(btnPan, BorderLayout.SOUTH);
this.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.setVisible(true);
drawPan.setVisible(true);
btnPan.setVisible(true);
drawFrame.setVisible(true);
this.paintComponent(gfx);
drawFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
draw();
}
public void draw() {
ml = new MyListener();
this.addMouseListener(ml);
this.addMouseMotionListener(ml);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
if(img == null){
img = createImage(this.getWidth(),this.getHeight());
gfx = (Graphics2D) img.getGraphics();
gfx.setPaint(Color.RED);
gfx.fillRect(0, 0, this.getSize().width, this.getSize().height);
gfx.setPaint(Color.RED);
gfx.dispose();
}
gfx.drawImage(img, 0, 0, this);
}
class Action implements ActionListener {
public void actionPerformed(ActionEvent e) {
if(e.getSource() == red){
gfx.setPaint(Color.RED);
repaint();
} else if (e.getSource() == green){
gfx.setPaint(Color.GREEN);
repaint();
} else if (e.getSource() == blue) {
gfx.setPaint(Color.BLUE);
repaint();
} else if (e.getSource() == clear) {
gfx.clearRect(0, 0, drawFrame.getWidth(), drawFrame.getHeight());
repaint();
}
}
}
class MyListener extends MouseAdapter {
public void mousePressed(MouseEvent e) {
prevX = e.getX();
prevY = e.getY();
System.out.println("o ye");
}
public void mouseDragged(MouseEvent e) {
x = e.getX();
y = e.getY();
gfx.drawLine(prevX, prevY, x, y);
repaint();
prevX = x;
prevY = y;
}
}
}
You added the PaintGUI to frame but LayoutManager don't know the size and can't set desired size.
Either set the preferred size (or override getPreferredSize to return desired size)
or add to the PaintGUI instance some component(s) with preferred size (e.g. buttons)
I try to make a window with a resize handle at the bottom right.
So far I managed to detect the mouse hover and dragging.
The mouse cursor is changing to resize cursor successfully.
But the actually resize operation I'm unsure how to solve.
The Idea I testet at first place is to just setsize on parent when dragging on resize handle.
It works, but then the window get's resize immediately, that's not how the standard resize looks.
The standard resize is a transparent window with white border (may be different on different systems and look and feel).
Is it possible to trigger/use the built in resize mechanism?
Below you have a sample code.
Thanks!
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Polygon;
import java.awt.RenderingHints;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
public class main extends JFrame {
private JPanel contentPane;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
main frame = new main();
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public class StatusBar extends JPanel implements MouseListener, MouseMotionListener {
private Polygon resizeCorner = new Polygon();
private int offsetX;
private int offsetY;
private Dimension offsetSize;
private Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
public StatusBar() {
super();
setPreferredSize(new Dimension(200,40));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
private void createResizeHandle() {
resizeCorner.reset();
resizeCorner.addPoint(getWidth()-2, getHeight()-2);
resizeCorner.addPoint(getWidth()-40, getHeight()-2);
resizeCorner.addPoint(getWidth()-2, getHeight()-40);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2;
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING,
RenderingHints.VALUE_RENDER_QUALITY);
g2.setColor(Color.red);
createResizeHandle();
g2.drawPolygon(resizeCorner);
}
#Override
public void mouseDragged(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
int width = (int) (this.offsetSize.getWidth() - this.offsetX + e.getXOnScreen());
int height = (int) (this.offsetSize.getHeight() - this.offsetY + e.getYOnScreen());
this.getRootPane().getParent().setSize(width, height);
createResizeHandle();
}
}
#Override
public void mouseMoved(MouseEvent e) {
if (resizeCorner.contains(e.getX(), e.getY())) {
setCursor(resizeCursor);
} else {
setCursor(defaultCursor);
}
}
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent e) {
if (resizeCorner.contains(e.getX(), e.getY())) {
this.offsetX = e.getXOnScreen();
this.offsetY = e.getYOnScreen();
this.offsetSize = this.getRootPane().getParent().getSize();
}
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
public main() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
setContentPane(contentPane);
JLabel lblNewLabel = new JLabel("New label");
StatusBar bar = new StatusBar();
contentPane.add(lblNewLabel, BorderLayout.NORTH);
contentPane.add(bar, BorderLayout.SOUTH);
}
}
lots of small mistakes, starts with correct naming for ClassName ... end with usage java reserved words for class/void/method's name(s)
minor changes (now it works for me), with one my mistake against Swing rules, I set there setPreferredSize(new Dimension(400, 300));, lets childrens returns PreferredSize for Container
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
public class MyToolBar extends JFrame {
private static final long serialVersionUID = 1L;
private JPanel contentPane;
public class StatusBarX extends JPanel implements MouseListener, MouseMotionListener {
private static final long serialVersionUID = 1L;
private Polygon resizeCorner = new Polygon();
private int offsetX;
private int offsetY;
private Dimension offsetSize;
private Cursor resizeCursor = new Cursor(Cursor.SE_RESIZE_CURSOR);
private Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
public StatusBarX() {
super();
setPreferredSize(new Dimension(200, 40));
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
private void createResizeHandle() {
resizeCorner.reset();
resizeCorner.addPoint(getWidth() - 2, getHeight() - 2);
resizeCorner.addPoint(getWidth() - 40, getHeight() - 2);
resizeCorner.addPoint(getWidth() - 2, getHeight() - 40);
}
#Override
public void paint(Graphics g) {
super.paint(g);
Graphics2D g2;
g2 = (Graphics2D) g;
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
g2.setColor(Color.red);
createResizeHandle();
g2.drawPolygon(resizeCorner);
}
#Override
public void mouseDragged(MouseEvent e) {
if ((e.getModifiers() & InputEvent.BUTTON1_MASK) != 0) {
int width = (int) (this.offsetSize.getWidth() - this.offsetX + e.getXOnScreen());
int height = (int) (this.offsetSize.getHeight() - this.offsetY + e.getYOnScreen());
this.getRootPane().getParent().setSize(width, height);
createResizeHandle();
}
}
#Override
public void mouseMoved(MouseEvent e) {
if (resizeCorner.contains(e.getX(), e.getY())) {
setCursor(resizeCursor);
} else {
setCursor(defaultCursor);
}
}
#Override
public void mouseClicked(MouseEvent arg0) {
}
#Override
public void mouseEntered(MouseEvent arg0) {
}
#Override
public void mouseExited(MouseEvent arg0) {
}
#Override
public void mousePressed(MouseEvent e) {
if (resizeCorner.contains(e.getX(), e.getY())) {
this.offsetX = e.getXOnScreen();
this.offsetY = e.getYOnScreen();
this.offsetSize = this.getRootPane().getParent().getSize();
}
}
#Override
public void mouseReleased(MouseEvent arg0) {
}
}
public MyToolBar() {
JLabel lblNewLabel = new JLabel("New label");
StatusBarX bar = new StatusBarX();
contentPane = new JPanel();
contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
contentPane.setLayout(new BorderLayout(0, 0));
contentPane.add(lblNewLabel, BorderLayout.NORTH);
contentPane.add(bar, BorderLayout.SOUTH);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(100, 100);
setPreferredSize(new Dimension(400, 300));
add(contentPane);
pack();
setVisible(true);
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
#Override
public void run() {
try {
MyToolBar frame = new MyToolBar();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
}
The standard resize is a transparent window with white border (may be different on different systems and look and feel).
When using Windows XP the standard is to resize top level components (frame, dialog etc) immediately. It doesn't matter what the LAF is.
In general other components don't have resizing functionality built in. The only exception to this that I can think of is JInternalFrame. In this case is does support the "outline" resizing of a component.
So if you want to add this type of functionality to your component then you would need to look at the internal frame UI to find the resizing code.
I would guess that the code would display a Glass Pane when dragging starts and then do the outline custom painting on the Glass Plane. Then on mouseReleased the frame size would be changed.
In case you are interested, Component Resizing shows how I do resizing for any component. It does immediate resizing, not outline resizing.