Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-25-2008, 12:57 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
Scaling-ache and mouse dragging
Scaling-ache and mouse dragging

Imagine a class that inherences of JPanel (that’s the way to say it, I believe). I’d like to get a frame (drawn in a graphical context in a central position of the panel. The panel will be added in another class (details I´ll discuse some later). But the code I wrote is acting strangely:

public void setframeSize (int framew, int frameh) {
this.framew = framew; // the frame size is set in other class
this.frameh = frameh;
framex = getWidth()/2 - (framew/2);
framey = getHeight()/2 - (frameh/2);


}


public void drawframe(Graphics g) {
g.setColor(Color.RED);
g.drawRect(framex, framey, framew, frameh);
g.drawRect(framex-1, framey-1, framew, frameh);
g.drawRect(framex-2, framey-2, framew, frameh);
}

The getWidth()/getHeight() should deliver the size of the JPanel, Correct?
But the only way to make the frame (the size of the applet) appear correctly is to add the code in bold to the second method which is part of the same class. I do not understand why that is, because both methods take through get() the size from the same JPanel, or not?

(once the frame is set, the dragging will follow...have the code working allready but still some stuff to solve, more questions will follow)

willemjav
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 06-25-2008, 01:25 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
small addition:
the constructor of the class sets the pref size of the JPanel
public Editimages() {

imagepanelX = 800;
imagepanelY = 500;
setPreferredSize(new Dimension(imagepanelX, imagepanelY));

etc.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 06-25-2008, 01:52 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.MouseInputAdapter; public class FramingTest extends JPanel { int framex; int framey; int framew; int frameh; public void setFrame(Point p1, Point p2) { framew = Math.abs(p1.x - p2.x); frameh = Math.abs(p1.y - p2.y); // Center frame in this component view. // But this won't look right while dragging. //framex = (getWidth() - framew)/2; //framey = (getHeight() - frameh)/2; // Calculate x and y for more intuitive // and accurate display of dragged shape: framex = (p1.x < p2.x) ? p1.x : p2.x; framey = (p1.y < p2.y) ? p1.y : p2.y; repaint(); } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setStroke(new BasicStroke(3f)); g2.setColor(Color.RED); g2.drawRect(framex, framey, framew, frameh); } public static void main(String[] args) { FramingTest test = new FramingTest(); Framer framer = new Framer(test); test.addMouseListener(framer); test.addMouseMotionListener(framer); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } } class Framer extends MouseInputAdapter { FramingTest component; Point start; boolean dragging = false; public Framer(FramingTest test) { component = test; } public void mousePressed(MouseEvent e) { start = e.getPoint(); } public void mouseDragged(MouseEvent e) { component.setFrame(start, e.getPoint()); } }
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 06-25-2008, 01:59 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
Yes I noticed that when dragging things get worse
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 06-25-2008, 02:03 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
but getWidth() provides the width of the active component which is JPanel or not? By putting the getWidth() at different ¨places¨ in the class (different methods) gives different results which makes no since to me.
Bookmark Post in Technorati
Reply With Quote
  #6 (permalink)  
Old 06-25-2008, 02:06 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
I am figuring out a dragging position algoritme (difficult word to write)
myself but of course study yours carefully
ops this is ofcourse not a java chat
Bookmark Post in Technorati
Reply With Quote
  #7 (permalink)  
Old 06-25-2008, 02:08 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
good nigth from spain
Bookmark Post in Technorati
Reply With Quote
  #8 (permalink)  
Old 06-25-2008, 09:00 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
Timing
Timing is an important issue. The two framesize variables framew/ frameh weren´t set at the right time. So I learned that the constructor of the second class is the place to trigger some important things like the frame size of the applet. The framesize enters now as parameter of the constructor. The constructor is called first when creating a object of that class. I noticed that I can not use the getWidth() in the constructor (probably the main component JPanel is not yet set?).

public Editimages(int framew, int frameh) { // the second class consructor

imagepanelX = 800;
imagepanelY = 500;
setPreferredSize(new Dimension(imagepanelX, imagepanelY));
this.framew = framew;
this.frameh = frameh;
framex = imagepanelX/2 - (framew/2);
framey = imagepanelY/2 - (frameh/2);
infotextPanel = new JTextArea();
infotextPanel.setEditable(false);
infotextPanel.setLineWrap(true);
infotextPanel.setMinimumSize(new Dimension(50, 50));
infotextPanel.setMaximumSize(new Dimension(50, 50));
infotextPanel.setOpaque(false);
setLayout(null);
add(infotextPanel);
addMouseListener(this);
addMouseMotionListener(this);

etc.

The above delivers the framew/h as the fixed size and the framex/h as the fixed central position. These four variables could be the basis of the dragging?
Probably not… I’ll continuo studying hardwire´s code.

Bytheway I learn by trail and error. The small panel

JOptionPane.showMessageDialog(infoPane, "x y " + framex +" / "+ framey + " frame w/h " + frameh + " / " + framew);

Is extremely useful to me. I plug this little code in where ever I want to stop the program and to see what’s going on at that specific spot. I know there are more sophisticated and professional ways…. But for the moment, hardwired, this works for me.

willemjav
Bookmark Post in Technorati
Reply With Quote
  #9 (permalink)  
Old 06-27-2008, 01:07 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
my dragging code
Here than my dragging code.
It works but it is a little stiff
compared to the fluid dragging
of Hardwired´s code

public void mousePressed(MouseEvent evt) {
dragging = false;
prevX = evt.getX(); // Current position of Mouse.
prevY = evt.getY();
}



public void mouseDragged(MouseEvent evt) {
if (prevX>frameMovX && prevX<(frameMovX+framew) && prevY>frameMovY
&& prevY<(frameMovY+frameh) && framelock) {
dragging = true; // true when dragging occurs
}
}

public void mouseReleased(MouseEvent evt) {

if (dragging) {

frameMovX = evt.getX() - prevX + frameMovX; // dragging stopped position of Mouse.
frameMovY = evt.getY() - prevY + frameMovY; // calculating the new position from
repaint(); // starting from the centre position
dragging = false; // waiting for new drag
}
}

public void mouseMoved(MouseEvent evt) {}
public void mouseClicked(MouseEvent evt) {}
public void mouseEntered(MouseEvent evt) {}
public void mouseExited(MouseEvent evt) {}



public void drawframe(Graphics g) {
g.setColor(Color.RED);
if (!framelock) {
frameMovX = framex;
frameMovY = framey;
}

g.drawRect(frameMovX, frameMovY, framew, frameh);
g.drawRect(frameMovX-1, frameMovY-1, framew, frameh);
g.drawRect(frameMovX-2, frameMovY-2, framew, frameh);
}



protected void paintComponent(Graphics g) {
// This next line will fill component background
// with background color specified in constructor
// above.
super.paintComponent(g);

int x = getWidth();
int y = getHeight();

int xpic = (x - newwidth)/2;
int ypic = (y - newheight)/2;

//infotextPanel.setText(x + " / " + y);
if(image != null) {
g.drawImage(pic, xpic, ypic, this);
}
drawframe(g);
}



public void drawinfotext (String infotext, String txtcolor, String txtsize,
int infotextx, int infotexty, int rw, int cl) {
//infotextPanel.setSize(300,300);
//setLocation(infotextx,infotexty);
int offset = lineLength(infotext);
settextColor(txtcolor);
settextSize(txtsize);
infotextx = infotextx + framex + (framew/2) - offset;
infotexty = infotexty + framey + frameh - 20;
infotextPanel.setBounds(infotextx,infotexty, framew ,frameh);
infotextPanel.setText(infotext);
}


public int lineLength(String str) { // this will centre the info text
int result=0;
double sc = 7.4;
if (!str.contains("\n"))
result = str.length()/2;
else
result = str.indexOf("\n")/2;
return (int) (result*sc);
}


public void displayImage(double sc) {
this.setBackground(Color.WHITE);
if (sc == 0) sc = 1.0;

newwidth = (int)(imgwidth*sc);
newheight = (int)(imgheight*sc);

pic = image.getScaledInstance(newwidth, newheight, Image.SCALE_SMOOTH);

revalidate();
repaint();
}
Bookmark Post in Technorati
Reply With Quote
  #10 (permalink)  
Old 06-28-2008, 12:43 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
next step dragging a text component
adding this code will keep the frame inside the JPanel

public void drawframe(Graphics g) {
g.setColor(Color.RED);
if (!framelock) { // frame lock
frameMovX = framex;
frameMovY = framey;
}
if (frameMovX<0) // frame stays inside the borders
frameMovX=0;
if (frameMovY<0)
frameMovY=0;
if (frameMovX>(imagepanelX-framew))
frameMovX=imagepanelX-framew;
if (frameMovY>(imagepanelY-frameh))
frameMovY=imagepanelY-frameh;

The next step is to drag a component (JTextArea, which displays some info text lines over the image). I´d like to find the write position of the text over the image. The component is added to JPanel where the frame dragging takes place.
1) I needed to add extra mouse listeners on the JTextArea.
2) I need to know when the JTextArea is selected for dragging (by yet another listeners or by position coordinates that mark the contours of the component e.g. int height = infotextPanel.getSize().height etc. similar to the frame dragging of before.
3) The position change can be done by infotextPanel.setBounds(infotextx,infotexty, w , h); infotextx set by the drag.
For the moment I do not know how to handle 2). Once I have the mouse noticed over the JTextArea dragging can start (the setOpaque() is set to false.

willemjav
Bookmark Post in Technorati
Reply With Quote
  #11 (permalink)  
Old 06-28-2008, 12:44 AM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
write position = right position (sorry)
Bookmark Post in Technorati
Reply With Quote
  #12 (permalink)  
Old 06-28-2008, 07:34 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
You can call setBounds on your textArea after dragging the frame rectangle, possibly in the mouseReleased method or in the ActionListener of a button.
Here's another approach using graphics that can allow more flexibility and greater accuracy.
Code:
import java.awt.*; import java.awt.event.*; import java.awt.font.*; import java.awt.image.BufferedImage; import java.io.*; import java.text.*; import javax.imageio.ImageIO; import javax.swing.*; import javax.swing.event.*; public class ImageEditor extends JPanel implements ActionListener { Framer framer = new Framer(this); JTextField editor; String text; int size = 24; BufferedImage source; BufferedImage scaled; Rectangle rect = new Rectangle(); boolean showFrame = true; double scale = 1.0; public ImageEditor(BufferedImage image) { source = image; scaleImage(); } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("SAVE")) { save(); } else { if(ac.equals("SHOW FRAME")) showFrame = ((JCheckBox)e.getSource()).isSelected(); if(ac.equals("SET TEXT")) { text = editor.getText(); } repaint(); } } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_PURE); int w = getWidth(); int h = getHeight(); int x = (w - scaled.getWidth())/2; int y = (h - scaled.getHeight())/2; g2.drawImage(scaled, x, y, this); g2.setPaint(Color.red); if(showFrame) { g2.draw(rect); } if(!framer.isDragging()) { drawText(g2); } } public Dimension getPreferredSize() { return new Dimension(500,500); } private void drawText(Graphics2D g2) { if(text == null || text.length() < 1) return; int pad = 10; int wrappingWidth = rect.width - 2*pad; FontRenderContext frc = g2.getFontRenderContext(); AttributedString as = new AttributedString(text); as.addAttribute(TextAttribute.SIZE, Integer.valueOf(size)); AttributedCharacterIterator aci = as.getIterator(); LineBreakMeasurer lbm = new LineBreakMeasurer(aci, frc); float x = rect.x + pad; float y = rect.y + pad; while(lbm.getPosition() < text.length()) { TextLayout layout = lbm.nextLayout(wrappingWidth); //layout = layout.getJustifiedLayout(wrappingWidth); y += layout.getAscent(); layout.draw(g2, x, y); y += layout.getDescent() + layout.getLeading(); } } public void setFrame(Point p1, Point p2) { rect.setFrameFromDiagonal(p1, p2); repaint(); } private void save() { int w = getWidth(); int h = getHeight(); int iw = scaled.getWidth(); int ih = scaled.getHeight(); int x = (w - iw)/2; int y = (h - ih)/2; int type = BufferedImage.TYPE_INT_RGB; BufferedImage toSave = new BufferedImage(iw, ih, type); Graphics2D g2 = toSave.createGraphics(); g2.translate(-x, -y); paint(g2); g2.dispose(); String ext = "jpg"; String path = "imageEditor." + ext; try { ImageIO.write(toSave, ext, new File(path)); } catch(IOException e) { System.out.println("write error: " + e.getMessage()); } ImageIcon icon = new ImageIcon(toSave); JOptionPane.showMessageDialog(null, icon, "", -1); } private void scaleImage() { int w = (int)(scale*source.getWidth()); int h = (int)(scale*source.getHeight()); int type = BufferedImage.TYPE_INT_RGB; scaled = new BufferedImage(w, h, type); Graphics2D g2 = scaled.createGraphics(); g2.drawImage(source, 0, 0, w, h, this); g2.dispose(); } private JPanel getEditorPanel() { JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.weightx = 1.0; gbc.fill = GridBagConstraints.HORIZONTAL; gbc.gridwidth = GridBagConstraints.REMAINDER; panel.add(getEditor(), gbc); panel.add(getControls(), gbc); return panel; } private JTextField getEditor() { editor = new JTextField(); return editor; } private JPanel getControls() { final SpinnerNumberModel model = new SpinnerNumberModel(size,16,48,1); JSpinner spinner = new JSpinner(model); model.addChangeListener(new ChangeListener() { public void stateChanged(ChangeEvent e) { size = model.getNumber().intValue(); repaint(); } }); String[] ids = { "show frame", "set text", "save" }; JPanel panel = new JPanel(new GridBagLayout()); GridBagConstraints gbc = new GridBagConstraints(); gbc.insets = new Insets(2,2,2,2); gbc.weightx = 1.0; for(int i = 0; i < ids.length; i++) { AbstractButton button = (i == 0) ? new JCheckBox(ids[i], showFrame) : new JButton(ids[i]); button.setActionCommand(ids[i].toUpperCase()); button.addActionListener(this); if(i == 2) { gbc.anchor = GridBagConstraints.EAST; panel.add(new JLabel("font size"), gbc); gbc.anchor = GridBagConstraints.WEST; panel.add(spinner, gbc); gbc.anchor = GridBagConstraints.CENTER; } panel.add(button, gbc); } return panel; } public static void main(String[] args) throws IOException { String path = "images/bison.jpg"; BufferedImage image = ImageIO.read(new File(path)); ImageEditor test = new ImageEditor(image); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.add(test); f.add(test.getEditorPanel(), "Last"); f.pack(); f.setLocation(200,200); f.setVisible(true); } } class Framer extends MouseInputAdapter { ImageEditor editor; Point start; boolean dragging = false; public Framer(ImageEditor ie) { editor = ie; editor.addMouseListener(this); editor.addMouseMotionListener(this); } public void mousePressed(MouseEvent e) { start = e.getPoint(); dragging = true; } public void mouseReleased(MouseEvent e) { dragging = false; } public void mouseDragged(MouseEvent e) { if(dragging) { editor.setFrame(start, e.getPoint()); } } public boolean isDragging() { return dragging; } }
Bookmark Post in Technorati
Reply With Quote
  #13 (permalink)  
Old 06-28-2008, 07:55 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
thick as a brick
Hardwired, I taught I was almost there, I just needed a small detail more to get my component dragging…. but what I got instead is this enormous thick brick falling over me……. I started studying your code…. you do not even use a JTextArea … it is overwhelming.....
The frame and the image draw are done in a graphical context, why not the text display as well…. that makes since. The problem is that graphical programming is more difficult and some times way over my head.

The frame dragging was relatively easy to do and the text component dragging, which happens independently from the frame dragging, should be not very different, I taught. Let me explain what goes on in my head. There are three states:
1) mouse clicked, get: start-position;
2) mouse dragged, set: Boolean drag true;
3) mouse released, get: end-position;
So the new position = componentposition + endposition – startpostion.
And the setBounds() is ideal to redirect the component.

