Re: Graphics and JVMs




I'm not clear what you want to do with your message. It is very easy
however to scale the box drawings. Assume a standard size, in this case
400x300, and transform your Graphics component accordingly.

### code omitted ###


--

Knute Johnson
email s/nospam/knute/

Yes I used the same setup to transform my graphics objects but like I
said I only want to scale the drawing in my panel but not the message
e.g. the boxes will be twice in size after scaling but the message
character size remains.

From the help I got from here, I arrived to this and this is what I
wanted. I find that if I use double buffer method I will lose the
message box transparency. This is probably caused by drawImage(...)
method


import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Point;
import java.util.Random;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JViewport;


public class TestRandomBoxes extends JFrame {
private Random pointRandomizer = new Random();

public TestRandomBoxes() {

final JPanel p = new JPanel(){
@Override
public void paintComponent(Graphics g) {
g.setColor(getBackground());
g.fillRect(0, 0, 200, 200);
g.setColor(Color.BLACK);
g.fillRect(50 + randomPoint().x,
50 + randomPoint().y, 50, 50);
}
};
p.setPreferredSize(new Dimension(200, 200));

final JViewport viewport = new JViewport() {
@Override
public void paint(Graphics g) {
super.paint(g);
int x=50;
int y=50;
String message = "Hi! There";

int w = g.getFontMetrics().stringWidth(message)
+ 10 ;
int h = g.getFontMetrics().getHeight() + 4;

g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
g.fillRect(x, y, w, h);
g.setColor(new Color(0x33, 0xFF, 0xFF));
g.drawRect(x, y, w, h);
g.setColor(Color.BLACK);
g.drawString(message, x, y + h - 4);

/* double buffering method
Image bufferImage = createImage(w + 1, h + 1);
Graphics buffer = bufferImage.getGraphics();
buffer.setColor(new Color(0x33, 0xFF, 0xFF, 50));
buffer.fillRect(0, 0, w, h);
buffer.setColor(new Color(0x33, 0xFF, 0xFF));
buffer.drawRect(0, 0, w, h);
buffer.setColor(Color.BLACK);
buffer.drawString(message, 0, h - 4);

g.drawImage(bufferImage, x, y, null);
buffer.dispose();
*/
}
};
viewport.add(p);
JScrollPane scrollPane = new JScrollPane();
scrollPane.setViewport(viewport);

getContentPane().add(scrollPane);

Thread animator = new Thread() {
@Override
public void run() {
while (true) {
repaint();
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
};
animator.start();
}

private void drawMessage(Graphics g, String message) {
int w = g.getFontMetrics().stringWidth(message) + 10;
int h = g.getFontMetrics().getHeight() + 4;

g.setColor(new Color(0x33, 0xFF, 0xFF, 50));
g.fillRect(5, 5, w, h);
g.setColor(new Color(0x33, 0xFF, 0xFF));
g.drawRect(5, 5, w, h);
g.setColor(Color.BLACK);
g.drawString(message, 8, (5 + h - 4));
}

private Point randomPoint() {
return new Point(pointRandomizer.nextInt(50),
pointRandomizer.nextInt(50));
}

public static void main(String[] args) {
TestRandomBoxes sl = new TestRandomBoxes();
sl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
sl.setBounds(600,450, 100, 100);
sl.validate();
sl.setVisible(true);
}
}


.



Relevant Pages

  • printing contents of JPanel on multiple pages...
    ... I've made a class My_panel which extends JPanel and implements the ... I do some drawings in the paintComponent(Graphics ... public void paintComponent{ ... public int print(Graphics g, PageFormat pageFormat, int ...
    (comp.lang.java.programmer)
  • Re: Java API sound
    ... I was using mixer but port is better solution; ... private void CreateInterface ... public void actionPerformed{ ... int d = -1; ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... private void init() { ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)
  • JFrame Resize Issues (Redux)
    ... However, when the LookAndFeel is set, Java's Window Manager ... public int OffSetX = 0; ... private Rectangle positRectangle; ... public void componentHidden{ ...
    (comp.lang.java.programmer)
  • Re: mehrfach unterstreichen =?ISO-8859-1?Q?m=F6glich=3F?=
    ... */ public class FlowDOMView extends JPanel {private DefaultTreeModel model = null; ... private void fill(DefaultMutableTreeNode node, int depth) { ... public void changedUpdate{ ... return vGap; ...
    (de.comp.lang.java)