Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.gui > #2724

Re: Graphics and JVMs

From "sshark" <sshark@THRWHITE.remove-dii-this>
Subject Re: Graphics and JVMs
Message-ID <1194324481.487224.326510@y27g2000pre.googlegroups.com> (permalink)
Newsgroups comp.lang.java.gui
References <DxIXi.3405$Tp3.1077@newsfe15.lga>
Date 2011-04-27 15:40 +0000
Organization TDS.net

Show all headers | View raw


  To: comp.lang.java.gui
>
> 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);
        }
}

---
 * Synchronet * The Whitehouse BBS --- whitehouse.hulds.com --- check it out free usenet!
--- Synchronet 3.15a-Win32 NewsLink 1.92
Time Warp of the Future BBS - telnet://time.synchro.net:24

Back to comp.lang.java.gui | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
  Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
        Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
          Re: Graphics and JVMs "Knute Johnson" <knute.johnson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
  Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
  Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
    Re: Graphics and JVMs "sshark" <sshark@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
      Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
        Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
          Re: Graphics and JVMs "Roedy Green" <roedy.green@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000
            Re: Graphics and JVMs "Andrew Thompson" <andrew.thompson@THRWHITE.remove-dii-this> - 2011-04-27 15:40 +0000

csiph-web