Results 1 to 12 of 12
Thread: Custom control does not display
- 11-20-2010, 08:01 PM #1
Custom control does not display
Hi,
I've got a custom control that doesn't display, and I can't figure out why. (I need a custom control because it has to move about the screen.) Here's the code:
Java Code:package view.JAccount; import java.awt.Color; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; import java.io.Serializable; import javax.swing.JComponent; public class JAccount extends JComponent implements Serializable { private Integer accountID; private AnEllipse shape; private Text text; public JAccount(Integer accountID) { this.setAccountID(accountID); MovingAdapter ma = new MovingAdapter(); addMouseMotionListener(ma); addMouseListener(ma); shape = new AnEllipse(150, 70, 80, 80); text = new Text(150, 70); setDoubleBuffered(true); } /** * Get the value of accountID * * @return the value of accountID */ public Integer getAccountID() { return accountID; } /** * Set the value of accountID * * @param accountID new value of accountID */ public void setAccountID(Integer accountID) { this.accountID = accountID; } @Override public void paint(Graphics g) { super.paint(g); Graphics2D g2d = (Graphics2D) g; g2d.setColor(new Color(142, 151, 225)); g2d.fill(shape); } class AnEllipse extends Ellipse2D.Float { public AnEllipse(float x, float y, float width, float height) { setFrame(x, y, width, height); } public boolean isHit(float x, float y) { if (getBounds2D().contains(x, y)) { return true; } else { return false; } } public void addX(float x) { this.x += x; } public void addY(float y) { this.y += y; } public void addWidth(float w) { this.width += w; } public void addHeight(float h) { this.height += h; } } class Text extends JComponent { int x; int y; public Text(int x, int y) { Graphics g = this.getGraphics(); g.setColor(Color.black); g.drawString(String.valueOf(accountID), x, y); } public void addX(int x) { this.x += x; } public void addY(int y) { this.y += y; } } class MovingAdapter extends MouseAdapter { private int x; private int y; @Override public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); } @Override public void mouseDragged(MouseEvent e) { int dx = e.getX() - x; int dy = e.getY() - y; if (shape.isHit(x, y)) { shape.addX(dx); shape.addY(dy); text.addX(dx); text.addY(dy); repaint(); } x += dx; y += dy; } } }
Last edited by horndinkle; 11-20-2010 at 08:37 PM. Reason: Maybe it's in the wrong place
-
For one thing you should be overriding the paintComponent method, not the paint method, but that's not what's causing your problem, and to be truthful, given all that code, I'm not quite sure what is the cause of your problems. Can you give us more information about what you're trying to achieve from a user's perspective rather than from a code perspective?
- 11-20-2010, 09:11 PM #3
I'm trying to do the following:
- Get the accounts listed in a JTree (I already have that working)
- Select an account from the list
- Automatically generate an ellipse with text in it (the account ID) when the account is selected that moves around the screen
- Let them select another account (there's no limit to the number they can select)
- Draw a line between the two accounts, creating a sub-account
I'm stuck on the "generating" part. It selects all right, but it returns nothing. That I can see, anyway. Can you (or someone else) help me, please?
-
Perhaps the ellipse and account ID can be drawn in a BufferedImage, placed in an ImageIcon and this placed in an JLabel, and then place the JLabel in either a JPanel that uses null layout, allowing it to be moved anywhere or a JLayeredPane.
-
Also, you should almost never call getGraphics to get a JComponent's Graphics object.
edit: There are other significant issues on further review of your code. What is the Text class for? It doesn't seem to serve a purpose and you never seem to add an object of it to a visible component ever. Also, have you gone through the Swing tutorials on graphics? If not, have a look as there's much to be learned there.Last edited by Fubarable; 11-20-2010 at 09:31 PM.
- 11-20-2010, 09:45 PM #6
Okay. I'll put your suggestions in my code, and I'll tell you what happens. (I hope something good!)
-
You may want to start over and implement one thing at a time, test that it works and then move on to the next only when all is working well.
-
For some reason, this problem of draggable JLabels with connecting lines intrigued me, so I had to try to make one....
Java Code:package yr2010.m11.a; import java.awt.BasicStroke; import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.Dimension; import java.awt.GradientPaint; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Point; import java.awt.RenderingHints; import java.awt.Stroke; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.util.ArrayList; import java.util.List; import java.util.Random; import javax.swing.*; public class MyEllipseLabelTest { private static void createAndShowUI() { String[] labelStrings = {"Fubarable", "World", "Solar System", "Galaxy", "Universe"}; Random random = new Random(); Dimension mainSize = new Dimension(800, 650); MainPanel mainPanel = new MainPanel(mainSize); MyMouseListener mouseAdapter = new MyMouseListener(mainPanel); for (int i = 0; i < labelStrings.length; i++) { MyEllipseLabel myELabel = new MyEllipseLabel("Hello " + labelStrings[i] + "!"); myELabel.setSize(myELabel.getPreferredSize()); int x = 20 + random.nextInt(mainSize.width - 150); int y = 20 + random.nextInt(mainSize.height - 100); myELabel.setLocation(x, y); myELabel.addMouseListener(mouseAdapter); myELabel.addMouseMotionListener(mouseAdapter); mainPanel.add(myELabel); } JFrame frame = new JFrame("MyEllipseLabel"); frame.getContentPane().add(mainPanel); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setResizable(false); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { java.awt.EventQueue.invokeLater(new Runnable() { public void run() { createAndShowUI(); } }); } } class MyEllipseLabel extends JLabel { private static final long serialVersionUID = 1L; private static final Color ELLIPSE_COLOR = new Color(200, 250, 200); private static final float STROKE_SIZE = 3f; private int ebGap = 30; // empty border gap private Ellipse2D ellipse; public MyEllipseLabel(String text) { super(text); setBorder(BorderFactory.createEmptyBorder(ebGap, ebGap, ebGap, ebGap)); } public Point getCenter() { Point p = getLocation(); Dimension d = getSize(); return new Point(p.x + d.width / 2, p.y + d.height / 2); } public boolean containsEllipse(Point p) { if (ellipse != null) { return ellipse.contains(p); } return super.contains(p); } @Override protected void paintComponent(Graphics g) { int w = getWidth(); int h = getHeight(); Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); ellipse = new Ellipse2D.Double(ebGap / 4, ebGap / 4, w - ebGap / 2, h - ebGap / 2); g2.setColor(ELLIPSE_COLOR); g2.fill(ellipse); g2.setStroke(new BasicStroke(STROKE_SIZE)); g2.setColor(Color.black); g2.draw(ellipse); super.paintComponent(g); } } class MainPanel extends JPanel { private static final long serialVersionUID = 1L; private static final Color LINE_COLOR = Color.black; private static final Stroke LINE_STROKE = new BasicStroke(4); private static final Color TOP_COLOR = Color.blue.darker(); private static final Color BOTTOM_COLOR = Color.black; private List<MyEllipseLabel> eLabelList = new ArrayList<MyEllipseLabel>(); private BufferedImage backgroundImage = null; public MainPanel(Dimension mainSize) { setLayout(null); setPreferredSize(mainSize); backgroundImage = new BufferedImage(mainSize.width, mainSize.height, BufferedImage.TYPE_INT_ARGB); Graphics2D g2 = backgroundImage.createGraphics(); g2.setPaint(new GradientPaint(0, 0, TOP_COLOR, 0, mainSize.height, BOTTOM_COLOR, false)); g2.fillRect(0, 0, mainSize.width, mainSize.height); g2.dispose(); } @Override public Component add(Component comp) { if (comp instanceof MyEllipseLabel) { eLabelList.add((MyEllipseLabel) comp); } return super.add(comp); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); if (backgroundImage != null) { g.drawImage(backgroundImage, 0, 0, null); } Graphics2D g2 = (Graphics2D) g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); if (eLabelList.size() > 1) { g2.setColor(LINE_COLOR); g2.setStroke(LINE_STROKE); for (int i = 0; i < eLabelList.size() - 1; i++) { for (int j = i + 1; j < eLabelList.size(); j++) { Point p1 = eLabelList.get(i).getCenter(); Point p2 = eLabelList.get(j).getCenter(); g2.drawLine(p1.x, p1.y, p2.x, p2.y); } } } } public List<MyEllipseLabel> getELabelList() { return eLabelList; } } class MyMouseListener extends MouseAdapter { private Point deltaLocation = null; private MainPanel mainPanel; public MyMouseListener(MainPanel mainPanel) { this.mainPanel = mainPanel; } @Override public void mousePressed(MouseEvent e) { MyEllipseLabel ellipseLabel = (MyEllipseLabel) e.getSource(); if (ellipseLabel.containsEllipse(e.getPoint())) { Point p1 = ellipseLabel.getLocation(); Point p2 = e.getLocationOnScreen(); deltaLocation = new Point(p1.x - p2.x, p1.y - p2.y); } } @Override public void mouseDragged(MouseEvent e) { if (deltaLocation == null) { return; } Point p1 = deltaLocation; Point p2 = e.getLocationOnScreen(); int x = p1.x + p2.x; int y = p1.y + p2.y; MyEllipseLabel ellipsePanel = (MyEllipseLabel) e.getSource(); ellipsePanel.setLocation(x, y); mainPanel.repaint(); } @Override public void mouseReleased(MouseEvent e) { deltaLocation = null; } }
- 11-21-2010, 06:08 PM #9
If that's what it takes, then I'm really screwed. :eek:
<sigh>
- 11-22-2010, 12:21 AM #10
I got it to work! Thanks a bunch, Fubarable!
-
Congrats! Please show us your latest code.
- 12-27-2010, 09:56 PM #12
Here's my working code
Sorry this has taken so long - I put a lot of changes in this, and it didn't work anymore. So I had to put it back, only I didn't remember what I did, etc. You know, the usual!
Java Code:package view.Account; import javax.swing.JFrame; public class Thing { public static void main(String[] args) { JFrame frame = new JFrame("Thing"); frame.add(new EllipsePanel()); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(500, 500); frame.setLocationRelativeTo(null); frame.setVisible(true); } }
Java Code:package view.Account; import java.awt.Graphics; import java.awt.Dimension; import java.awt.FlowLayout; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JList; import javax.swing.JRootPane; import javax.swing.JScrollPane; import javax.swing.ListSelectionModel; import javax.swing.event.ListSelectionEvent; import javax.swing.event.ListSelectionListener; public class SelectionPanel extends JPanel { private JList accountList; public SelectionPanel(JFrame frame) { final JFrame f = frame; this.setLayout(new FlowLayout(FlowLayout.CENTER)); accountList = new JList(new AccountListModel()); accountList.addListSelectionListener(new ListSelectionListener() { public void valueChanged(ListSelectionEvent e) { JList accounts = (JList) e.getSource(); MyString.setAccountID((Integer) accounts.getSelectedValue()); EllipsePanel ePanel = new EllipsePanel(); ePanel.setVisible(true); // do I need that? f.getContentPane().add(ePanel); repaint(); } }); accountList.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); JScrollPane pane = new JScrollPane(accountList); pane.setPreferredSize(new Dimension(200, 50)); this.setSize(getPreferredSize()); this.add(pane); } @Override public final Dimension getPreferredSize() { Dimension d = new Dimension(); d.setSize(200, 100); return d; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); } }
Java Code:package view.Account; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.RenderingHints; import javax.swing.JPanel; public class EllipsePanel extends JPanel { private BlueEllipse blueEllipse; private MyString str; public EllipsePanel() { MovingAdapter ma = new MovingAdapter(); addMouseMotionListener(ma); addMouseListener(ma); blueEllipse = new BlueEllipse(300, 200, 100, 50); str = new MyString(); setDoubleBuffered(true); } public BlueEllipse getEllipse() { return blueEllipse; } public MyString getString() { return str; } @Override public void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; Font font = new Font("Serif", Font.BOLD, 16); g2d.setFont(font); g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2d.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2d.setColor(new Color(142, 151, 225)); g2d.fill(blueEllipse); str.paintComponent(g, (int) (blueEllipse.getX() + 40), (int) (blueEllipse.getY() + 30)); } }
I set it to do the EllipsePanel instead of the SelectionPanel, because there's something still wrong with my SelectionPanel. It has to do with my valueChanged method. I think I'm doing multiple JPanels within a JFrame wrong, but I don't know how to fix it. Could anyone make some suggestions?
Similar Threads
-
What is the different between Text format display on web browser and display on midle
By Basit781 in forum CLDC and MIDPReplies: 1Last Post: 05-31-2010, 08:46 AM -
Create custom control
By Omarero in forum New To JavaReplies: 1Last Post: 03-24-2009, 05:50 PM -
How to display a list of items and on click display subitems?
By mandyj in forum New To JavaReplies: 8Last Post: 12-29-2008, 07:12 AM -
How to display information about the display device in SWT
By Java Tip in forum SWT TipsReplies: 0Last Post: 06-28-2008, 09:26 PM -
control app width based on certain control
By thebillybobjr in forum SWT / JFaceReplies: 0Last Post: 05-15-2008, 04:52 PM
Bookmarks