I tried to make a custom swing component. I managed to get it to draw it right. However, there is a problem. It still has a rectangle border. I want it to have the border of the shape passed to it.
I KNOW there HAS to be some way of making a border other than a rectangle. (Otherwise, they wouldn't be able to have JRadioButton.)
I've tried paintBorder and even tried messing with the ComponentUI. Nothing.
I know there probably is a way to set the thing so that it would only handle an event if I clicked within the bounds that I painted. However, that doesn't deal with the fact that it's still using a rectangle border and hence would still be taking up the extra space.
(I think I could handle the clicking thing by using contains() in Shape and checking, using a MouseListener or something, to see if the point clicked is inside said shape.)
I'm trying to make a triangle component. It's got the triangle form, but, as you can see, as long as you click inside the rectangular bounds, it's still printing "Click".
It was hard enough to find something online to help me draw the new component. Finding something that helps reshape the border has proven fruitless so far.
I was more or less going for a shaped button (but doing it as a JComponent subclass could make it apply to more things so I'm keeping it JComponent for now.)
However, I hate this rectangular bounds.
I found this code example online. It was drawing something else. However, I don't think it modified the borders either. I made it so that it would draw a shape.
However, making the border stay with the shape is proving elusive.
package javaswing; import java.awt.Dimension; import java.awt.Graphics; import javax.swing.Box; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.SwingUtilities; import java.awt.event.MouseListener; import java.awt.event.MouseEvent; /** * * @web [url=http://java-buddy.blogspot.com/]Java-Buddy[/url] */ public class JavaTestSwing { static JFrameWin jFrameWindow; public static class MyComponent extends JComponent implements MouseListener{ // Dimension myDimension = new Dimension(300, 100); private java.awt.Shape shape; private class MyComponentUI extends javax.swing.plaf.ComponentUI { private java.awt.Shape shape; public MyComponentUI(java.awt.Shape shape) { this.shape = shape; } @Override public void paint(Graphics g, JComponent c) { java.awt.Graphics2D g2d = (java.awt.Graphics2D) g; g2d.draw(shape); } } public MyComponent(java.awt.Shape shape) { this.shape = shape; addMouseListener(this); setBorder(new javax.swing.border.EtchedBorder(java.awt.Color.RED, java.awt.Color.BLUE)); setUI(new MyComponentUI(shape)); } /* @Override public Dimension getPreferredSize() { return myDimension; } @Override public Dimension getMaximumSize() { return myDimension; } @Override public Dimension getMinimumSize() { return myDimension; } */ @Override protected void paintBorder(Graphics g) { java.awt.Graphics2D g2d = (java.awt.Graphics2D) g; g2d.draw(shape); } @Override protected void paintComponent(Graphics g) { /* g.drawLine(0, 0, getWidth()-1, getHeight()-1); g.drawLine(0, getHeight()-1, getWidth()-1, 0); g.drawRect(0, 0, getWidth()-1, getHeight()-1); */ /* g.drawLine(0, 0, 100, 100); g.drawLine(100, 100, 0, 200); g.drawLine(0, 200, 0, 0); */ java.awt.Graphics2D g2d = (java.awt.Graphics2D) g; g2d.draw(shape); } public void mouseClicked(MouseEvent e) { System.out.println("Clicked"); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mousePressed(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } } public static class JFrameWin extends JFrame{ public JFrameWin(){ this.setTitle("java-buddy.blogspot.com"); this.setSize(350, 250); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); java.awt.Polygon poly = new java.awt.Polygon(); poly.addPoint(0, 0); poly.addPoint(100, 100); poly.addPoint(0, 200); MyComponent myComponent1 = new MyComponent(poly); MyComponent myComponent2 = new MyComponent(poly); Box verticalBox = Box.createVerticalBox(); verticalBox.add(myComponent1); verticalBox.add(myComponent2); this.add(verticalBox); } } public static void main(String[] args){ Runnable doSwingLater = new Runnable(){ public void run() { jFrameWindow = new JFrameWin(); jFrameWindow.setVisible(true); } }; SwingUtilities.invokeLater(doSwingLater); } }
I know there has to be a way to do it. You can do it with JFrame too.
(P.S., I tried the paintBorder method. Doesn't do it.)