Posted by : Unknown
Friday, 19 June 2015
import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
public class TransparencyDemo extends JPanel
import javax.swing.*;
import javax.swing.event.*;
public class TransparencyDemo extends JPanel
{
public static void main(String[] args)
public static void main(String[] args)
{
JFrame window = new JFrame("TransparencyDemo");
TransparencyDemo content = new TransparencyDemo();
window.setContentPane(content);
window.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private JSlider triangleTransparency;
private JSlider ovalTransparency;
private JSlider rectangleTransparency;
private JSlider textTransparency;
private Font textFont = new Font("Serif",Font.BOLD,42);
private JPanel display = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color triangleColor = new Color(255,0,0,triangleTransparency.getValue());
Color ovalColor = new Color(0,255,0,ovalTransparency.getValue());
Color rectangleColor = new Color(0,0,255,rectangleTransparency.getValue());
Color textColor = new Color(0,0,0,textTransparency.getValue());
g.setColor(triangleColor);
g.fillPolygon(new int[] { scaleX(0.25), scaleX(0.7), scaleX(0.1) },
new int[] { scaleY(0.1), scaleY(0.7), scaleY(0.7) },
3);
g.setColor(ovalColor);
g.fillOval(scaleX(0.3),scaleY(0.45),getWidth()/2,getHeight()/2);
g.setColor(rectangleColor);
g.fillRect(scaleX(0.4),scaleY(0.15),getWidth()/2,getHeight()/2);
FontMetrics metrics = g.getFontMetrics(textFont);
int lineWidth1 = metrics.stringWidth("Transparency");
int lineWidth2 = metrics.stringWidth("Demo");
int textHeight = metrics.getHeight() + metrics.getAscent();
int topSkip = (getHeight() - textHeight) / 2;
int leftSkip1 = (getWidth() - lineWidth1) / 2;
int leftSkip2 = (getWidth() - lineWidth2) / 2;
g.setColor(textColor);
g.setFont(textFont);
g.drawString("Transparency", leftSkip1, topSkip + metrics.getAscent());
g.drawString("Demo", leftSkip2, topSkip + metrics.getAscent() + metrics.getHeight());
}
private int scaleX(double x) {
return (int)(x * getWidth());
}
private int scaleY(double y) {
return (int)(y * getHeight());
}
};
private ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
display.repaint();
}
};
public TransparencyDemo() {
setLayout(new BorderLayout(3,3));
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
setBackground(Color.GRAY);
display.setBackground(Color.WHITE);
display.setPreferredSize(new Dimension(400,300));
add(display,BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(4,2,3,3));
add(bottom,BorderLayout.SOUTH);
bottom.add( new JLabel(" Triangle Transparency:") );
triangleTransparency = new JSlider(0,255);
triangleTransparency.addChangeListener(listener);
bottom.add( triangleTransparency );
bottom.add( new JLabel(" Oval Transparency:") );
ovalTransparency = new JSlider(0,255);
ovalTransparency.addChangeListener(listener);
bottom.add( ovalTransparency );
bottom.add( new JLabel(" Rectangle Transparency:") );
rectangleTransparency = new JSlider(0,255);
rectangleTransparency.addChangeListener(listener);
bottom.add( rectangleTransparency );
bottom.add( new JLabel(" Text Transparency:") );
textTransparency = new JSlider(0,255);
textTransparency.addChangeListener(listener);
bottom.add( textTransparency );
}
}
JFrame window = new JFrame("TransparencyDemo");
TransparencyDemo content = new TransparencyDemo();
window.setContentPane(content);
window.pack();
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
window.setLocation( (screenSize.width - window.getWidth())/2,
(screenSize.height - window.getHeight())/2 );
window.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
window.setVisible(true);
}
private JSlider triangleTransparency;
private JSlider ovalTransparency;
private JSlider rectangleTransparency;
private JSlider textTransparency;
private Font textFont = new Font("Serif",Font.BOLD,42);
private JPanel display = new JPanel() {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Color triangleColor = new Color(255,0,0,triangleTransparency.getValue());
Color ovalColor = new Color(0,255,0,ovalTransparency.getValue());
Color rectangleColor = new Color(0,0,255,rectangleTransparency.getValue());
Color textColor = new Color(0,0,0,textTransparency.getValue());
g.setColor(triangleColor);
g.fillPolygon(new int[] { scaleX(0.25), scaleX(0.7), scaleX(0.1) },
new int[] { scaleY(0.1), scaleY(0.7), scaleY(0.7) },
3);
g.setColor(ovalColor);
g.fillOval(scaleX(0.3),scaleY(0.45),getWidth()/2,getHeight()/2);
g.setColor(rectangleColor);
g.fillRect(scaleX(0.4),scaleY(0.15),getWidth()/2,getHeight()/2);
FontMetrics metrics = g.getFontMetrics(textFont);
int lineWidth1 = metrics.stringWidth("Transparency");
int lineWidth2 = metrics.stringWidth("Demo");
int textHeight = metrics.getHeight() + metrics.getAscent();
int topSkip = (getHeight() - textHeight) / 2;
int leftSkip1 = (getWidth() - lineWidth1) / 2;
int leftSkip2 = (getWidth() - lineWidth2) / 2;
g.setColor(textColor);
g.setFont(textFont);
g.drawString("Transparency", leftSkip1, topSkip + metrics.getAscent());
g.drawString("Demo", leftSkip2, topSkip + metrics.getAscent() + metrics.getHeight());
}
private int scaleX(double x) {
return (int)(x * getWidth());
}
private int scaleY(double y) {
return (int)(y * getHeight());
}
};
private ChangeListener listener = new ChangeListener() {
public void stateChanged(ChangeEvent arg0) {
display.repaint();
}
};
public TransparencyDemo() {
setLayout(new BorderLayout(3,3));
setBorder(BorderFactory.createLineBorder(Color.GRAY,2));
setBackground(Color.GRAY);
display.setBackground(Color.WHITE);
display.setPreferredSize(new Dimension(400,300));
add(display,BorderLayout.CENTER);
JPanel bottom = new JPanel();
bottom.setLayout(new GridLayout(4,2,3,3));
add(bottom,BorderLayout.SOUTH);
bottom.add( new JLabel(" Triangle Transparency:") );
triangleTransparency = new JSlider(0,255);
triangleTransparency.addChangeListener(listener);
bottom.add( triangleTransparency );
bottom.add( new JLabel(" Oval Transparency:") );
ovalTransparency = new JSlider(0,255);
ovalTransparency.addChangeListener(listener);
bottom.add( ovalTransparency );
bottom.add( new JLabel(" Rectangle Transparency:") );
rectangleTransparency = new JSlider(0,255);
rectangleTransparency.addChangeListener(listener);
bottom.add( rectangleTransparency );
bottom.add( new JLabel(" Text Transparency:") );
textTransparency = new JSlider(0,255);
textTransparency.addChangeListener(listener);
bottom.add( textTransparency );
}
}
Output:
Related Posts :
- Back to Home »
- Example program , Frames »
- Color Transparency