I just began my adventure with java. I have written an application with a panel and three buttons. But I have a problem. I want to add selection this buttons using the mouse. I mean like we have in Windows on the Desktop. I press the left mouse button and with the movement of the mouse the area selection is growing. And I have a question. Is there a specific interface in this or do I have it manualy, call the appropriate methods for event listeners and there draw transparent rectangle? Here is a picture:
I make a some progress, but i have another problem. Everything is ok but when I paint rectangle, button is repainting. I want to this button don't disapear when i paint rectangle. This is code:
import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.geom.Rectangle2D;
import java.util.Vector;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class zaznacz extends JPanel implements MouseListene开发者_StackOverflow社区r, MouseMotionListener
{
Rectangle newLine=null;
public zaznacz()
{
addMouseListener(this);
addMouseMotionListener(this);
}
static class Rectangle {
int x1,y1,x2,y2;
Rectangle(int _x1, int _y1,int _x2, int _y2){
x1=_x1;y1=_y1;x2=_x2;y2=_y2;
}
void paint(Graphics g)
{
g.drawRect(x1, y1, x2, y2);
}
}
public void mouseClicked(MouseEvent e) {
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent e) {
startPoint=e.getPoint();
setOpaque(true);
Graphics2D g2 = (Graphics2D)getGraphics();
Rectangle2D prostokat = new Rectangle2D.Double();
prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
g2.setComposite(AlphaComposite.getInstance(rule, alpha));
g2.draw(prostokat);
g2.setColor(Color.BLUE);
g2.fill(prostokat);
}
@Override
public void mouseReleased(MouseEvent e)
{
repaint();
}
Point startPoint;
@Override
public void mouseDragged(MouseEvent e) {
setOpaque(true);
Graphics2D g2 = (Graphics2D)getGraphics();
Rectangle2D prostokat = new Rectangle2D.Double();
prostokat.setFrameFromDiagonal(e.getPoint().x, e.getPoint().y,startPoint.x, startPoint.y);
g2.setComposite(AlphaComposite.getInstance(rule, alpha));
g2.draw(prostokat);
g2.setColor(Color.BLUE);
g2.fill(prostokat);
paintComponent(g2);
}
@Override
public void mouseMoved(MouseEvent e) {
}
int rule = AlphaComposite.SRC_OVER;
float alpha = 0.85F;
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable()
{
public void run()
{
zaznacz rys = new zaznacz();
JFrame frame = new JFrame();
JButton Button = new JButton("1");
JPanel panel = new JPanel();
panel.add(Button);
rys.add(panel);
frame.setSize(400,300);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
panel.setOpaque(false);
frame.add(rys);
}
});
}
}
Somebody can help me?
Edit: If you are referring to the highlighted rectangle, such as the one that appears as you drag your mouse over the desktop, then your approach would certainly be valid and feasible; I do not believe that are any features in Swing which provide an implementation of this feature. I don't know whether JDesktop supports this, but it may be worth a look.
Edit 2: Also take a look at GlassPane. It would make it much more convenient to implement what you describe than doing everything on the same pane would.
If I understand your question correctly, the effects which ensue when the button is being hovered over or pressed are controlled by the Look and Feel (L&F). You can select the Look and Feel you would like among those that are available on the host operating system by using the javax.swing.UIManager
class. Writing your own Look and Feel is a nontrivial task and usually takes several weeks of time, even for a person who is experienced in using image-editing applications and knows how to program graphical user-interfaces.
Here's an example (from this link) which shows you how to change the L&F to Nimbus:
try {
for (LookAndFeelInfo info : UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (Exception e) {
// If Nimbus is not available, you can set the GUI to another look and feel.
}
There are many others available, but it bears noting that many developers (and users) prefer to use the system's default look and feel rather than that which is default for Java Swing components.
I think the GlassPane approach is not the most appropriate in this case. It would be much better to subclass JPanel and override the paintComponent() method. This way, as you are subclassing JPanel, you are able to take control over the children also, or code some kind of custom behaviour your panel should have, probably related to the childrens.
Do the painting as follows:
@override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g.create();
//Your painting code here
g2.dispose();
}
精彩评论