But there is actually a difference when dragging the frame compared to a component. The frame gets drawn into the JPanel and the motion listener is registered on the panel. A component is added to the panel and forms a “second layer”, when moving the mouse over the component it stops informing (so you have to register the component as well!).

I have the JTextArea-dragging going, but the silly thing is that I have the component moving by dragging any where on the Jpanel (of course the independent frame dragging gets blocked by a logical or, because I can´t have both moving around that would be pretty messy).

But I´d like the component only to move when the drag starts by clicking on the component itself (there should be a "simple" if-statement like, if JTextArea is selected, included at point 2). Text components do have a bunch of listeners but they deal with text manipulation and other things. The suitable EventListener does not work with JTextArea (I´d look into the addContainerListener).

willemjav
Bookmark Post in Technorati
Reply With Quote
  #14 (permalink)  
Old 06-30-2008, 03:19 AM
Senior Member
 
Join Date: Jul 2007
Posts: 1,124
hardwired is on a distinguished road
Sorry for the thick brick.
When you try to drag something like a text component in its parent container you run into trouble. One of the difficulties is that it consumes (uses) MouseEvents. Another is that mouseListeners added to the component have an awkward time trying to move the component in its parent. One way around this is to use a glassPane. Fortunately, there is an example of this in the tutorial (url shown in code comment below).
The middle button is a JToggleButton used to allow dragging of the textArea. The other two are JButtons.
Code:
import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.MouseInputAdapter; public class DragTest extends JPanel implements ActionListener { Rectangle rect = new Rectangle(); JTextArea textArea = new JTextArea(); GlassPane glassPane = new GlassPane(this); public DragTest() { glassPane.setVisible(false); setLayout(null); textArea.setOpaque(false); textArea.setLineWrap(true); textArea.setWrapStyleWord(true); } public void actionPerformed(ActionEvent e) { String ac = e.getActionCommand(); if(ac.equals("ADD TEXTAREA")) { if(getComponentCount() == 0) { add(textArea); } textArea.setBounds(rect); textArea.requestFocusInWindow(); repaint(); } if(ac.equals("DRAG TEXTAREA")) { boolean selected = ((AbstractButton)e.getSource()).isSelected(); glassPane.setVisible(selected); if(selected) { textArea.setEditable(false); textArea.setFocusable(false); textArea.setBorder(BorderFactory.createLineBorder(Color.blue)); } else { textArea.setEditable(true); textArea.setFocusable(true); textArea.setBorder(BorderFactory.createEmptyBorder(1,1,1,1)); } } if(ac.equals("REMOVE TEXTAREA")) { if(getComponentCount() > 0) { remove(textArea); repaint(); } } } protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.setPaint(Color.red); g2.draw(rect); } private JPanel getControls() { String[] ids = { "add textArea", "drag textArea", "remove textArea" }; JPanel panel = new JPanel(); for(int i = 0; i < ids.length; i++) { AbstractButton button; if(i == 1) button = new JToggleButton(ids[i], false); else button = new JButton(ids[i]); button.setActionCommand(ids[i].toUpperCase()); button.addActionListener(this); panel.add(button); } return panel; } public static void main(String[] args) { DragTest test = new DragTest(); test.addMouseListener(test.mia); test.addMouseMotionListener(test.mia); JFrame f = new JFrame(); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setGlassPane(test.glassPane); f.add(test); f.add(test.getControls(), "Last"); f.setSize(400,400); f.setLocation(200,200); f.setVisible(true); } private MouseInputAdapter mia = new MouseInputAdapter() { Point start; boolean dragging = false; public void mousePressed(MouseEvent e) { start = e.getPoint(); dragging = true; } public void mouseReleased(MouseEvent e) { dragging = false; } public void mouseDragged(MouseEvent e) { if(dragging) { rect.setFrameFromDiagonal(start, e.getPoint()); repaint(); } } }; } class GlassPane extends JComponent { DragTest component; JTextArea textArea; public GlassPane(DragTest dt) { component = dt; textArea = component.textArea; addMouseListener(mia); addMouseMotionListener(mia); } private MouseInputAdapter mia = new MouseInputAdapter() { Point offset = new Point(); boolean dragging = false; public void mousePressed(MouseEvent e) { Point p = e.getPoint(); if(!textArea.isEditable()) { Rectangle r = textArea.getBounds(); if(r.contains(p)) { // Mouse is over textArea. offset.x = p.x - r.x; offset.y = p.y - r.y; dragging = true; } else { // Mouse may be over gui button area. forwardEvent(e); } } } /** * Code adapted from GlassPaneDemo.java found at * http://java.sun.com/docs/books/tutorial/ * uiswing/components/rootpane.html */ private void forwardEvent(MouseEvent e) { Container topLevel = getTopLevelAncestor(); Container contentPane = ((JFrame)topLevel).getContentPane(); Point p = e.getPoint(); p = SwingUtilities.convertPoint(e.getComponent(), p, contentPane); Component comp = SwingUtilities.getDeepestComponentAt(contentPane, p.x, p.y); if(comp != null && comp instanceof AbstractButton) { Point loc = SwingUtilities.convertPoint(GlassPane.this, p, comp); comp.dispatchEvent(new MouseEvent(comp, e.getID(), e.getWhen(), e.getModifiers(), loc.x, loc.y, e.getClickCount(), e.isPopupTrigger())); } } public void mouseReleased(MouseEvent e) { dragging = false; forwardEvent(e); } public void mouseDragged(MouseEvent e) { if(dragging) { int x = e.getX() - offset.x; int y = e.getY() - offset.y; textArea.setLocation(x,y); // textArea is non-opaque so parent draws it. component.repaint(); } } }; }
Bookmark Post in Technorati
Reply With Quote
  #15 (permalink)  
Old 06-30-2008, 08:14 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
Keep on sculpting your tick bricks because I’ll read and study them all. As a beginning (java) programmer I am delighted with the swing (black) boxes. This kind of “module-programming” is relatively easy. The trouble starts, using these handy components, when some features are not present, such as dragging a text components (which makes since because they are not for dragging at the first place). A professional programmer should look for a good alternative but an amateur programmer (like me) tries to find some (dirty) tricks to make it work anyway.

I did read before about glass panes, but I did not understand what to do with them. Your hint is interesting and I pulled your code through the compiler but can get the text to appear yet….. okay I might need some more time.
In general: I should draw a glass over all the JPanel and do the mouse dragging on its transparent surface? Interesting idea (I was thinking of removing and adding the text comp… but that would be absurd…). I´ll need some more time and I´ll come back to the subject.

willemjav
Bookmark Post in Technorati
Reply With Quote
  #16 (permalink)  
Old 07-11-2008, 11:59 PM
Senior Member
 
Join Date: Dec 2007
Location: Spain
Posts: 210
willemjav is on a distinguished road
computer are sophisticated type-writers
Returning to the Glasspane I have a hard time in understanding Hardwired´s code. I need to do some more studying. I do believe that I have gained a basic understanding of Java. But this Glasspane code is to complicated for me, is there any good text book for advanced reading that you could recommend, hardwired?

Let me explain what I understood so far of the Glasspane concept.
A Glasspane is not a swing component (there is no JGlasspane), it is actually a feature of some swing components. So one creates a class called e.g. MyGlasspane which extends JComponent (or some other component, see the api under setGlassPane()). One could do for instance the mouse-drag listening in that class. Once an object is created of that class (called mgp) one should say that, that object is going to function as a glasspane (I my case the object forms an transparent layer over the image edit panel where I would like to drag a JTextArea).

When saying something like setGlassPane(mgp) (telling java to tread the object mgp as a glasspane). The problem is that the top container is a JPanel and JPanels do not like glasspanes (there is probably some profound reason that lays in inherence or whatever).
So I changed the top container into a JFrame and found out that JFrames do not like paintComponent´s stuff.

So here my labyrinth, hardwired?
You see my point why I think computer are sophisticated type-writers because they can´t really figure out what I want!

willemjav










PHP Code:
package setimages;


/**
 *
 * 
 */
/*
 * Main.java
 *
 * Created on March 2, 2008, 8:10 PM
 * The first class of setimages, called Loadimagesource
 * looks for the folder Imagestore that contains the photos
 * and the text file Slidessource.txt that sets the applet
 * parameters (in case there is no such folder the class creates one.
 * The second class, SetimagePanel, installs all the GUI controllers
 * for manipulating the images preparing them for the slideshow applet
 * The third class Editimages displays the image and the info text
 * here the actual photo editing takes place.
 * There will be an other class that simulates the applet slideshow
 * to see the result of the edit.
 +
 */



import java.awt.*;
import java.awt.Image;
import java.awt.event.*;
import java.util.Scanner;
import javax.imageio.ImageIO;
import javax.swing.*;
import java.net.URL;
import java.io.*;
import java.awt.image.BufferedImage;
import java.net.MalformedURLException;

   
    
    public class 
Editimages extends JPanel implements 
            
MouseListenerMouseMotionListener {
         
        private 
JFileChooser fileDialog;
        private 
BufferedImage image;
        private 
Image pic;
        private 
int imgwidthimgheightnewwidthnewheightframeh,
                
framewframexframeyprevXprevYfrdragXfrdragY
                
txtdragXtxtdragYimagepanelXimagepanelY,
                
infotextXinfotextY;
        private 
Scanner scanner;
        private 
Component infoPane;
        private 
JScrollPane scrollpane;
        private 
JTextArea infotextPanel;
        private 
File filepath;
        private 
String infotext;
        private 
Font smallFontnormalFontlargeFont
                
verylargeFontcurrentFont;
        private 
boolean framedraginfotextdragframelocktextinfolock;
        static private 
GlassPanel glasspanel;
        
        
        
     public 
Editimages(int framewint frameh)   {
        
        
        
imagepanelX 800;
        
imagepanelY 480;
        
setPreferredSize(new Dimension(imagepanelXimagepanelY));
        
this.framew framew;
        
this.frameh frameh;
        
framex imagepanelX/- (framew/2);
        
framey imagepanelY/- (frameh/2);
        
frdragX framex;
        
frdragY framey;
        
infotextPanel = new JTextArea();
        
infotextPanel.setEditable(false);
        
infotextPanel.setLineWrap(false);