import java.awt.Color;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame {
JPanel jPanel = new JPanel ();
public Draw () {
jPanel.setBackground (Color.red);
add (jPanel);
Drawation drawaction = new Drawation () ;/ / add the drawing, the above jpanel set to cover ; if the first add a drawing and then add
add (drawaction); ; / / jpanel put the drawing covers
}
public static void main (String [] args) {
Draw draw = new Draw ();
draw.setTitle ("abc");
draw.setSize (300,300);
draw.setVisible (true);
draw.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
class Drawation extends JPanel {
public void paintComponent (Graphics g) {
super.paintComponents (g);
g.drawString ("agagh", 50, 45);
}
}
This is just a simple validation code , no meaning . Want to know how to be in the dynamic drawing while retaining the original jpanel on a variety of components, but can draw on in their
------ Solution ------------- -------------------------------
JLayer example. JXLayer ( from swinglabs website ) similar , JLayer is JXLayer into java 7 results.
import javax.swing.*;
import javax.swing.plaf.LayerUI;
import java.awt.*;
public class JLayerSample {
private static JLayer<JComponent> createLayer() {
// This custom layerUI will fill the layer with translucent green
// and print out all mouseMotion events generated within its borders
LayerUI<JComponent> layerUI = new LayerUI<JComponent>() {
public void paint(Graphics g, JComponent c) {
// paint the layer as is
super.paint(g, c);
// fill it with the translucent green
g.setColor(new Color(0, 128, 0, 128));
g.fillRect(0, 0, c.getWidth(), c.getHeight());
}
public void installUI(JComponent c) {
super.installUI(c);
// enable mouse motion events for the layer's subcomponents
((JLayer) c).setLayerEventMask(AWTEvent.MOUSE_MOTION_EVENT_MASK);
}
public void uninstallUI(JComponent c) {
super.uninstallUI(c);
// reset the layer event mask
((JLayer) c).setLayerEventMask(0);
}
// overridden method which catches MouseMotion events
public void eventDispatched(AWTEvent e, JLayer<? extends JComponent> l) {
System.out.println("AWTEvent detected: " + e);
}
};
// create a component to be decorated with the layer
JPanel panel = new JPanel();
panel.add(new JButton("JButton"));
// create the layer for the panel using our custom layerUI
return new JLayer<JComponent>(panel, layerUI);
}
private static void createAndShowGUI() {
final JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// work with the layer as with any other Swing component
frame.add(createLayer());
frame.setSize(200, 200);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public static void main(String[] args) throws Exception {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}
------ For reference only ----------------------------------- ----
in a position to add components , the later will replace the front .
1.7 before you can use JXLayer, 1.7 can be used JLayer.
------ For reference only -------------------------------------- -
Thank you for your answer first
What do you mean ?
------ For reference only ---------------------------- -----------
is it like this ?
http://happy.host898.net/java/album/index.html
------ For reference only -------------------------------------- -
are you going to draw background image inside JFrame or what you means
------ For reference only --------- ------------------------------
almost meant it. But this is to insert a picture it, I want to use graphics drawing . I wonder if these two are not of the same nature
------ For reference only ---------------------------------- -----
at first you should insert the definit earticle that is "the" before JFrame, and then if you want ; to ask me you should say what do you mean rather than what you means, last something that you are not good at, don ' t come out to make a fool of yourself
------ For reference only ------------------ ---------------------
you use a JPanel as the bottom of the container , and then draw on this JPanel not on the line ?
override paintComponent method
the first one to call super.paintComponent (g) on the line
This method implements double buffering and will not overwrite the existing controls on JPanel
------ For reference only ---------------- -----------------------
I also do that ah , my code is not meant do you say that . Where not seen double buffering
------ For reference only ------------------------------- --------
you can draw on the GlassPane before components are not in on it so it will not contentPane cover GlassPane you can also control the display or not
- ---- For reference only ---------------------------------------
< br />
I said, and you write that meant little difference, changed your class , you see the effect
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame {
MainPane mainPane;
public Draw() {
setTitle("abc");
setSize(300, 300);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setContentPane(getMainPane());
setVisible(true);
}
/**
* 初始化并返回底层面板,
* 你所有的其他控件都应该添加在这个底层面板上
* @return
*/
protected JPanel getMainPane() {
if (mainPane == null) {
mainPane = new MainPane();
mainPane.add(new JButton("1"));
mainPane.add(new JButton("2"));
mainPane.add(new JButton("3"));
mainPane.add(new JButton("4"));
mainPane.add(new JButton("5"));
mainPane.add(new JButton("6"));
}
return mainPane;
}
public static void main(String[] args) {
new Draw();
}
class MainPane extends JPanel {
public void paintComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2d = (Graphics2D)g;
int width = this.getWidth();
int height = this.getHeight();
g2d.setPaint(new GradientPaint(0, 0, Color.white,
width, height, Color.cyan));
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.black);
g.drawString("agagh", 50, 45);
}
}
}
------ For reference only ----------------------------------- ----
check your own question and make it more readable and clear
firstly before you posted the thread!!
i could not catch on what you said at all ...
the intent of the comments is not to beat you
just beat someone who regard others as a fool
------ For reference only ----------------- ----------------------
no more.
------ For reference only ----- ----------------------------------
Thank you for your answer , it looks really achieve that does not cover , to achieve a certain goal . If you want to draw the dynamics of program execution . For example there is a determination condition is true then the mapping, does not hold is canceled . I generally do, but did not determine the conditions yo piece of code . But to say there is an abnormality
import java.awt.Graphics;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class Draw extends JFrame {
private JPanel jPanel = new JPanel ();
Graphics g = jPanel.getGraphics ();
public Draw () {
g.drawString ("agagh", 50, 45);
jPanel.add (new JButton ("d"));
setContentPane (jPanel);
}
public static void main (String [] args) {
Draw draw = new Draw ();
draw.setTitle (" Zhao Wei made " ) ;
draw.setSize (300,300);
draw.setVisible (true);
draw.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
}
}
------ For reference only --------------------------------- ------
learning ............ I also tangled , and my drawing is to use a for loop , the drawing results , there is no flash
没有评论:
发表评论