How to use Graphics in swings?

Hi,
I would like to write a Graphics application in swings, Is it possibel?
help me.

Hi,
I would like to write a Graphics application in
swings, Is it possibel?
help me.Yes! a short example using JApplet in swing using graphics.
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.JApplet;
public class Animate extends JApplet implements Runnable {
    Thread runner;
    boolean notDone;
    int x, y;
    public void init(){
        notDone = true;
        x = 10;
        y = 20;
        setSize(500,100);
        startRunning();
    public void run(){
        while(notDone){
            if(x < 400){
                x++; repaint();
                try {
                    Thread.sleep(500); // control the speed
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
            } else {
                x = 10;
    public void startRunning(){
        runner = new Thread(this);
        runner.start();
    public void paint(Graphics g){
        animate(g);
    public void repaint(){
        try{
            Image img = this.createImage(getSize().width , getSize().height);
            Graphics g = img.getGraphics();
            g.setColor(this.getBackground());
            g.fillRect(0, 0, getSize().width, getSize().height);
            g.setColor(this.getForeground());
            paint(g);
            this.getGraphics().drawImage(img, 0, 0, null);
        }catch(NullPointerException e){}
    public void animate(Graphics g){
        g.drawString("Help me -->", x, y);
}

Similar Messages

  • How to use stysheet for Swing components?

    Hi,
    Give me a guidence abt how to use Stylesheet for Swing components to crate good looking GUI...........thnx

    thank you for your reply,
    I want to know how many times a component is refreshed. I'm asumming the RefreshCondition attribute gets called everytime a component gets refreshed, hence it could be counted.
    I'm just not sure how to use it.

  • How to Use Graphics Map

    Hi,
    i want to create events on the  graphics as the maps in BSP.  we can do this in HTML using <map> and <area>extensions.
    There is <graphics> extension in BSP, but i don't know how to use this extension.
    <%@extension name="graphics" prefix="graphics" %>
    <graphics:map>
    </graphics:map>
    Does anybody know how to use the graphics as maps in bsp?  Can somebody help me please?
    Thanks.

    Look at the below sample BSP application:
    GRAPH_BSP_TEST
    GRAPH_TUT_CHART
    GRAPH_TUT_JNET
    Hope it might help.
    Raja

  • Need help with using graphics in swing components

    Hi. I'm new to Java and trying to learn it while also developing an application for class this semester. I've been following online tutorials for about 2 months now, though, and so I'm not sure my question counts as a "new to Java" question any more as the code is quite long.
    Here is the basic problem. I started coding the application as a basic awt Applet (starting at "Hello World") and about a month in realized that Swing components offer better buttons, panels, layouts, etc. So I converted the application, called BsfAp, to a new JApplet and started adding JPanels and JComponents with layout managers. My problem is, none of the buffered graphics run in any kind of JPanel, only the buttons do. I assume the buffered graphics are written straight to the JApplet top level container instead but I'm not entirely sure.
    So as to not inundate the forum with code, the JApplet runs online at:
    http://mason.gmu.edu/~dho2/files/sensor.html
    The source code is also online at:
    http://mason.gmu.edu/~dho2/files/BsfAp.java
    What I would like to do is this - take everything in the GUI left of the tabbed button pane and put it into a JScrollPane so that I can use a larger grid size with map display I can scroll around. The grid size I would like to use is more like 700x1000 pixels, but I only want to display about 400x400 pixels of it at a time in the JScrollPane. Then I could also move this JScrollPane around with layout manager. I think this is possible, but I don't know how to do it.
    I'm sure the code is not organized or optimized appropriately to those of you who use Java every day, but again I'm trying to learn it. ;-)
    Thanks for any help or insight you could provide in this.
    Matt

    Couple of recs:
    * Don't override paint and paint directly on the JApplet. Paint on a JPanel and override paintComponent.
    * The simplest way to display a graphic is to put an image into an ImageIcon and show this in a JLabel. This can then easily go inside of the JScrollPane.
    * You can also create a graphics JPanel that overrides the paintComponent, draw the image in that and show that inside of the JScrollPane.
    * don't call paint() directly. Call repaint if you want the graphic to repaint.
    Here's a trivial example quickly put together:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.FlowLayout;
    import java.awt.GradientPaint;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.Paint;
    import java.awt.RenderingHints;
    import java.awt.geom.Ellipse2D;
    import java.lang.reflect.InvocationTargetException;
    import javax.swing.JApplet;
    import javax.swing.JPanel;
    import javax.swing.JScrollPane;
    import javax.swing.SwingUtilities;
    public class BsfCrap extends JApplet
        private JPanel mainPanel = new JPanel();
        private JScrollPane scrollPane;
        private JPanel graphicsPanel = new JPanel()
            @Override
            protected void paintComponent(Graphics g)
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D)g;
                RenderingHints rh = new RenderingHints(
                    RenderingHints.KEY_ANTIALIASING,
                    RenderingHints.VALUE_ANTIALIAS_ON);
                g2d.setRenderingHints(rh);
                Paint gPaint = new GradientPaint(0, 0, Color.blue,
                    40, 40, Color.magenta, true);
                g2d.setPaint(gPaint);
                g2d.fill(new Ellipse2D.Double(0, 0, 800, 800));
        public BsfCrap()
            mainPanel.setPreferredSize(new Dimension(400, 400));
            mainPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
            graphicsPanel.setPreferredSize(new Dimension(800, 800));
            graphicsPanel.setBackground(Color.white);
            scrollPane = new JScrollPane(graphicsPanel);
            scrollPane.setPreferredSize(new Dimension(300, 300));
            mainPanel.add(scrollPane);
        public JPanel getMainPanel()
            return mainPanel;
        @Override
        public void init()
            try
                SwingUtilities.invokeAndWait(new Runnable()
                    public void run()
                        setSize(new Dimension(400, 400));
                        getContentPane().add(new BsfCrap().getMainPanel());
            catch (InterruptedException e)
                e.printStackTrace();
            catch (InvocationTargetException e)
                e.printStackTrace();
    }

  • How to use com.bruceeckel.swing

    Im trying to execute some code that comes in bruce eckel�s thinking in java, but it imports com.bruceeckel.swing, and the compiler (eclipse) doesnt know where the package is located. How do i use those classes that come with the book? where do i have to place them? please, anyone who�s gone trhu this, please explain

    Im trying to execute some code that comes in bruce
    eckel�s thinking in java, but it imports
    com.bruceeckel.swing, and the compiler (eclipse)
    doesnt know where the package is located. How do i
    use those classes that come with the book? where do i
    have to place them? please, anyone who�s gone trhu
    this, please explainDownload the class files or their source code and add/import them into your project in Eclipse.

  • How to use MVC in Swing application

    Hi,
    I'm creating an application using Java Swing. I want to use Model View Controller (MVC) architecture to design by application. I want to use MVC to seperate by business logic functionality from the control and presentation logic that uses this logic.
    I need your help to get this done successfully. Please send me a SAMPLE JAVA SWING APPLICATION where they have applied MVC, it will be helpful to me.
    Thank you,
    Regards
    kumar

    Would this be a basic example of an application that uses MVC?
    //MODEL
    public class Model {
         public void showMessage() {
              System.out.println("You clicked me!");
    //VIEW
    import javax.swing.JButton;
    import javax.swing.JPanel;
    public class View extends JPanel {
         private JButton buttonSave;
         public View() {
              super();
              initUI();
         public void initUI() {
              buttonSave = new JButton("Save");
              buttonSave.addActionListener(new Controller());
              add(buttonSave);
    //CONTROLLER
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    public class Controller implements ActionListener {
         private Model m;
         public Controller() {
              m = new Model();
         public void actionPerformed(ActionEvent e) {
              m.showMessage();
    }Is this a correct implementation of MVC? I am trying to learn it, and use it in an example.
    Thanks for your help/input.

  • How use jdbc with swing ? i am new in this concept.

    hi
    freinds,
    i am new in java can any one give the example which explain the how to use jdbc with swing.
    thanks in advace.
    satish
    thanks.

    See here  >  http://support.apple.com/kb/HT4236
    More info in the User Guides for your Devices...

  • How to use series in business graphic ui element

    Hi All,
    How to use the series in business graphic ui element? my requirement is i have to display the sales in selected months in graph(these values dont know at design time).
    Regards
    Srikanth

    Hi,
    Use the dynamic node  and bind this node to the BG source node and series nodes.
    Create a node and bind this node to the BG source and the value that you want to shown in series has to be bound to that attribute of the node and similarly with the category.
    Check out for the dynamic node creation and bind this node to the BG.
    Regards,
    Lekha.

  • How to use the fonts in the graphics menu

    I am looking to add text from the graphics options on the right hand side of the screen.  When I double click or drag one of the text options to my project a text box appears, however the text is not the one selected from the options on the right – instead the text appears and I can change the text from the list of standard fonts at the lower left - and the font I double clicked from the menu on the left isn't listed. 
    How can I select and use the fonts listed on the graphics list at the right?   I am looking to use the gray and black "outline" font.
    Thank you!

    When I press the "+" at the lower right corner of the screen I am presented with, among other things, a selection of font types.  I am able to change the text color in the graphics menu but can't figure out how to use one of the fonts listed in the menu.  For example, from the graphics menu I select "type" then "text" and from the fonts listed I double click "outline" and a text box appears on my project with "your text here", however the font is "rosewood std" and the only fonts available are from the typical list - no outline font as listed in the graphics menu on the right.   When I double click the text type "outline" the font doesn't change.  I think I am missing a step somewhere but can't figure it out.   Any help would be appreciated.  Thanks.

  • How to use annotation with GUI in swing

    hi,
    i am new to annotation. I know how to use this with classes, methods and java elements. I am developing a tool in applet to post comment on my notepad, but unable to find that how to use this with GUI of swing. I have gone through most of forums and tutorials but got no idea. Anyone could give me little idea.
    Thanks

    >
    i need to use the applet mousedown function in swing..>There is no such class as 'applet' in the JRE, Swing has an upper case 1st letter, functions in Java are generally referred to as methods, and there is no 'mousedown' method in the JRE.
    OTOH a Swing JApplet inherits the mouseDown method from Component, and it was deprecated in Java 1.1. The documentation (JavaDocs) provides instructions on what to use instead.
    >
    is there any way to use it or any other alternative is available for this?
    if can provide me a sample code.>If can provide me cash. Short of that, you might actually need to do some research/coding of your own.
    As an aside. What does any of this have to do with Java 2D?

  • How to use 'Wait'/'Delay' in Swing

    Hi all,
    I want to simulate an interation between two peers,
    that exchange information among each other.
    The idea is to have a JPanel where JLabel("Text") are
    added in a fixed but consecutive intervals. Something like
    <Add label>; <Delay/wait a second>;<Add other label>; <Delay>;.....
    Is there an easy/simpler way to do this without using
    threads?
    Does Swing have a simple way to do what I want?
    Can you recomend a link for this?
    Pls note that I don't want to block my entire application.
    Thanks in advance for the help,
    Jorge

    Here is a link to the Swing tutorial on "How to Use Timers":
    http://java.sun.com/docs/books/tutorial/uiswing/misc/timer.html
    And here is a link to to thread that uses a Timer to 'continuously scroll text':
    http://forum.java.sun.com/thread.jsp?forum=57&thread=291161

  • How to do "not equals" mapping using Graphical

    Hi friends
    i want to do a mapping like this:
    if A is not equal to b then value is 1
    elseif ...something
    Anyone please tell me how can i achieve this using graphical tool.
    Thanks

    Hi,
    it is very simple.
    Use equals function
    A input and second input constant(B) give it to equals function. output will be true or false give it to ifthenelse. inthen use constant(somthing value) ans in else use constant with 1 value and map it to target element.
    U can write UDF
    if(A.equals("b"))
    result("something");
    else
    result("1");
    Thnx
    Chirag.

  • How to run 3d swing on the browser? how to use pugin  HTML conventer??

    hi dear all,
    I have problem now. I want to run my program in the browser. i use java 3d swing in my code as well. I look from books.It is saying that is possiable to do it , but , you have to use the java pugin conventer . I have tried , and it is not work on my computer , i have been doing just this for a week . If someone can give me some help , i will really really greatful about it .
    Thank you in advance .
    regards
    jojo

    What kind of command - HTML is a markup language not an execution language. If you mean how can you display an HTML document then use WEB.SHOW_DOCUMENT()

  • How to use  Advance java function in graphical mapping in XI 2.0?

    Hi,
    currently I am using a simple java function to make an RFC call to R3 system.
    I want to avoid making connection for each lookup. Instead I want to make a single connection for whole message queue and get the corresponding values in some array or container object.
    please suggest a solution.
    I think this is possible using Advance java function, but I am not able to find any example on using Advance java function at help.sap.com.
    thaks in advance.

    Hi!!!
    I'm not sure if I understood you well.
    Do you want to preload some data into your structures in the memory and keep them there so you don't need to make a new connection during processing the whole message or every message?
    In my opinion you can cache some data during processing a message, but it's impossible to cache some data between processing messages.
    If you write your java mapping or you use graphical mapping (even with user-defined function), then you have a java class. The problem is that XI 2.0 reloads this class during processing every message, so even if you load some data from your data source into your structures in the memory, this data will be lost after reloading your mapping class.
    Regards,
    Andrzej Filusz

  • How to use Toolkit.getDefaultToolkit().getImage in JPanel in Java Swing

    Hi Sir,
    I want to know how to use Toolkit.getDefaultToolkit.getImage()
    in JPanel (ie) the class that extends the JPanel.There is no problem to include the Toolkit.getDefaultToolkit.getImage()in JFrame.It is displaying it without any problem.But in JPanel it is showing a problem of cannot resolve symbol.Here i can't use JFrame instead of
    JPanel.So how to solve the problem.Pls.do send the code of how to do this.It is Urgent.
    Thanx,
    M.Ananthu

    you use Toolkit.getDefaultToolkit.getImage() in a JPanel exactly the same way you do in a JFrame.
    You are getting a compile message "cannot resolve symbol". You have made a programming error and since you have not included code there is no way to guess what you did wrong.

Maybe you are looking for

  • Jabber Cannot Control Desk Phone

    I know, I know.  A well-worn topic.  But it's just not happening for me, and I'm new to Presence 9.1  This is a new installation. Also using CUCM 9.1 and Jabber 9.2.6.  I don't even know where the Jabber error logs are. Anyway, when I try to get Jabb

  • FBL5N Header Chacacteristics

    OSS note 393290 indicates to me that you can add additional characteristics to the FBL5N header fields.  For example SORT2 It is clearly an accepted method for inserting characteristics into the report headers, with limitations as to the tables permi

  • JDeveloper -- Plug-In(Webgrid)

    Hi, I am using JDeveloper 11g(11.1.2.3.0), Oracle Database 11.2.0.3. I did not discover a component, besides the ADF table or tree, for loading dynamic data from the database. The ADF table is restricted in its functions. So, my question is: Is there

  • Report Painter - Characteristic cannot be combined with key figures

    Hi, I have added a new field (ZZABTEI) to the report structure CCSS and using that field as a characteristic in the report. But when I execute the report, it is giving the error "Characteristic ZZABTEI cannot be combined with key figures". Can anyone

  • Information about members of strcut

    I am programming in Solaris 2.5. I need information about members of some struct such as proc, user, as. I can find their definitons in .h files but cannot get their information in detail. How or where can I get these? Heartless