Re: Graphics and JVMs
- From: sshark <sshark@xxxxxxxxx>
- Date: Tue, 06 Nov 2007 04:48:01 -0000
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.
wanted. I find that if I use double buffer method I will lose theFrom the help I got from here, I arrived to this and this is what I
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);
}
}
.
- Follow-Ups:
- Re: Graphics and JVMs
- From: Knute Johnson
- Re: Graphics and JVMs
- References:
- Graphics and JVMs
- From: sshark
- Re: Graphics and JVMs
- From: sshark
- Re: Graphics and JVMs
- From: Knute Johnson
- Graphics and JVMs
- Prev by Date: Re: Graphics and JVMs
- Next by Date: Re: Graphics and JVMs
- Previous by thread: Re: Graphics and JVMs
- Next by thread: Re: Graphics and JVMs
- Index(es):
Relevant Pages
|
|